2017-03-01 71 views
0

我試圖從數據框中按組/方面繪製相關圖。如果我爲每個變量分配數據,我可以做到這一點。我怎樣才能一次對所有的變量做這個事情,以便根據每個變量生成分面圖?R方面或分組相關和相關圖圖

###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) 

這很好,因爲我從數據框中剔除了一個變量「virginica」。我如何自動執行此操作來進行獨特的相關性計算,然後將所有單個變量作爲單個方面進行corplot?

回答

1

據我所知你想要一個corplot爲每個Species級別。 所以,你可以嘗試:

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) 
}) 

enter image description here

+0

好的,但也可以考慮標記答案爲接受。 – Jimbou

0

@Jimbou,感謝您的代碼。我已經編輯了一下,在一個代碼中添加了相關分析,獨特的R和繪圖,並且還爲每個繪圖添加了一個唯一的名稱。 Plot with titles

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)}