跟着Nature Communications学作图:R语言ggcorrplot包做相关性热图展示环境变量之间的相关性

2023-11-24 17:20:38 浏览数 (1)

论文

Genomic insights into local adaptation and future climate-induced vulnerability of a keystone forest tree in East Asia

论文中提供的代码链接

https://github.com/jingwanglab/Populus_genomic_prediction_climate_vulnerability/blob/main/4-Local_adaptation/3cor_plot.R

https://www.nature.com/articles/s41467-022-34206-8#Sec22

论文中提供的数据在 Source data 部分获取

环境变量的相关性对应的论文中的 Supplementary Fig. 9. a

论文中提供的环境数据的部分截图

image.png

读取数据

代码语言:javascript复制
library(tidyverse)
raw_data<-read_csv("data/20231110/Source data/Fig2c&d/environment.csv") %>% 
  select(-c(1,2,3))

计算相关性

代码语言:javascript复制
corrmatrix <- cor(raw_data, method = "spearman")
corrmatrix

相关性检验

代码语言:javascript复制
res1 <-corrplot::cor.mtest(corrmatrix, conf.level= .95)

res1$p
res1$lowCI
res1$uppCI

论文中提供的作图代码

代码语言:javascript复制
col3 <- grDevices::colorRampPalette(c("#2082DD", "white", "#FF3F3F")) 
col3

corrplot::corrplot.mixed(corr=corrmatrix,
                         lower="number",
                         upper="circle",
                         diag="u",
                         upper.col =col3(20),
               lower.col = col3(20),
               number.cex=0.9,
               p.mat= res1$p,
               sig.level= 0.05,
               bg = "white",
               is.corr = TRUE, 
               outline = FALSE, 
               mar = c(0,0,3,0),
               addCoef.col = NULL, 
               addCoefasPercent = FALSE, 
               order = c("original"),
               rect.col = "black", 
               rect.lwd = 1, 
               tl.cex = 1.2,
               tl.col = "black", 
               tl.offset = 0.4, 
               tl.srt = 90,
               cl.cex = 1.1, 
               cl.ratio = 0.2,
               tl.pos="lt",
               cl.offset = 0.5 )
title(main="Correlation coefficient of 19 environmental variables",cex.main=2.1)

出图效果

image.png

这个有好多参数,参数具体都有什么作用后面如果用到了再来研究吧

ggcorrplot作图

这个是ggplot2系列,修改细节可能会比较方便

安装

代码语言:javascript复制
install.packages("ggcorrplot")

作图代码

代码语言:javascript复制
library(ggcorrplot)
ggcorrplot(corr = corrmatrix,
           hc.order = TRUE,
           method = "circle",
           type="upper",
           lab = TRUE) 
  theme(axis.text.x = element_blank(),
        panel.grid = element_blank(),
        legend.position = c(0.8,0.3))

组合图

代码语言:javascript复制
library(ggcorrplot)
p1<-ggcorrplot(corr = corrmatrix,
           hc.order = TRUE,
           method = "circle",
           type="upper",
           lab = TRUE) 
  theme(axis.text.x = element_blank(),
        panel.grid = element_blank(),
        legend.position = c(0.8,0.3))

p1
p2<-ggcorrplot(corr = corrmatrix) 
  scale_fill_viridis_c()

library(patchwork)

p1 p2

image.png

0 人点赞