【问题标题】:Plotting multiple correlation matrices by a categorical variable using ggcorrplot使用 ggcorrplot 通过分类变量绘制多个相关矩阵
【发布时间】:2018-09-15 12:30:39
【问题描述】:

我使用ggcorrplot 包和以下代码创建了一个简单的相关矩阵:

library(ggcorrplot)
corr <- round(cor(data[,18:24], use = "complete.obs"),2)
gg <- ggcorrplot(corr)
print(gg)

我现在想做的是使用相同的数据创建多个相关矩阵,但通过名为“区域”的分类变量(列位置“5”)将其分解:类似于使用 facet_wrap 函数。但是,当我尝试这样做时,出现错误。我尝试了以下方法:

library(ggcorrplot)
corr <- round(cor(data[,18:24], use = "complete.obs"),2)
gg <- ggcorrplot(corr) +
facet_wrap("region", ncol = 2)
print(gg)

我得到的错误是"Error in combine_vars(data, params$plot_env, vars, drop = params$drop) : At least one layer must contain all variables used for facetting"

我知道 'corr' 没有引用“区域”字段,我想知道如何才能做到这一点。所以基本上,输出将是由“区域”分隔的 6 个相关矩阵,而不是所有数据的一个相关矩阵。

【问题讨论】:

    标签: r ggplot2 r-corrplot


    【解决方案1】:

    使用ggcorrplot 可能无法做到这一点,它将相关矩阵作为其输入并将其融合到合适的数据帧中,然后用于某些特定的ggplot 内容以制作绘图。

    但是您可以使用ggcorrplot 源代码来获得您想要的。

    作为初步步骤,让我们看一下“融化”的相关矩阵。

    (small_cor <- cor(replicate(2, rnorm(25))))
    #>            [,1]       [,2]
    #> [1,] 1.00000000 0.06064063
    #> [2,] 0.06064063 1.00000000
    (reshape2::melt(small_cor))
    #>   Var1 Var2      value
    #> 1    1    1 1.00000000
    #> 2    2    1 0.06064063
    #> 3    1    2 0.06064063
    #> 4    2    2 1.00000000
    

    它是相关矩阵的数据框版本,其中每一行是来自原始数据的变量组合的相关性。

    现在我们将着手处理一些示例数据。有 6 个区域和 7 个变量。

    library(tidyverse)
    library(reshape2)
    
    my_data <- data.frame(region = factor(rep(1:6, each = 25)),
                          replicate(7, rnorm(6*25)))
    

    我们需要融合了区域 ID 的相关矩阵。这就是我的做法。可能有更好的方法。我认为这可能是您必须做的最棘手的事情。

    my_cors <- cbind(region = factor(rep(levels(my_data$region), each = 7^2)),
                  do.call(rbind, lapply(split(my_data, my_data$region), function(x) melt(cor(x[,-1])))))
    

    现在我将从ggcorrplot 源代码复制并粘贴。首先,从参数列表中粘贴以获得一些默认值:

    ggtheme = ggplot2::theme_minimal
    colors = c("blue", "white", "red")
    outline.color = "gray"
    legend.title = "Corr"
    tl.cex = 12
    tl.srt = 45
    

    现在我剪切并粘贴ggcorrplot 的相关部分并在最后粘贴facet_wrap 以获得您想要的。

    my_cors %>% 
      ggplot(aes(Var1, Var2, fill = value)) + 
      geom_tile(color = outline.color) + 
      scale_fill_gradient2(low = colors[1], 
                           high = colors[3], 
                           mid = colors[2], 
                           midpoint = 0,
                           limit = c(-1, 1), 
                           space = "Lab", 
                           name = legend.title) + 
      ggtheme() + theme(axis.text.x = element_text(angle = tl.srt,
                                                   vjust = 1, 
                                                   size = tl.cex, hjust = 1), 
                        axis.text.y = ggplot2::element_text(size = tl.cex)) + 
      coord_fixed() +
      facet_wrap("region", ncol=2)
    

    【讨论】:

    • 感谢您的意见。我试过你的代码,不幸的是它对我不起作用。然而,我所做的是创建了几个数据框,然后使用 ggpubr/ggarrange 包将所有相关矩阵包含在一个页面上。我用这种方法取得了成功,我非常感谢你向我展示了 reshape2 包的熔化功能。
    猜你喜欢
    • 1970-01-01
    • 2015-06-08
    • 2021-12-22
    • 2015-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-05
    相关资源
    最近更新 更多