【问题标题】:Make rectangular matrix of correlation values in R, possibly using corrplot在 R 中制作相关值的矩形矩阵,可能使用 corrplot
【发布时间】:2020-06-25 23:05:49
【问题描述】:

我想在 R 中制作一个相关值矩阵。但是,我没有将参数与它们本身进行比较,因此它不是典型的 corrplot,它是一个三角形并镜像在对角轴上。我实际上已经有了相关值

我只是希望招募corrplot 函数(来自corrplot package),它会生成如下图像:

我的数据,已经包含我想要绘制的相关值,是:

            X    animal.1   animal.2     animal.3    animal.4   animal.5
1 parameter 1  0.10258087  0.3338782  0.150246554  0.07295133  0.2484152
2 parameter 2  0.04205748  0.4062727 -0.002101464  0.12068818  0.2951127
3 parameter 3  0.11264488  0.4114954  0.067145776  0.13361071  0.3246052
4 parameter 4 -0.02261649 -0.2426341  0.108042167 -0.12820517 -0.2005686
5 parameter 5 -0.01576384 -0.2300852  0.112941655 -0.12391976 -0.1906473
6 parameter 6 -0.09749030 -0.3110920  0.021994297 -0.13570257 -0.2557532

我希望绘图看起来像一个 corrplot,但它将是一个完整的矩形。我想要不同直径的圆圈,正值或负值的两种不同颜色,以及沿图边的颜色渐变。本质上,我想要一个矩形值表的 corrplot 主题。

我还要求查看是否可以将星号放置在具有显着 (p

structure(list(X = structure(1:6, .Label = c("parameter 1", "parameter 2", 
"parameter 3", "parameter 4", "parameter 5", "parameter 6"), class = "factor"), 
Animal.1 = c(0.2454906, 0.63471, 0.2019519, 0.7984066, 0.8587147, 
0.2698293), Animal.2 = c(0.000103586, 1.62e-06, 1.15e-06, 
0.005412082, 0.008451775, 0.000315107), Animal.3 = c(0.08796553, 
0.9810688, 0.447827, 0.2211191, 0.2007585, 0.8038392), Animal.4 = c(0.4094627, 
0.1713835, 0.1296492, 0.1460429, 0.1601174, 0.1236984), Animal.5 = c(0.004374306, 
0.000653099, 0.00016464, 0.02213469, 0.0298038, 0.003315349
)), .Names = c("X", "animal.1", "animal.2", "animal.3", "animal.4", 
"animal.5"), class = "data.frame", row.names = c(NA, -6L))

【问题讨论】:

  • 请不要以图片形式提供您的数据。没有人想再次全部输入。相反,请使用 dput 创建数据的文本版本并将结果粘贴到您的问题中。另外,请提供您用来创建您现在拥有的 corrplot 的代码您可能应该阅读How do I ask a good question?
  • 两张图片都是为了展示我想要的。我拍了一张我在网上找到的随机 corrplot 图片示例。提供我的数据只是为了展示我想要的绘图形状。我不知道 dput 是什么。我是 stackoverflow 的新手
  • 这行得通吗?我认为我的输入正确
  • 你一定已经从一些数据中得到了相关值——为什么不直接使用原始数据,这就是corrplot 的设计目的?
  • 原始数据格式不正确,否则我不知道如何提取数据。我不是那么精通R。我做所有事情都笨拙但公式化

标签: r ggplot2 plot correlation r-corrplot


【解决方案1】:

当您使用ggplot2 标记此问题时,我为您提供了一种使用ggplot2 的方法。

首先,您需要重塑包含相关值的数据框(此处命名为 df)和具有 pvalue 的数据框(此处命名为 df_pval)。 为此,例如,您可以使用 tidyr 包中的 pivot_longer 函数(也是 tidyverse 包的一部分)。

在包含 pvalues 的数据框中,我们将添加一列 Label 以便用星号标记所有低于 0.05 的值。

library(tidyr)
library(dplyr)
library(ggplot2)

corr_values <- df %>% pivot_longer(-X, names_to = "Animal", values_to = "value") 

# Here an extract of the dataframe with correlation values after reshaping it:
  X           Animal    value
  <fct>       <chr>     <dbl>
1 parameter 1 animal.1 0.103 
2 parameter 1 animal.2 0.334 
3 parameter 1 animal.3 0.150 
4 parameter 1 animal.4 0.0730
5 parameter 1 animal.5 0.248 
6 parameter 2 animal.1 0.0421
corr_pval <- df_pval %>% 
  pivot_longer(-X, names_to = "Animal", values_to = "p.value") %>%
  mutate(Label = ifelse(p.value < 0.05, "*",NA)) 

# Here an extract of the dataframe obtained
  X           Animal    p.value Label
  <fct>       <chr>       <dbl> <chr>
1 parameter 1 animal.1 0.245    NA   
2 parameter 1 animal.2 0.000104 *    
3 parameter 1 animal.3 0.0880   NA   
4 parameter 1 animal.4 0.409    NA   
5 parameter 1 animal.5 0.00437  *    
6 parameter 2 animal.1 0.635    NA   

然后,您可以使用geom_pointgeom_tile 获取热图。 您还可以通过传递scale_size_continuousscale_color_gradient 等各种函数来自定义绘图的颜色、大小等。

最后,您可以通过将新数据框 corr_pval 调用到 geom_text 并指定适当的 aesthetic 参数来为 pvalues 添加星号:

library(ggplot2)

ggplot(corr_values, aes(x = X, y = Animal))+
  geom_tile(color = "black", fill = "white")+
  geom_point(aes(color = value, size = abs(value)))+
  scale_color_gradient2(low = "green", mid = "white", midpoint = 0, high = "red", name = "")+
  scale_size_continuous(range = c(5,15), name = "")+
  geom_text(data = corr_pval, aes(label = Label), size = 8, vjust = 0.7, hjust = 0.5)

所以,你应该得到类似的东西:

【讨论】:

    【解决方案2】:

    我认为您只需将 df 转换为矩阵,然后运行 ​​corrplot 函数。

    library(corrplot)
    
    #Transform data to matrix
    matrix_cor<-as.matrix(df[,-1])
    
    #Set row names as df first column
    row.names(matrix_cor)<-df[,1]
    
    corrplot(matrix_cor,
             method = "circle")
    

    【讨论】:

    • 你做的正是我想要的。点赞一百万次,谢谢大佬!实际上还有一件事,有没有办法在每个圆圈上画一个星号,或者在图上指示其他方式,如果它是显着的(p
    • 我从 cor.test 中提取了动物 3 参数 3 的 p 值,它并不像您的代码通过在此处放置星号所显示的那样重要
    • 是的,抱歉,您必须在原始数据上运行cor.mtest,而不是相关矩阵。所以,你可以先result&lt;-cor.mtest("original_data", conf.level = 0.95) 然后corrplot(matrix_cor, p.mat = result$p, insig = "label_sig", sig.level = 0.05, pch.cex = 1.5, pch.col = "black")
    • 我刚刚通过单独复制粘贴来自 cor.test 的所有 p 值创建了一个新的 csv 矩阵。你可以用它代替并将它覆盖在情节上吗?我已将该表添加到原始问题中
    • 另外,我的原始数据与我发布的这些简化数据集的结构不同,所以很遗憾我无法引用原始数据
    猜你喜欢
    • 1970-01-01
    • 2021-11-24
    • 2019-07-28
    • 2018-12-05
    • 1970-01-01
    • 2018-02-04
    • 2014-02-04
    • 2019-12-07
    • 1970-01-01
    相关资源
    最近更新 更多