寻找一种使用 ggplot 绘制以下相关数据的方法

Looking for a way to plot the following correlation data using ggplot

我正在尝试可视化以下数据,希望得到一些建议。基本上我 运行 一堆相关性并且想可视化变量 A 或变量 B 是否与身高、体重、体积等更密切相关

variable <- c('A','B','A','B','A','B')
outcome <- c('Height','Height','Weight', 'Weight', 'Volume', 'Volume')
correlation_coeff <- c(0.76, 0.65, 0.77,0.56,0.91,-0.34)
p_value<- c(0.04,0.03,0.01,0.02,0.001,0.09)

data <- data.frame(variable, outcome, correlation_coeff, p_value)

因为这不是相关系数矩阵(例如,我从未研究过身高和体重之间的相关性),所以我不确定该怎么做。通常我只使用 ggcorrplot() 函数,但在这种情况下它显然不起作用。有什么想法吗?

ggcorrplot() 将矩阵作为输入,因此您只需将数据转换为矩阵即可:

variable <- c('A','B')
outcome <- c('Height','Weight', 'Volume')
correlation_coeff <- c(0.76, 0.65, 0.77,0.56,0.91,-0.34)
p_value<- c(0.04,0.03,0.01,0.02,0.001,0.09)

data <- matrix(correlation_coeff, nrow = 2, ncol = 3, dimnames = list(variable,outcome))

ggcorplot(data)

这应该会按照您正在寻找的样式制作 2x3 相关图表。

您可以直接使用 geom_tile 绘制相关图,这与 ggcorrplot 的外观非常相似。

您可以选择性地覆盖 p 值:

ggplot(data, aes(variable, outcome, fill = correlation_coeff)) +
  geom_tile(color = "black") +
  geom_text(aes(label = paste("p =", p_value)), size = 10) +
  scale_fill_gradientn(colors = c("blue", "white", "red"), limits = c(-1, 1)) +
  theme_minimal() +
  theme(axis.title = element_blank(),
        axis.text = element_text(size = 16)) +
  labs(fill = "Correlation coefficient") +
  coord_equal()