安装和加载
install.packages("ggcorrplot") library(ggcorrplot)
计算相关性矩阵
#以内置的mtcars数据集为例 data(mtcars) #数据集格式如下
#cor()函数可以非常方便快捷的计算出连续变量之间的相关系数、 corr <- round(cor(mtcars), 1) head(corr[, 1:6])
> head(corr[, 1:6]) mpg cyl disp hp drat wt mpg 1.0 -0.9 -0.8 -0.8 0.7 -0.9 cyl -0.9 1.0 0.9 0.8 -0.7 0.8 disp -0.8 0.9 1.0 0.8 -0.7 0.9 hp -0.8 0.8 0.8 1.0 -0.4 0.7 drat 0.7 -0.7 -0.7 -0.4 1.0 -0.7 wt -0.9 0.8 0.9 0.7 -0.7 1.0
#用ggcorrplot包提供的函数cor_pmat()计算p值 p.mat <- cor_pmat(mtcars) head(p.mat[, 1:4])
> head(p.mat[, 1:4]) mpg cyl mpg 0.000000e+00 6.112687e-10 cyl 6.112687e-10 0.000000e+00 disp 9.380327e-10 1.802838e-12 hp 1.787835e-07 3.477861e-09 drat 1.776240e-05 8.244636e-06 wt 1.293959e-10 1.217567e-07 disp hp mpg 9.380327e-10 1.787835e-07 cyl 1.802838e-12 3.477861e-09 disp 0.000000e+00 7.142679e-08 hp 7.142679e-08 0.000000e+00 drat 5.282022e-06 9.988772e-03 wt 1.222320e-11 4.145827e-05
可视化相关性矩阵
ggcorrplot(corr)#method默认为square,即方形
# method = "circle" 圆形 ggcorrplot(corr, method = "circle")
#重排矩阵,使用分层聚类 ggcorrplot(corr, hc.order = TRUE, outline.color = "white")
#展示下半三角 ggcorrplot(corr, hc.order = TRUE, type = "lower", outline.color = "white")
#展示上半三角 ggcorrplot(corr, hc.order = TRUE, type = "upper", outline.color = "white")
#改变主题和颜色 ggcorrplot( corr, hc.order = TRUE, type = "lower", outline.color = "white", ggtheme = ggplot2::theme_gray, colors = c("#6D9EC1", "white", "#E46726") )
#加上相关性系数标签 ggcorrplot(corr, hc.order = TRUE, type = "lower", lab = TRUE)
#给有显著性差异的标上记 ggcorrplot(corr, hc.order = TRUE, type = "lower", p.mat = p.mat)
#在没有显著系数的地方留空 ggcorrplot( corr, p.mat = p.mat, hc.order = TRUE, type = "lower", insig = "blank" )
参考
https://rpkgs.datanovia.com/ggcorrplot/