合并两组数据的corrplot?
Combined corrplot for data with two groups?
我想知道我是否可以为两组数据生成一个组合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))
现以本例为例,数字如下:
如果有人可以将这两个数字合并为一个数字,我会很感兴趣?提前致谢!
您可以用另一个相关矩阵的值替换一个相关矩阵的值。
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)))
这是一种可能性:
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))
我想知道我是否可以为两组数据生成一个组合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))
现以本例为例,数字如下:
如果有人可以将这两个数字合并为一个数字,我会很感兴趣?提前致谢!
您可以用另一个相关矩阵的值替换一个相关矩阵的值。
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)))
这是一种可能性:
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))