在R中可視化相關(guān)矩陣(correlation matrix)的最簡(jiǎn)單方法是使用corrplot包。另一種方法是在ggally包中使用函數(shù)ggcorr()。 但是,ggally包不提供用于重新排序相關(guān)矩陣或顯示顯著水平的選項(xiàng)。
接下來,我們將使用R包ggcorrplot可視化相關(guān)矩陣。
ggcorrplot的主要特征
ggcorrplot具有重新排序相關(guān)矩陣以及在熱圖上顯示顯著性水平的功能。此外,它還包括用于計(jì)算相關(guān)性p值的矩陣的功能。
ggcorrplot(): 使用ggplot2相關(guān)矩陣可視化。
cor_pmat(): 計(jì)算相關(guān)性的p值。
ggcorrplot下載與加載
#CRAN
install.packages("ggcorrplot")
#GitHub
if(!require(devtools)) install.packages("devtools")
devtools::install_github("kassambara/ggcorrplot")
library(ggcorrplot)
使用
計(jì)算相關(guān)矩陣
使用R自帶數(shù)據(jù)集mtcars進(jìn)行接下來的分析。ggcorlplot函數(shù)cor_pmat()用于計(jì)算相關(guān)性的p值矩陣。
# 相關(guān)性矩陣計(jì)算
library(ggcorrplot)
data(mtcars)
corr <- round(cor(mtcars), 1) #格式設(shè)置,僅保留1位小數(shù)
head(corr[, 1:6])
mtcars
# 計(jì)算相關(guān)性的P值矩陣
p.mat <- cor_pmat(mtcars)
head(p.mat[, 1:4])
p.mat
相關(guān)矩陣可視化
#可視化相關(guān)矩陣
#----------------------------------------
#默認(rèn)作圖,method = "square"
ggcorrplot(corr)
默認(rèn)作圖.png
# 調(diào)整矩形熱圖為圓形,method = "circle"
ggcorrplot(corr, method = "circle")
調(diào)整矩形熱圖為圓形.png
#重新排序相關(guān)矩陣
#----------------------------------------
#使用分層群集(hierarchical clustering)
ggcorrplot(corr, hc.order = TRUE, outline.col = "white") #方形或圓圈的輪廓顏色。 默認(rèn)值為“灰色”。
分層群集.png
#類型的相關(guān)圖布局
#----------------------------------------
#獲取下三角形
ggcorrplot(corr, hc.order = TRUE, type = "lower",
outline.col = "white")
下三角形.png
#上三角形
ggcorrplot(corr, hc.order = TRUE, type = "upper",
outline.col = "white")
上三角形.png
#更改顏色和主題
#----------------------------------------
#參數(shù):
ggcorrplot(corr, hc.order = TRUE, type = "lower",
outline.col = "white",
ggtheme = ggplot2::theme_void,
colors = c("#6D9EC1", "white", "#E46726")) #ggtheme:主題設(shè)置
更改顏色和主題.png
更多顏色搭配可以借助一些配色網(wǎng)站 ,如coolors。
#添加相關(guān)系數(shù)
#----------------------------------------
#參數(shù) lab = true
ggcorrplot(corr, hc.order = TRUE, type = "lower",
lab = TRUE)
添加相關(guān)系數(shù).png
#添加相關(guān)性顯著水平
#----------------------------------------
#參數(shù) p.mat.
#默認(rèn)叉掉不顯著的系數(shù)
ggcorrplot(corr, hc.order = TRUE,
type = "lower", p.mat = p.mat)
添加相關(guān)性顯著水平.png
#留空不顯著的系數(shù)
ggcorrplot(corr, p.mat = p.mat, hc.order = TRUE,
type = "lower", insig = "blank")
留空不顯著的系數(shù).png
美化
行列一致
即同一個(gè)文件內(nèi)的指標(biāo),或兩個(gè)文件的指標(biāo)數(shù)目一致分析,是一個(gè)i*j(i=j)的矩陣;
install.packages("ggcorrplot")
install.packages("ggthemes")
library(ggcorrplot)
library(ggthemes)
data<-mtcars # mtcars數(shù)據(jù)集是美國(guó)Motor Trend收集的1973到1974年期間總共32輛汽車的11個(gè)指標(biāo): 油耗及10個(gè)與設(shè)計(jì)及性能方面的指標(biāo)。
dim(data) #文件維度
#計(jì)算它們的相關(guān)性系數(shù),還需要計(jì)算體現(xiàn)其顯著性的 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)
上圖中需要注意的是:格子中含有小方框的格子表示該相關(guān)性不顯著(0.05),且格子中小方框顏色表示p value 大小,可修改參數(shù)為: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包來計(jì)算相關(guān)性和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