简体   繁体   English

将合并Corrplot与两组数据进行比较?

[英]Combined corrplot for data with two groups?

I wonder if I could generate a combined corrplot for two groups of data? 我想知道是否可以为两组数据生成组合Corrplot? That is to say, the upper triangular for one group, and the lower triangular for another group. 也就是说,一组为上三角,另一组为下三角。 I give an example as follows: 我举一个例子如下:

library(corrplot)

mydata <- replicate(5, rnorm(20))

colnames(mydata) <- c('x1','x2','x3','x4','x5')

mydata <- transform(mydata, group = c(rep('A',10),rep('B',10)))

corrplot(cor(mydata[which(mydata$group=='A'),c(1:5)]), method="number", type="upper", title="Group A", mar=c(1,0,1,0))
corrplot(cor(mydata[which(mydata$group=='B'),c(1:5)]), method="number", type="lower", title="Group B", mar=c(1,0,1,0))

Now in this example, the figures are as follows: 现在在此示例中,数字如下: 在此处输入图片说明 在此处输入图片说明

It would be interesting to me if somebody could combine these two figures into one figure ? 如果有人可以将这两个数字组合成一个数字,对我来说会很有趣吗? Thanks in advance! 提前致谢!

You could replace the values of one correlation matrix with the values from the other. 您可以将一个相关矩阵的值替换为另一个相关矩阵的值。

cor_A <- cor(mydata[which(mydata$group=='A'),c(1:5)])
cor_B <- cor(mydata[which(mydata$group=='B'),c(1:5)])

# replace the values
cor_A[lower.tri(cor_A)] <- cor_B[lower.tri(cor_B)]

corrplot(cor_A, method="number", title="Group A & B", mar=c(1,0,1,0))

在此处输入图片说明

data 数据

set.seed(1)
mydata <- replicate(5, rnorm(20))
colnames(mydata) <- c('x1','x2','x3','x4','x5')
mydata <- transform(mydata, group = c(rep('A',10),rep('B',10)))

Here is one possibility: 这是一种可能性:

library(corrplot)    
set.seed(1)
mydata <- replicate(5, rnorm(20))   
colnames(mydata) <- c('x1','x2','x3','x4','x5')    
mydata <- transform(mydata, group = c(rep('A',10),rep('B',10)))

cor_A = cor(mydata[which(mydata$group=='A'),c(1:5)])
cor_B = cor(mydata[which(mydata$group=='B'),c(1:5)])
my_cor = upper.tri(cor_A)*cor_A + lower.tri(cor_B) * cor_B

corrplot(cor(mydata[which(mydata$group=='A'),c(1:5)]), method="number", type="upper", title="Group A", mar=c(1,0,1,0))
corrplot(cor(mydata[which(mydata$group=='B'),c(1:5)]), method="number", type="lower", title="Group B", mar=c(1,0,1,0))
corrplot(my_cor, method="number", title="Group A (upper) and B (lower)", mar=c(1,0,1,0))

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM