R包corrplot提供了一个关于相关矩阵的可视化探索工具,支持自动变量重新排序,以帮助检测变量之间的隐藏模式。
corrplot 非常易于使用,并在可视化方法、图形布局、颜色、图例、文本标签等方面提供了丰富的绘图选项。它还提供 p 值和置信区间,以帮助用户确定相关性的统计显着性。
corrplot()
有大约 50 个参数,但最常见的参数只有几个。在大多数场景中,我们只需要一行代码就可以得到一个相关矩阵图。
最常用的参数有method
, type
, order
, diag
等。
methodcorrplot 包中有七种可视化方法(参数), 分别是 circle, square, ellipse, number, shade, color, pie。默认颜色设置下,字形的颜色强度与相关系数成正比。
circle
/square
,圆形或正方形的面积表示相应相关系数的绝对值。ellipse
,椭圆的偏心率参数化地缩放到相关值。number
, 不同颜色的系数数字。color
,大小相同的正方形,颜色不同。shade
, 类似于color
, 但负系数字形带有阴影。pie
,圆圈顺时针填充正值,逆时针填充负值。
corrplot.mixed()
是一个混合可视化风格的封装函数,可以分别设置下三角和上三角的可视化方式。
共有三种布局类型(参数type
)full
:upper
和'lower
。
相关矩阵可以根据相关矩阵系数重新排序。这对于识别矩阵中的隐藏结构和模式很重要。
library(corrplot)
#图1
M = cor(mtcars)
corrplot(M, method = 'number') # colorful number
![corrplot包绘制相关系数图插图 corrplot包绘制相关系数图插图](http://www.gongju128.cn/wp-content/uploads/2022/07/图1.png)
#图2
corrplot(M, method = 'color', order = 'alphabet')
![corrplot包绘制相关系数图插图1 corrplot包绘制相关系数图插图1](http://www.gongju128.cn/wp-content/uploads/2022/07/图2.png)
#图3
corrplot(M) # by default, method = 'circle'
![corrplot包绘制相关系数图插图2 corrplot包绘制相关系数图插图2](http://www.gongju128.cn/wp-content/uploads/2022/07/图3.png)
#图4
corrplot(M, order = 'AOE') # AOE重新排序以后
![corrplot包绘制相关系数图插图3 corrplot包绘制相关系数图插图3](http://www.gongju128.cn/wp-content/uploads/2022/07/图4.png)
#图5
corrplot(M, method = 'shade', order = 'AOE', diag = FALSE)
![corrplot包绘制相关系数图插图4 corrplot包绘制相关系数图插图4](http://www.gongju128.cn/wp-content/uploads/2022/07/图5.png)
#图6
corrplot(M, method = 'square', order = 'FPC', type = 'lower', diag = FALSE)
![corrplot包绘制相关系数图插图5 corrplot包绘制相关系数图插图5](http://www.gongju128.cn/wp-content/uploads/2022/07/图6.png)
#图7
corrplot(M, method = 'ellipse', order = 'AOE', type = 'upper')
![corrplot包绘制相关系数图插图6 corrplot包绘制相关系数图插图6](http://www.gongju128.cn/wp-content/uploads/2022/07/图7.png)
#图8
corrplot.mixed(M, order = 'AOE')
![corrplot包绘制相关系数图插图7 corrplot包绘制相关系数图插图7](http://www.gongju128.cn/wp-content/uploads/2022/07/图8.png)
#图9
corrplot.mixed(M, lower = 'shade', upper = 'pie', order = 'hclust')
![corrplot包绘制相关系数图插图8 corrplot包绘制相关系数图插图8](http://www.gongju128.cn/wp-content/uploads/2022/07/图9.png)
ggpolot2当然也可以绘制这些图,但是显然没有corrplot2简单,基本上只需要一行代码,就可以得到很精美的相关系数热力图。
大家可以试试。
以上来自corrplot的官方网站:https://cran.r-project.org/web/packages/corrplot/vignettes/corrplot-intro.html