R语言绘制相关性热图全总结
R语言绘制相关性热图全总结
引言
相关性热图是科研论文中一种常见的可视化手段,而在地学领域,我们常常需要分析一些环境因素之间的相关性,来判断环境生物因素中是否存在相关性情况。
尤其是在进行多变量分析时,分析目标因素和各变量之间的关系,往往需要首先考察变量之间的相关性,再考虑主成分等相关问题。地学环境生态领域常常用相关性热图的形式进行展示。总而言之,往大了说,任何表征相关性的数值都可以用相关性热图来进行绘制。
我们先来看看下面这张图,这是一篇发表在 ISME Journal ( IF = 11) 上的文章图,展示了生物和非生物因素的相关性。之间的相关性,其中红色的颜色代表负相关,蓝色代表正相关。每一格的数字代表相关系数。

Wijaya, W., Suhaimi, Z., Chua, C.X. et al. Frequent pulse disturbances shape resistance and resilience in tropical marine microbial communities. ISME COMMUN. 3, 55 (2023). https://doi.org/10.1038/s43705-023-00260-6
下图是一篇发表在 PLoS Medicine ( IF = 11.048) 上的文章图,展示了 22 种免疫细胞群体之间的相关性:

下图是发表在Scientific reports期刊上的一张相关性图,该图展示了微生物参数和土壤理化性质参数的Pearson相关性。

Ozlu, E., Sandhu, S.S., Kumar, S. et al. Soil health indicators impacted by long-term cattle manure and inorganic fertilizer application in a corn-soybean rotation of South Dakota. Sci Rep 9, 11776 (2019). https://doi.org/10.1038/s41598-019-48207-z
下图是发表在Science正刊上的一张相关性图,具有很高的颜值,同时是多个图表的组合。

Sunagawa, S., Coelho, L. P., Chaffron, S., Kultima, J. R., Labadie, K., Salazar, G., ... & Velayoudon, D. (2015). Structure and function of the global ocean microbiome. Science, 348(6237), 1261359.
其实这类相关性热图是使用R语言的ggcorrplot或corrplot包绘制的。能调整多种样式,你也能像顶刊一样绘制相关性结果。
ggcorrplot包
接下来介绍ggcorrplot包,进行一个简单的绘制,ggcorrplot包是2017年开发的,比较早,所以功能也比较简单。
首先安装ggcorrplot包
install.packages("ggcorrplot")
devtools::install_github("kassambara/ggcorrplot")
以mtcars内置数据为例:
# Compute a correlation matrix
data(mtcars)
corr 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
# Compute a matrix of correlation p-values
p.mat 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)

将样式改为圆形:
# method = "circle"
ggcorrplot(corr, method = "circle")

接下来使用hc.order关键字做层次聚类(hierarchical clustering)结果:
ggcorrplot(corr, hc.order = TRUE, outline.color = "white")

通过type=="lower"关键字来做下三角效果:
ggcorrplot(corr,
hc.order = TRUE,
type = "lower",
outline.color = "white")

同理利用type = "upper"关键字做上三角效果:
ggcorrplot(
corr,
hc.order = TRUE,
type = "upper",
outline.color = "white",
ggtheme = ggplot2::theme_gray,
colors = c("#6D9EC1", "white", "#E46726")
)

还可以标注显著性,通过lab = TRUE关键字:
ggcorrplot(corr,
hc.order = TRUE,
type = "lower",
lab = TRUE)

通过关键字手动标识不显著的地方,画叉号:
ggcorrplot(corr,
hc.order = TRUE,
type = "lower",
p.mat = p.mat)

corrplot包
通过上述的结果,我们能实现顶刊1和2的效果,但是如果对于更复杂的图绘3,需要借助最新的corrplot包。该包是2021年发行的,比较新,能适用于更复杂和美观的效果。
首先安装corrplot包
install.packages("corrplot")
以mtcars内置数据为例(上一节的数据示例):
data(mtcars)
corr Warning: Removed 8 rows containing missing values (geom_text).

参考
- https://github.com/ixxmu/mp_duty/issues/3521,相关性热图还能玩出什么新花样
- https://github.com/kassambara/ggcorrplot ggcorrplot开发者文档
-
https://github.com/Github-Yilei/ggcor ggcor开发者文档
本文由 mdnice 多平台发布