感谢关注天善智能,走好数据之路↑↑↑
欢迎关注天善智能,我们是专注于商业智能BI,人工智能AI,大数据分析与挖掘领域的垂直社区,学习,问答、求职一站式搞定!
对商业智能BI、大数据分析挖掘、机器学习,python,R等数据领域感兴趣的同学加微信:tstoutiao,邀请你进入数据爱好者交流群,数据爱好者们都在这儿。
作者简介:
taoyan:伪码农,R语言爱好者,爱开源。
个人博客: https://ytlogos.github.io/
基于ggplot2包以及corrplot包的相关矩阵可视化包ggcorrplot,ggcorrplot包提供对相关矩阵重排序以及在相关图中展示显著性水平的方法,同时也能计算相关性p-value
安装方法就不提了,不懂的可以浏览以前的文章(链接:https://www.sohu.com/a/219810280_572440)
library(ggcorrplot)
#计算相关矩阵(cor()计算结果不提供p-value)
data("mtcars")
corr <- round(cor(mtcars), 1)
head(corr[, 1:6])
![](https://img.haomeiwen.com/i3901436/786cf873b05662bc.jpg)
#用ggcorrplot包提供的函数cor_pmat()
p.mat <- cor_pmat(mtcars)
head(p.mat[, 1:4])
![](https://img.haomeiwen.com/i3901436/169d2f4bb48e46d0.jpg)
可视化相关性矩阵
ggcorrplot(corr)#method默认为square
![](https://img.haomeiwen.com/i3901436/537e36343887cb96.jpg)
#方法为circle
ggcorrplot(corr, method = "circle")
![](https://img.haomeiwen.com/i3901436/1e8f3f28db95437b.jpg)
#重排矩阵,使用分等级聚类
ggcorrplot(corr, hc.order = TRUE, outline.color = "white")
![](https://img.haomeiwen.com/i3901436/ba0e78890feb542a.jpg)
#控制矩阵形状
ggcorrplot(corr, hc.order = TRUE, type = "lower", outline.color = "white")#下三角形
![](https://img.haomeiwen.com/i3901436/75bc984bf78dc055.jpg)
#上三角形
ggcorrplot(corr, hc.order = TRUE, type = "upper", outline.color = "white")
![](https://img.haomeiwen.com/i3901436/225b1874f9e6fc63.jpg)
#更改颜色以及主题
ggcorrplot(corr, hc.order = TRUE, type = "lower", outline.color = "white",
ggtheme = ggplot2::theme_gray, colors = c("#6D9EC1", "white", "#E46726"))
![](https://img.haomeiwen.com/i3901436/8f7619c832bee481.jpg)
#添加相关系数
ggcorrplot(corr, hc.order = TRUE, type = "lower", lab = TRUE)
![](https://img.haomeiwen.com/i3901436/727820eb2764e671.jpg)
#增加显著性水平,不显著的话就不添加了
ggcorrplot(corr, hc.order = TRUE, type = "lower", p.mat = p.mat)
![](https://img.haomeiwen.com/i3901436/f1205275163ea746.jpg)
#将不显著的色块设置成空白
ggcorrplot(corr, p.mat = p.mat, hc.order=TRUE, type = "lower", insig = "blank")
![](https://img.haomeiwen.com/i3901436/4ecb86f3ec4c9d09.jpg)
网友评论