ggcorrplot | 简单的相关性热图绘制

在这里插入图片描述

ggcorrplot的基础用法指南

ggcorrplot

ggcorrplot提供了一种对相关矩阵进行重新排序的解决方案,并在相关图上显示显著性水平。它还包括一个用于计算相关p值矩阵的函数。

安装和加载

install.packages("ggcorrplot")
library(ggcorrplot)

计算相关性矩阵

#以内置的mtcars数据集为例
data(mtcars)
#数据集格式如下

mtcars

#cor()函数可以非常方便快捷的计算出连续变量之间的相关系数、
corr <- round(cor(mtcars), 1)
head(corr[, 1:6])
> 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
#用ggcorrplot包提供的函数cor_pmat()计算p值
p.mat <- cor_pmat(mtcars)
head(p.mat[, 1:4])
> head(p.mat[, 1:4])
              mpg          cyl
mpg  0.000000e+00 6.112687e-10
cyl  6.112687e-10 0.000000e+00
disp 9.380327e-10 1.802838e-12
hp   1.787835e-07 3.477861e-09
drat 1.776240e-05 8.244636e-06
wt   1.293959e-10 1.217567e-07
             disp           hp
mpg  9.380327e-10 1.787835e-07
cyl  1.802838e-12 3.477861e-09
disp 0.000000e+00 7.142679e-08
hp   7.142679e-08 0.000000e+00
drat 5.282022e-06 9.988772e-03
wt   1.222320e-11 4.145827e-05

可视化相关性矩阵

ggcorrplot(corr)#method默认为square,即方形

cor_square

# method = "circle" 圆形
ggcorrplot(corr, method = "circle")

cor_cicrl

#重排矩阵,使用分层聚类
ggcorrplot(corr, hc.order = TRUE, outline.color = "white")

cor_hc

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

cor_low

#展示上半三角
ggcorrplot(corr,
           hc.order = TRUE,
           type = "upper",
           outline.color = "white")

cor_upper

#改变主题和颜色
ggcorrplot(
  corr,
  hc.order = TRUE,
  type = "lower",
  outline.color = "white",
  ggtheme = ggplot2::theme_gray,
  colors = c("#6D9EC1", "white", "#E46726")
)

cor_color

#加上相关性系数标签
ggcorrplot(corr,
           hc.order = TRUE,
           type = "lower",
           lab = TRUE)

cor_lab

#给有显著性差异的标上记
ggcorrplot(corr,
           hc.order = TRUE,
           type = "lower",
           p.mat = p.mat)

cor_pmat

#在没有显著系数的地方留空
ggcorrplot(
  corr,
  p.mat = p.mat,
  hc.order = TRUE,
  type = "lower",
  insig = "blank"
)

cor_pmat_blank

参考

https://rpkgs.datanovia.com/ggcorrplot/


往期:

ggpubr|让数据可视化更加优雅

ggsci | 让你的配色Nature化

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值