火狐截图_2020-02-11T08-36-22.554Z.png
ggplot2:速绘相关性矩阵热图-R软件和数据可视化
1. 构建数据
rm(list = ls())
library(ggplot2)
mydata <- mtcars[, c(1, 3, 4, 5, 6, 7)]
head(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
#compute the correlation matrix
cormat <- round(cor(mydata), 2)
head(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)
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
2. 默认绘图 geom_tile()
#The function geom_tile()[ggplot2 package] is used to visualize the correlation matrix :
ggplot(data = melted_cormat, aes(x=Var1, y=Var2, fill=value)) +
geom_tile()
3. 重塑数据(上三角)
# Get upper triangle of the correlation matrix
get_upper_tri <- function(cormat){
cormat[lower.tri(cormat)]<- NA
return(cormat)
}
upper_tri <- get_upper_tri(cormat)
upper_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
#melt the correlation matrix
library(reshape2)
melted_cormat <- melt(upper_tri,na.rm = T)
4. 美化矩阵热图
# Heatmap
ggplot(data = melted_cormat, aes(x=Var2, y=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()
5. 添加相关系数
#Add correlation coefficients on the heatmap
ggplot(data = melted_cormat, aes(x=Var2, y=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)