Visit my website for more like this!
References
Most of this material is borrowed from:
This lesson teach you how to plot a correlogram in R. Correlogram is a graph of correlation matrix. It is very useful to highlight the most correlated variables in a data table. In this plot, correlation coefficients is colored according to the value. Correlation matrix can be also reordered according to the degree of association between variables. The R corrplot package is used here.
corrplot package is required to execute the R code in this article.
#install.packages("corrplot")
library(corrplot)
Data for correlation analysis
The mtcars data is used to compute correlation matrix.
head(mtcars)
## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
## Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
## Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
## Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
library(broom)
M<-cor(mtcars)
tidy(head(round(M,2)))
## .rownames mpg cyl disp hp drat wt qsec vs am gear
## 1 mpg 1.00 -0.85 -0.85 -0.78 0.68 -0.87 0.42 0.66 0.60 0.48
## 2 cyl -0.85 1.00 0.90 0.83 -0.70 0.78 -0.59 -0.81 -0.52 -0.49
## 3 disp -0.85 0.90 1.00 0.79 -0.71 0.89 -0.43 -0.71 -0.59 -0.56
## 4 hp -0.78 0.83 0.79 1.00 -0.45 0.66 -0.71 -0.72 -0.24 -0.13
## 5 drat 0.68 -0.70 -0.71 -0.45 1.00 -0.71 0.09 0.44 0.71 0.70
## 6 wt -0.87 0.78 0.89 0.66 -0.71 1.00 -0.17 -0.55 -0.69 -0.58
## carb
## 1 -0.55
## 2 0.53
## 3 0.39
## 4 0.75
## 5 -0.09
## 6 0.43
R corrplot function is used to plot the graph of the correlation matrix.
The simplified format of the function is :
corrplot(corr, method="circle")
corr: The correlation matrix to visualize. To visualize a general matrix, please use is.corr=FALSE.
method: The visualization method : “circle”, “color”, “number”, etc.
Seven different visualization methods can be used : “circle”, “square”, “ellipse”, “number”, “shade”, “color”, “pie”.
library(corrplot)
corrplot(M, method="circle")
corrplot(M, method="pie")
corrplot(M, method="color")
Positive correlations are displayed in blue and negative correlations in red color. Color intensity and the size of the circle are proportional to the correlation coefficients.
corrplot(M, method="number")
There are three types of layout :
“full” (default) : display full correlation matrix
“upper”: display upper triangular of the correlation matrix
“lower”: display lower triangular of the correlation matrix
corrplot(M, type="upper")
corrplot(M, type="lower")
The correlation matrix can be reordered according to the correlation coefficient. This is important to identify the hidden structure and pattern in the matrix. “hclust” for hierarchical clustering order is used in the following examples.
# correlogram with hclust reordering
corrplot(M, type="upper", order="hclust")
# Using different color spectrum
col<- colorRampPalette(c("red", "white", "blue"))(20)
corrplot(M, type="upper", order="hclust", col=col)
# Change background color to lightblue
corrplot(M, type="upper", order="hclust", col=c("black", "white"),
bg="lightblue")
As shown in the above section, the color of the correlogram can be customized. RcolorBrewer palette of colors are used in the R script below :
library(RColorBrewer)
corrplot(M, type="upper", order="hclust",
col=brewer.pal(n=8, name="RdBu"))
corrplot(M, type="upper", order="hclust",
col=brewer.pal(n=8, name="RdYlBu"))
corrplot(M, type="upper", order="hclust",
col=brewer.pal(n=8, name="PuOr"))
tl.col (for text label color) and tl.srt (for text label string rotation) are used to change text colors and rotations.
corrplot(M, type="upper", order="hclust", tl.col="black", tl.srt=45)
Combining correlogram with the significance test Computing the p-value of correlations
To compute the matrix of p-value, a custom R function is used:
# mat : is a matrix of data
# ... : further arguments to pass to the native R cor.test function
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)
tidy(head(p.mat[, 1:5]))
## .rownames mpg cyl disp hp
## 1 mpg 0.000000e+00 6.112687e-10 9.380327e-10 1.787835e-07
## 2 cyl 6.112687e-10 0.000000e+00 1.802838e-12 3.477861e-09
## 3 disp 9.380327e-10 1.802838e-12 0.000000e+00 7.142679e-08
## 4 hp 1.787835e-07 3.477861e-09 7.142679e-08 0.000000e+00
## 5 drat 1.776240e-05 8.244636e-06 5.282022e-06 9.988772e-03
## 6 wt 1.293959e-10 1.217567e-07 1.222320e-11 4.145827e-05
## drat
## 1 1.776240e-05
## 2 8.244636e-06
## 3 5.282022e-06
## 4 9.988772e-03
## 5 0.000000e+00
## 6 4.784260e-06
# Specialized the insignificant value according to the significant level
corrplot(M, type="upper", order="hclust", p.mat = p.mat, sig.level = 0.01)
# Leave blank on no significant coefficient
corrplot(M, type="upper", order="hclust",
p.mat = p.mat, sig.level = 0.01, insig = "blank")
In the above figure, correlations with p-value > 0.01 are considered as insignificant. In this case the correlation coefficient values are leaved blank or crosses are added.
col <- colorRampPalette(c("#BB4444", "#EE9988", "#FFFFFF", "#77AADD", "#4477AA"))
corrplot(M, method="color", col=col(200),
type="upper", 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
)