简体   繁体   English

R中的分面或分组相关和相关图

[英]facet or grouped correlation and correlogram plots in R

I'm trying to plot a correlogram by group/facet from a data frame. 我正在尝试从数据框中按组/构面绘制相关图。 I'm able to do this if I subset the data for each variable. 如果我将每个变量的数据子集化,我就能做到这一点。 How can I do this for all the variables at once to generate facet plots based on each variable? 如何一次对所有变量执行此操作,以基于每个变量生成构面图?

###Load libraries
library(gdata)
library(corrplot)
library(ggplot2)
library(gtable)
library(ggpmisc)
library(grid)
library(reshape2)
library(plotly)
packageVersion('plotly')

##Subset ample data from the "iris" data set in R
B<-iris[iris$Species == "virginica", ]

##calculate correlation for numeric columns only
M<-cor(B[,1:4])
head(round(M,2))

###calculate significance
cor.mtest <- function(mat, ...) {
mat <- as.matrix(mat)
n <- ncol(mat)
p.mat<- matrix(NA, n, n)
diag(p.mat) <- 0
for (i in 1:(n - 1)) {
    for (j in (i + 1):n) {
        tmp <- cor.test(mat[, i], mat[, j], ...)
        p.mat[i, j] <- p.mat[j, i] <- tmp$p.value
    }
}
colnames(p.mat) <- rownames(p.mat) <- colnames(mat)
p.mat
}
# matrix of the p-value of the correlation
p.mat <- cor.mtest(B[,1:4])

###plot
#color ramp
col<- colorRampPalette(c("red","white","blue"))(40)
corrplot(M, type="upper",tl.col="black", tl.cex=0.7,tl.srt=45, col=col,
p.mat = p.mat, insig = "blank", sig.level = 0.01)

This works well because I took out just one variable "virginica" from the data frame. 这很好,因为我从数据框中仅取出了一个变量“ virginica”。 How do I automate this to have a unique correlation calculation and then corrplot for all the individual variables as individual facets? 如何自动执行此操作以进行唯一的相关性计算,然后对所有单个变量作为各个方面进行Corrplot?

As I understand you want a corrplot for each Species level. 据我了解,您想要每个Species级别的Corrplot。 So, you can try: 因此,您可以尝试:

library(Hmisc) # this package has implemented a cor function calculating both r and p.  
library(corrplot)
# split the data 
B <- split(iris[,1:4], iris$Species)
# Calculate the correlation in all data.frames using lapply 
M <- lapply(B, function(x) rcorr(as.matrix(x)))

# Plot three pictures
par(mfrow=c(1,3))
col<- colorRampPalette(c("red","white","blue"))(40)
lapply(M, function(x){
corrplot(x$r, type="upper",tl.col="black", tl.cex=0.7,tl.srt=45, col=col,
         p.mat = x$P, insig = "blank", sig.level = 0.01)
})

在此处输入图片说明

@Jimbou, thanks for your code. @Jimbou,感谢您的代码。 I have edited it a bit to add the correlation analysis, unique R and plot in one code and also add a unique name to each plot. 我对其进行了一些编辑,以在一个代码中添加相关性分析,唯一的R和图,并为每个图添加唯一的名称。 标题图

library(ggplot2)
library(Hmisc) 
library(corrplot)
# split the data 
B <- split(iris[,1:4], iris$Species)
##extract names
nam<-names(B)
# Plot three pictures
par(mfrow=c(1,3))
col<- colorRampPalette(c("red","white","blue"))(40)
for (i in seq_along(B)){
# Calculate the correlation in all data.frames using lapply 
M<-rcorr(as.matrix(B[[i]]))
corrplot(M$r, type="upper",tl.col="black", tl.cex=0.7,tl.srt=45, col=col,
 addCoef.col = "black", p.mat = M$P, insig = "blank",sig.level = 0.01)
mtext(paste(nam[i]),line=1,side=3)}

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

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