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

在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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,739評(píng)論 6 534
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 98,634評(píng)論 3 419
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,653評(píng)論 0 377
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我,道長(zhǎng),這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,063評(píng)論 1 314
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 71,835評(píng)論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 55,235評(píng)論 1 324
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,315評(píng)論 3 442
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,459評(píng)論 0 289
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 49,000評(píng)論 1 335
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 40,819評(píng)論 3 355
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 43,004評(píng)論 1 370
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,560評(píng)論 5 362
  • 正文 年R本政府宣布,位于F島的核電站,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 44,257評(píng)論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,676評(píng)論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,937評(píng)論 1 288
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 51,717評(píng)論 3 393
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 48,003評(píng)論 2 374

推薦閱讀更多精彩內(nèi)容