R packages|ggcorrplot : 相關矩陣的可視化(熱圖)

在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])
mtcars
# 計算相關性的P值矩陣
p.mat <- cor_pmat(mtcars)
head(p.mat[, 1:4])
p.mat

相關矩陣可視化

#可視化相關矩陣
#----------------------------------------
#默認作圖,method = "square"
ggcorrplot(corr)
默認作圖.png
# 調整矩形熱圖為圓形,method = "circle"
ggcorrplot(corr, method = "circle")
調整矩形熱圖為圓形.png
#重新排序相關矩陣
#----------------------------------------
#使用分層群集(hierarchical clustering)
ggcorrplot(corr, hc.order = TRUE, outline.col = "white") #方形或圓圈的輪廓顏色。 默認值為“灰色”。
分層群集.png
#類型的相關圖布局
#----------------------------------------
#獲取下三角形
ggcorrplot(corr, hc.order = TRUE, type = "lower",
           outline.col = "white")
下三角形.png
#上三角形
ggcorrplot(corr, hc.order = TRUE, type = "upper",
           outline.col = "white")
上三角形.png
#更改顏色和主題
#----------------------------------------
#參數:
ggcorrplot(corr, hc.order = TRUE, type = "lower",
           outline.col = "white",
           ggtheme = ggplot2::theme_void,
           colors = c("#6D9EC1", "white", "#E46726")) #ggtheme:主題設置

更改顏色和主題.png

更多顏色搭配可以借助一些配色網站 ,如coolors

#添加相關系數
#----------------------------------------
#參數 lab = true
ggcorrplot(corr, hc.order = TRUE, type = "lower",
           lab = TRUE)
添加相關系數.png
#添加相關性顯著水平
#----------------------------------------
#參數 p.mat.
#默認叉掉不顯著的系數
ggcorrplot(corr, hc.order = TRUE,
           type = "lower", p.mat = p.mat)
添加相關性顯著水平.png
#留空不顯著的系數
ggcorrplot(corr, p.mat = p.mat, hc.order = TRUE,
           type = "lower", insig = "blank")
留空不顯著的系數.png

美化

行列一致

即同一個文件內的指標,或兩個文件的指標數目一致分析,是一個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)
圖片.png
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容