在R中可视化相关矩阵(correlation matrix)的最简单方法是使用corrplot包。另一种方法是在ggally包中使用函数ggcorr()。 但是,ggally包不提供用于重新排序相关矩阵或显示显著水平的选项。
接下来,我们将使用R包ggcorrplot可视化相关矩阵。
ggcorrplot的主要特征
ggcorrplot具有重新排序相关矩阵以及在热图上显示显著性水平的功能。此外,它还包括用于计算相关性p值的矩阵的功能。
ggcorrplot(): 使用ggplot2相关矩阵可视化。
cor_pmat(): 计算相关性的p值。
ggcorrplot下载与加载
#CRAN
install.packages("ggcorrplot")
#GitHub
if(!require(devtools)) install.packages("devtools")
devtools::install_github("kassambara/ggcorrplot")
library(ggcorrplot)
使用
计算相关矩阵
使用R自带数据集mtcars进行接下来的分析。ggcorlplot函数cor_pmat()用于计算相关性的p值矩阵。
# 相关性矩阵计算
library(ggcorrplot)
data(mtcars)
corr <- round(cor(mtcars), 1) #格式设置,仅保留1位小数
head(corr[, 1:6])

# 计算相关性的P值矩阵
p.mat <- cor_pmat(mtcars)
head(p.mat[, 1:4])

相关矩阵可视化
#可视化相关矩阵
#----------------------------------------
#默认作图,method = "square"
ggcorrplot(corr)

# 调整矩形热图为圆形,method = "circle"
ggcorrplot(corr, method = "circle")

#重新排序相关矩阵
#----------------------------------------
#使用分层群集(hierarchical clustering)
ggcorrplot(corr, hc.order = TRUE, outline.col = "white") #方形或圆圈的轮廓颜色。 默认值为“灰色”。

#类型的相关图布局
#----------------------------------------
#获取下三角形
ggcorrplot(corr, hc.order = TRUE, type = "lower",
outline.col = "white")

#上三角形
ggcorrplot(corr, hc.order = TRUE, type = "upper",
outline.col = "white")

#更改颜色和主题
#----------------------------------------
#参数:
ggcorrplot(corr, hc.order = TRUE, type = "lower",
outline.col = "white",
ggtheme = ggplot2::theme_void,
colors = c("#6D9EC1", "white", "#E46726")) #ggtheme:主题设置

更多颜色搭配可以借助一些配色网站 ,如coolors。
#添加相关系数
#----------------------------------------
#参数 lab = true
ggcorrplot(corr, hc.order = TRUE, type = "lower",
lab = TRUE)

#添加相关性显著水平
#----------------------------------------
#参数 p.mat.
#默认叉掉不显著的系数
ggcorrplot(corr, hc.order = TRUE,
type = "lower", p.mat = p.mat)

#留空不显著的系数
ggcorrplot(corr, p.mat = p.mat, hc.order = TRUE,
type = "lower", insig = "blank")

美化
行列一致
即同一个文件内的指标,或两个文件的指标数目一致分析,是一个i*j(i=j)的矩阵;
install.packages("ggcorrplot")
install.packages("ggthemes")
library(ggcorrplot)
library(ggthemes)
data<-mtcars # mtcars数据集是美国Motor Trend收集的1973到1974年期间总共32辆汽车的11个指标: 油耗及10个与设计及性能方面的指标。
dim(data) #文件维度
#计算它们的相关性系数,还需要计算体现其显著性的 P 值。
corr <- round(cor(mtcars), 1)
head(corr[, 1:6])
p.mat <- cor_pmat(mtcars)
head(p.mat[, 1:6])
corr1<- corr[, 1:6]
作图:
ggcorrplot(corr, method = c("square"), type = c("full"), ggtheme = ggplot2::theme_void, title = " ", show.legend = TRUE, legend.title = "Corr_r2", show.diag = T,
colors = c("#839EDB", "white", "#FF8D8D"), outline.color = "white",
hc.order = T, hc.method = "single", lab = F, lab_col = "black",
lab_size = 2, p.mat = NULL, sig.level = 0.05, insig = c("pch"), pch = 4, pch.col = "white", pch.cex = 4.5, tl.cex = 12,
tl.col = "black", tl.srt = 45, digits = 2)

ggcorrplot(corr, method = "square", type = "upper", ggtheme = ggplot2::theme_void, title = "",
show.legend = TRUE, legend.title = "Corr", show.diag = T,
colors = c("#839EDB", "white", "#FF8D8D"), outline.color = "white",
hc.order = T, hc.method = "single", lab = F, lab_col = "black",
lab_size = 3, p.mat = p.mat, sig.level = 0.05, insig = c("pch"), pch = 22, pch.col = "white", pch.cex = 4, tl.cex = 12,
tl.col = "black", tl.srt = 0, digits = 2)

上图中需要注意的是:格子中含有小方框的格子表示该相关性不显著(0.05),且格子中小方框颜色表示p value 大小,可修改参数为:pch = 22。
ggcorrplot(corr, method = "circle", type = "full", ggtheme = ggplot2::theme_void, title = "",
show.legend = TRUE, legend.title = "Corr", show.diag = F,
colors = c("#839EDB", "white", "#FF8D8D"), outline.color = "white",
hc.order = T, hc.method = "complete", lab = FALSE, lab_col = "black",
lab_size = 4, p.mat = NULL, sig.level = 0.05, insig = c("pch", "blank"), pch = 4, pch.col = "black", pch.cex = 5, tl.cex = 12,
tl.col = "black", tl.srt = 45, digits = 2)

ggcorrplot(corr, method = "circle", type = "upper", ggtheme = ggplot2::theme_bw(), title = "",
show.legend = TRUE, legend.title = "Corr", show.diag = T,
colors = c("#839EDB", "white", "#FF8D8D"), outline.color = "white",
hc.order = T, hc.method = "complete", lab = T, lab_col = "black",
lab_size = 2, p.mat = p.mat, sig.level = 0.05, insig = "blank", pch = 4, pch.col = "black", pch.cex = 5, tl.cex = 12,
tl.col = "black", tl.srt = 45, digits = 2)

美化:行列不一致
行列不一致,在这里借助psych包来计算相关性和p value。
library(ggcorrplot)
library(ggthemes)
library(psych)
data<-mtcars
data1 <- data[c(1:5)]
data2 <- data[c(6:11)] #刻意截取不一致
cor <- corr.test(data1,data2,method = "spearman",adjust = "BH",ci = F)
cmt<-cor$r
pmt<-cor$p.adj
ggcorrplot(cmt,method = "circle",outline.color = "white",
ggtheme = theme_bw(),colors = c("#839EDB", "white", "#FF8D8D"),lab = T,lab_size=2,
p.mat=pmt,insig="pch",pch.col = "red", pch.cex = 3, tl.cex = 12)

ggcorrplot(cmt,method = "circle",outline.color = "white",
ggtheme = theme_bw(),colors = c("#839EDB", "white", "#FF8D8D"),lab = T,lab_size=2,
p.mat = pmt, insig= "blank", pch.col = "red", pch.cex = 3, tl.cex = 12)

网友评论