我想知道我是否可以为两组数据生成组合的corrplot?也就是说,一组的上三角形和另一组的下三角形。我举一个例子如下:
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))
如果有人可以将这两个数字合并为一个数字,那将会很有趣?提前谢谢!
答案 0 :(得分:2)
您可以将一个相关矩阵的值替换为另一个的值。
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))
数据强>
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)))
答案 1 :(得分:1)
这是一种可能性:
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))