title: “correlation plots” author: “leela” date: “May 18, 2016” output: html_document
install.packages(“corrplot”) library(corrplot) head(mtcars)
M<-cor(mtcars) head(round(M,2))
Correlogram : Visualizing the correlation matrix
corrplot(M, method=“number”)
“lower”: display lower triangular of the correlation matrix
corrplot(M, type=“lower”,method=“number”) corrplot(M, type=“upper”,method=“number”)
Changing the color of the correlogram
library(RColorBrewer) corrplot(M, type=“full”, order=“hclust”,col=brewer.pal(n=8, name=“PuOr”),method=“number”)
Changing the color and the rotation of text labels
corrplot(M, type=“full”, order=“hclust”, tl.col=“purple”, tl.srt=45,method=“number”)
Computing the p-value of correlations
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(mtcars) head(p.mat[, 1:5])
Add significance level to the correlogram
corrplot(M, type=“full”, order=“hclust”,p.mat = p.mat, sig.level = 0.01,method=“number”)
corrplot(M, type=“full”, order=“hclust”, p.mat = p.mat, sig.level = 0.01, insig = “blank”,method=“number”)
Customize the correlogram
col <- colorRampPalette(c(“#BB4444”, “#EE9988”, “#FFFFFF”, “#77AADD”, “#4477AA”)) corrplot(M, method=“color”, col=col(400),
type=“full”, order=“hclust”, addCoef.col = “black”, # Add coefficient of correlation tl.col=“black”, tl.srt=45, #Text label color and rotation # Combine with significance p.mat = p.mat, sig.level = 0.01, insig = “blank”, # hide correlation coefficient on the principal diagonal diag=FALSE )