#####correlation matrix heatmap
rm(list = ls())
library(tidyverse)
## -- Attaching packages ---------------------------------------------------------------------- tidyverse 1.3.0 --
## √ ggplot2 3.3.2 √ purrr 0.3.4
## √ tibble 3.0.3 √ dplyr 1.0.1
## √ tidyr 1.1.2 √ stringr 1.4.0
## √ readr 1.3.1 √ forcats 0.5.0
## -- Conflicts ------------------------------------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
#recommend method_2
#################################################method_1
#Prepare the data
mydata <- mtcars[, c(1,3,4,5,6,7)]
head(mydata);dim(mydata);class(mydata)
## mpg disp hp drat wt qsec
## Mazda RX4 21.0 160 110 3.90 2.620 16.46
## Mazda RX4 Wag 21.0 160 110 3.90 2.875 17.02
## Datsun 710 22.8 108 93 3.85 2.320 18.61
## Hornet 4 Drive 21.4 258 110 3.08 3.215 19.44
## Hornet Sportabout 18.7 360 175 3.15 3.440 17.02
## Valiant 18.1 225 105 2.76 3.460 20.22
## [1] 32 6
## [1] "data.frame"
#Compute the correlation matrix
cormat <- round(cor(mydata),2)
cormat
## mpg disp hp drat wt qsec
## mpg 1.00 -0.85 -0.78 0.68 -0.87 0.42
## disp -0.85 1.00 0.79 -0.71 0.89 -0.43
## hp -0.78 0.79 1.00 -0.45 0.66 -0.71
## drat 0.68 -0.71 -0.45 1.00 -0.71 0.09
## wt -0.87 0.89 0.66 -0.71 1.00 -0.17
## qsec 0.42 -0.43 -0.71 0.09 -0.17 1.00
#Create the correlation heatmap with ggplot2
library(reshape2)
##
## Attaching package: 'reshape2'
## The following object is masked from 'package:tidyr':
##
## smiths
melted_cormat <- melt(cormat)
head(melted_cormat)
## Var1 Var2 value
## 1 mpg mpg 1.00
## 2 disp mpg -0.85
## 3 hp mpg -0.78
## 4 drat mpg 0.68
## 5 wt mpg -0.87
## 6 qsec mpg 0.42
ggplot(data = melted_cormat, aes(x=Var1, y=Var2, fill=value)) +
geom_tile()

#Get the lower and upper triangles of the correlation matrix
#Get lower triangle of the correlation matrix
lower_tri <- cormat
lower_tri[lower.tri(lower_tri)] <- NA #OR upper.tri function
lower_tri
## mpg disp hp drat wt qsec
## mpg 1 -0.85 -0.78 0.68 -0.87 0.42
## disp NA 1.00 0.79 -0.71 0.89 -0.43
## hp NA NA 1.00 -0.45 0.66 -0.71
## drat NA NA NA 1.00 -0.71 0.09
## wt NA NA NA NA 1.00 -0.17
## qsec NA NA NA NA NA 1.00
#Finished correlation matrix heatmap
melted_cormat <- reshape2::melt(lower_tri, na.rm = TRUE)
# Heatmap
ggplot(data = melted_cormat, aes(Var2, Var1, fill = value))+
geom_tile(color = "white")+
scale_fill_gradient2(low = "blue", high = "red", mid = "white",
midpoint = 0, limit = c(-1,1), space = "Lab",
name="Pearson\nCorrelation") +
theme_minimal()+
theme(axis.text.x = element_text(angle = 45, vjust = 1,
size = 12, hjust = 1))+
coord_fixed() +
geom_text(aes(Var2, Var1, label = value), color = "black", size = 4) +
theme(
axis.title.x = element_blank(),
axis.title.y = element_blank(),
panel.grid.major = element_blank(),
panel.border = element_blank(),
panel.background = element_blank(),
axis.ticks = element_blank(),
legend.justification = c(1, 0),
legend.position = c(0.6, 0.7),
legend.direction = "horizontal")+
guides(fill = guide_colorbar(barwidth = 7, barheight = 1,
title.position = "top", title.hjust = 0.5))

ggsave(filename = paste0(Sys.Date(),"-","-corr.tif"),
plot = last_plot(), device = "tiff", path = NULL,
width = 12, height = 12, units = "cm",
dpi = 300, limitsize = TRUE, compression = "lzw")
#############################################################method_2
#install.packages("ggcorrplot")
library(ggcorrplot)
cormat
## mpg disp hp drat wt qsec
## mpg 1.00 -0.85 -0.78 0.68 -0.87 0.42
## disp -0.85 1.00 0.79 -0.71 0.89 -0.43
## hp -0.78 0.79 1.00 -0.45 0.66 -0.71
## drat 0.68 -0.71 -0.45 1.00 -0.71 0.09
## wt -0.87 0.89 0.66 -0.71 1.00 -0.17
## qsec 0.42 -0.43 -0.71 0.09 -0.17 1.00
ggcorrplot(cormat)

## method = "circle"
ggcorrplot(cormat, method = "circle")

# Reordering the correlation matrix
# --------------------------------
# using hierarchical clustering
ggcorrplot(cormat, hc.order = TRUE, outline.col = "white")

# Types of correlogram layout
# --------------------------------
# Get the lower triangle
ggcorrplot(cormat, hc.order = TRUE, type = "lower",
outline.col = "white")

# Change colors and theme
# --------------------------------
# Argument colors ## or upper
ggcorrplot(cormat, hc.order = TRUE, type = "lower",
outline.col = "white",
ggtheme = ggplot2::theme_gray,
colors = c("#6D9EC1", "white", "#E46726"))

# Add correlation coefficients
# --------------------------------
# argument lab = TRUE
ggcorrplot(cormat, hc.order = TRUE, type = "lower",
lab = TRUE)

## Add correlation significance level
# --------------------------------
# Argument p.mat
# Barring the no significant coefficient
# Compute a matrix of correlation p-values
p.mat <- cor_pmat(mydata)
p.mat
## mpg disp hp drat wt
## mpg 0.000000e+00 9.380327e-10 1.787835e-07 1.776240e-05 1.293959e-10
## disp 9.380327e-10 0.000000e+00 7.142679e-08 5.282022e-06 1.222320e-11
## hp 1.787835e-07 7.142679e-08 0.000000e+00 9.988772e-03 4.145827e-05
## drat 1.776240e-05 5.282022e-06 9.988772e-03 0.000000e+00 4.784260e-06
## wt 1.293959e-10 1.222320e-11 4.145827e-05 4.784260e-06 0.000000e+00
## qsec 1.708199e-02 1.314404e-02 5.766253e-06 6.195826e-01 3.388683e-01
## qsec
## mpg 1.708199e-02
## disp 1.314404e-02
## hp 5.766253e-06
## drat 6.195826e-01
## wt 3.388683e-01
## qsec 0.000000e+00
ggcorrplot(cormat, hc.order = TRUE,
type = "lower", p.mat = p.mat)

# Leave blank on no significant coefficient
ggcorrplot(cormat, p.mat = p.mat, hc.order = TRUE,
type = "lower", insig = "blank")

#ref http://www.sthda.com/english/wiki/ggplot2-quick-correlation-matrix-heatmap-r-software-and-data-visualization
#http://www.sthda.com/english/wiki/ggcorrplot-visualization-of-a-correlation-matrix-using-ggplot2