【问题标题】:ggpairs plot with heatmap of correlation values带有相关值热图的 ggpairs 绘图
【发布时间】:2017-08-25 02:21:26
【问题描述】:

我的问题是双重的;

我有一个默认为 upper = list(continuous = cor) 的 ggpairs 图,我想通过相关值对图块着色(就像 ggcorr 所做的那样)。

我有这个:
我希望上图的相关值像这样着色:

library(GGally)

sample_df <- data.frame(replicate(7,sample(0:5000,100)))
colnames(sample_df) <- c("KUM", "MHP", "WEB", "OSH", "JAC", "WSW", "gaugings")

ggpairs(sample_df, lower = list(continuous = "smooth"))  
ggcorr(sample_df, label = TRUE, label_round = 2)

我曾短暂尝试使用 upper = list(continuous = wrap(ggcorr),但运气不佳,而且鉴于这两个函数都返回绘图调用,我认为这不是正确的路径吗?

我知道我可以在 ggplot 中构建它(例如 Sandy Muspratt's solution),但鉴于 GGally 包已经具有我正在寻找的功能,我想我可能会忽略一些东西。


更广泛地说,我想知道我们如何(或者如果可以的话)调用相关值?一个更简单的选择可能是为标签而不是瓷砖着色(即this question 使用颜色而不是大小),但我需要一个变量来分配颜色......

能够调用相关值以在其他图中使用会很方便,尽管我想我可以自己重新计算它们。

谢谢!

【问题讨论】:

    标签: r ggplot2 ggally aesthetics


    【解决方案1】:

    一种可能的解决方案是从ggcorr 相关矩阵图中获取颜色列表,并将这些颜色设置为ggpairs 矩阵图上方图块中的背景。

    library(GGally)   
    library(mvtnorm)
    # Generate data
    set.seed(1)
    n <- 100
    p <- 7
    A <- matrix(runif(p^2)*2-1, ncol=p) 
    Sigma <- cov2cor(t(A) %*% A)
    sample_df <- data.frame(rmvnorm(n, mean=rep(0,p), sigma=Sigma))
    colnames(sample_df) <- c("KUM", "MHP", "WEB", "OSH", "JAC", "WSW", "gaugings")
    
    # Matrix of plots
    p1 <- ggpairs(sample_df, lower = list(continuous = "smooth"))  
    # Correlation matrix plot
    p2 <- ggcorr(sample_df, label = TRUE, label_round = 2)
    

    相关矩阵图为:

    # Get list of colors from the correlation matrix plot
    library(ggplot2)
    g2 <- ggplotGrob(p2)
    colors <- g2$grobs[[6]]$children[[3]]$gp$fill
    
    # Change background color to tiles in the upper triangular matrix of plots 
    idx <- 1
    for (k1 in 1:(p-1)) {
      for (k2 in (k1+1):p) {
        plt <- getPlot(p1,k1,k2) +
         theme(panel.background = element_rect(fill = colors[idx], color="white"),
               panel.grid.major = element_line(color=colors[idx]))
        p1 <- putPlot(p1,plt,k1,k2)
        idx <- idx+1
    }
    }
    print(p1)
    

    【讨论】:

      【解决方案2】:

      您可以通过编写一个可以直接传递给ggpairs 的快速自定义函数将背景颜色映射到单元格。这涉及计算变量对之间的相关性,然后匹配到一些用户指定的颜色范围。

      my_fn <- function(data, mapping, method="p", use="pairwise", ...){
      
                    # grab data
                    x <- eval_data_col(data, mapping$x)
                    y <- eval_data_col(data, mapping$y)
      
                    # calculate correlation
                    corr <- cor(x, y, method=method, use=use)
      
                    # calculate colour based on correlation value
                    # Here I have set a correlation of minus one to blue, 
                    # zero to white, and one to red 
                    # Change this to suit: possibly extend to add as an argument of `my_fn`
                    colFn <- colorRampPalette(c("blue", "white", "red"), interpolate ='spline')
                    fill <- colFn(100)[findInterval(corr, seq(-1, 1, length=100))]
      
                    ggally_cor(data = data, mapping = mapping, ...) + 
                      theme_void() +
                      theme(panel.background = element_rect(fill=fill))
                  }
      

      使用 Marco 回答中的数据:

      library(GGally)    # version: ‘1.4.0’
      
      p1 <- ggpairs(sample_df, 
                         upper = list(continuous = my_fn),
                         lower = list(continuous = "smooth"))  
      

      这给出了:


      后续问题Change axis labels of a modified ggpairs plot (heatmap of correlation) 指出,theme 的后期绘图更新导致panel.background 颜色被删除。这可以通过删除theme_void 并删除主题中的网格线来解决。即将相关位更改为(注意 ggplot2 v3.3.0 不需要此修复)

      ggally_cor(data = data, mapping = mapping, ...) + 
                 theme(panel.background = element_rect(fill=fill, colour=NA),
                       panel.grid.major = element_blank()) 
      

      【讨论】:

      • 是否有可能使用您的解决方案更改 x 轴标签? stackoverflow.com/questions/60930523/…
      • @user20650:如果我将带有+ theme_minimal() 的主题添加到p1panel.background 颜色将再次被删除。有什么建议可以在哪里添加主题以使其最终不会被删除?非常感谢!
      • 嗨@mavericks;您能否解释一下您的预期结果,因为我希望 theme_minimal 会去除面板颜色(即p=ggplot()+theme(panel.background = element_rect(fill="red")); p ; p +theme_minimal())。但是是的,它可能是/在 ggpairs 中设置主题元素有点痛苦。
      • @mavericks;有一种方法可以更简单地做到这一点,但您可以将各个部分拼凑在一起以获得最终效果(我认为您想要)。将 theme_minimal 添加到下部和对角线图并删除 strip.background 。所以试试; ggpairs(sample_df, upper = list(continuous = my_fn), lower = list(continuous = function(...) ggally_points(...)+theme_minimal()), diag = list(continuous = function(...) ggally_densityDiag(...)+theme_minimal()))+ theme(strip.background = element_blank())
      • @user20650 非常感谢您的意见!刚刚添加了this SO question 总结了您之前的建议,这对我的情节很有帮助!再次感谢
      猜你喜欢
      • 2020-09-23
      • 1970-01-01
      • 2015-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-02
      • 1970-01-01
      • 2021-11-18
      相关资源
      最近更新 更多