2017-03-01 5 views
0

Ich versuche, ein Korrelogramm nach Gruppe/Facette aus einem Datenrahmen zu plotten. Ich bin in der Lage, dies zu tun, wenn ich die Daten für jede Variable unterteile. Wie kann ich dies für alle Variablen auf einmal tun, um Facettengrafiken basierend auf jeder Variablen zu generieren?Facette oder gruppierte Korrelation und Korrelogramm Plots in 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) 

Das funktioniert gut, weil ich nur eine Variable „virginica“ nahm aus dem Datenrahmen. Wie automatisiere ich dies, um eine eindeutige Korrelationsrechnung zu erhalten und dann für alle einzelnen Variablen als einzelne Facetten zu korrelieren?

Antwort

1

Wie ich verstehe, wollen Sie ein corrplot für jeden Species Ebene. So können Sie versuchen:

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

OK, aber auch in Betracht ziehen, die Antwort als akzeptiert zu markieren. – Jimbou

0

@Jimbou, vielen Dank für Ihren Code. Ich habe es ein wenig bearbeitet, um die Korrelationsanalyse hinzuzufügen, R und Plot in einem Code zu plotten und jedem Plot einen eindeutigen Namen hinzuzufügen. 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)}