分享

相关矩阵的ggplot2版本,方便拼图

 阿越就是我 2023-10-12 发布于上海
     Try to learn everything about something! 

ggcorrplot用来可视化相关矩阵,和之前介绍过的corrplot功能完全一样!可以说就是一个ggplot2版本的corrplot

corrplot包可视化相关矩阵详解

这个包的作者也是这个黑人小哥Alboukadel Kassambara,你可能不认识他,但他的很多包都很流行!比如ggpubrrstatix,survminer,factoextra,这些都是这个小哥的杰作!而且这个小哥还专门运营了一个网站,分享各种教程!http://www./

继续介绍ggcorrplot

  • 安装

  • 使用

    • 相关系数矩阵

    • P值矩阵

  • 拼图

安装

# 2选1
install.packages("ggcorrplot")

if(!require(devtools)) install.packages("devtools")
devtools::install_github("kassambara/ggcorrplot")

使用

相关系数矩阵

# 相关系数矩阵
data(mtcars)
corr <- round(cor(mtcars), 1)
head(corr[, 1:6])
##       mpg  cyl disp   hp drat   wt
## mpg   1.0 -0.9 -0.8 -0.8  0.7 -0.9
## cyl  -0.9  1.0  0.9  0.8 -0.7  0.8
## disp -0.8  0.9  1.0  0.8 -0.7  0.9
## hp   -0.8  0.8  0.8  1.0 -0.4  0.7
## drat  0.7 -0.7 -0.7 -0.4  1.0 -0.7
## wt   -0.9  0.8  0.9  0.7 -0.7  1.0
# p值矩阵
library(ggcorrplot)
## Warning: 程辑包'ggcorrplot'是用R版本4.1.3 来建造的
## 载入需要的程辑包:ggplot2
p.mat <- cor_pmat(mtcars)
head(p.mat[, 1:4])
##               mpg          cyl         disp           hp
## mpg  0.000000e+00 6.112687e-10 9.380327e-10 1.787835e-07
## cyl  6.112687e-10 0.000000e+00 1.802838e-12 3.477861e-09
## disp 9.380327e-10 1.802838e-12 0.000000e+00 7.142679e-08
## hp   1.787835e-07 3.477861e-09 7.142679e-08 0.000000e+00
## drat 1.776240e-05 8.244636e-06 5.282022e-06 9.988772e-03
## wt   1.293959e-10 1.217567e-07 1.222320e-11 4.145827e-05

基本图形:

ggcorrplot(corr)
plot of chunk unnamed-chunk-4

或者使用圆形,只支持这两种类型。

ggcorrplot(corr,method = "circle",type = "lower")
## Warning: `guides(<scale> = FALSE)` is deprecated. Please use `guides(<scale> =
## "none")` instead.
plot of chunk unnamed-chunk-5

边框色:

ggcorrplot(corr, type = "upper",outline.color = "black")
plot of chunk unnamed-chunk-6

聚类:

ggcorrplot(corr, hc.order = TRUE, type = "lower",outline.col = "white")
plot of chunk unnamed-chunk-7

改变主题和颜色:

ggcorrplot(corr, hc.order = TRUE, type = "lower",
   outline.col = "white",
   ggtheme = ggplot2::theme_gray,
   colors = c("#6D9EC1""white""#E46726"))
plot of chunk unnamed-chunk-8

添加相关系数:

ggcorrplot(corr, hc.order = TRUE,type = "lower",lab=T)
plot of chunk unnamed-chunk-9

P值矩阵

默认不显著的会显示×

p1 <- ggcorrplot(corr, hc.order = TRUE,type = "lower", p.mat = p.mat)
p1
plot of chunk unnamed-chunk-10

不显著的不显示:

ggcorrplot(corr, p.mat = p.mat, hc.order = TRUE,type = "lower", insig = "blank")
plot of chunk unnamed-chunk-11

拼图

使用ggplot2版本的一个好处就是可以随便拼图。

p2 <- ggplot(mtcars,aes(disp,mpg))+geom_point(aes(color=factor(cyl)))+theme_minimal()
library(patchwork)

p1 + p2
plot of chunk unnamed-chunk-13

以上就是今天的内容,希望对你有帮助哦!欢迎点赞、在看、关注、转发

    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章