ggcorr()提供了一种对相关矩阵进行重新排序的解决方案,并在相关图上显示显著性水平。它还包含一个用于计算相关p值矩阵的函数。
安装和加载
#Install
install.packages("ggcorrplot")
Or
if(!require(devtools)) install.packages("devtools")
devtools::install_github("kassambara/ggcorrplot")
#Loading
library(ggcorrplot)
Example
data(mtcars)
df<-mtcars
corr_1<-round(cor(df),2)
p<-ggcorrplot(corr_1)
p

# Compute a matrix of correlation p-values
p.mat <- cor_pmat(mtcars)
head(p.mat[, 1:4])
## mpg cyl disp hp
## mpg 0.000000e+00 6.112687e-10 9.380327e-10 1.787835e-07
## cyl 6.112687e-10 0.000000e+00 1.803002e-12 3.477861e-09
## disp 9.380327e-10 1.803002e-12 0.000000e+00 7.142679e-08
## hp 1.787835e-07 3.477861e-09 7.142679e-08 0.000000e+00
## drat 1.776240e-05 8.244636e-06 5.282022e-06 9.988772e-03
## wt 1.293959e-10 1.217567e-07 1.222311e-11 4.145827e-05
# method = "circle"
p<-ggcorrplot(corr_1,method = "circle")
p

#using hierarchical clustering
p<-ggcorrplot(corr_1, hc.order = TRUE, outline.col = "white") #hc.order使用hclust进行聚类,outline.col轮廓颜色
p

p<-ggcorrplot(corr_1, hc.order = TRUE, outline.col = "white",type="lower")
p

p<-ggcorrplot(corr_1, hc.order = TRUE, outline.col = "white",type="upper")
p

p<-ggcorrplot(corr_1, hc.order = TRUE, type = "lower",outline.col = "white",lab=T,lab_size = 2)
p

# Add correlation significance level
# --------------------------------
# Argument p.mat
# Barring the no significant coefficient
p<-ggcorrplot(corr_1, hc.order = TRUE, type = "lower",outline.col = "white",p.mat = p.mat,insig = "pch")
#insig=c("pch","blank")
p

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

参考:http://www.sthda.com/english/wiki/ggcorrplot-visualization-of-a-correlation-matrix-using-ggplot2
网友评论