ggpairs图与相关值的热图 [英] ggpairs plot with heatmap of correlation values

查看:182
本文介绍了ggpairs图与相关值的热图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是双重的;

我有一个默认值为upper = list(continuous = cor)的ggpairs图,我想通过相关值(完全类似于ggcorr的颜色)为图块上色.

I have a ggpairs plot with the default upper = list(continuous = cor) and I would like to colour the tiles by correlation values (exactly like what ggcorr does).

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

I have this:
I would like the correlation values of the plot above to be coloured like this:

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)有一段简短的尝试,但是没有任何运气,并且鉴于两个函数都返回绘图调用,我认为这不是正确的方法吗?

I had a brief go at trying to use upper = list(continuous = wrap(ggcorr) but didn't have any luck and, given that both functions return plot calls, I don't think that's the right path?

我知道我可以在ggplot中构建它(例如

I am aware that I could build this in ggplot (e.g. Sandy Muspratt's solution) but given that the GGally package already has the functionality I am looking for I thought I might be overlooking something.

更广泛地说,我想知道我们如何称呼相关值?一个简单的选择可能是给标签着色而不是为瓷砖着色(即此问题使用颜色而不是大小),但是我需要一个变量来分配颜色...

More broadly, I would like to know how we, or if we can, call the correlation values? A simpler option may be to colour the labels rather than the tile (i.e. this question using colour rather than size) but I need a variable to assign to colour...

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

Being able to call the correlation values to use in other plots would be handy although I suppose I could just recalculate them myself.

谢谢!

推荐答案

一种可能的解决方案是从ggcorr相关矩阵图中获取颜色列表,并将这些颜色设置为情节矩阵.

A possible solution is to get the list of colors from the ggcorr correlation matrix plot and to set these colors as background in the upper tiles of the ggpairs matrix of plots.

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)

这篇关于ggpairs图与相关值的热图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆