R語言可視化學習筆記之相關矩陣可視化包ggcorrplot


基于ggplot2包以及corrplot包的相關矩陣可視化包ggcorrplotggcorrplot包提供對相關矩陣重排序以及在相關圖中展示顯著性水平的方法,同時也能計算相關性p-value
<a id="more"></a>

計算相關矩陣(cor()計算結果不提供p-value)

library(ggcorrplot)
data("mtcars")
corr <- round(cor(mtcars), 1)
head(corr[, 1:6])

用ggcorrplot包提供的函數cor_pmat()

p.mat <- cor_pmat(mtcars)

head(p.mat[, 1:4])

可視化相關性矩陣

ggcorrplot(corr)#method默認為square

方法為circle

ggcorrplot(corr, method = "circle")

重排矩陣,使用分等級聚類

ggcorrplot(corr, hc.order = TRUE, outline.color = "white")

控制矩陣形狀

ggcorrplot(corr, hc.order = TRUE, type = "lower", outline.color = "white")#下三角形

上三角形

ggcorrplot(corr, hc.order = TRUE, type = "upper", outline.color = "white")

更改顏色以及主題

ggcorrplot(corr, hc.order = TRUE, type = "lower", outline.color = "white", 

ggtheme = ggplot2::theme_gray, colors = c("#6D9EC1", "white", "#E46726"))

添加相關系數

ggcorrplot(corr, hc.order = TRUE, type = "lower", lab = TRUE)

增加顯著性水平,不顯著的話就不添加了

ggcorrplot(corr, hc.order = TRUE, type = "lower", p.mat = p.mat)

將不顯著的色塊設置成空白

ggcorrplot(corr, p.mat = p.mat, hc.order=TRUE, type = "lower", insig = "blank")

轉載https://ytlogos.github.io/2017/06/25/R%E8%AF%AD%E8%A8%80%E5%8F%AF%E8%A7%86%E5%8C%96%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0%E4%B9%8B%E7%9B%B8%E5%85%B3%E7%9F%A9%E9%98%B5%E5%8F%AF%E8%A7%86%E5%8C%96%E5%8C%85ggcorrplot/

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容