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?
OK, aber auch in Betracht ziehen, die Antwort als akzeptiert zu markieren. – Jimbou