[Microbiome图表复现] 常规热图添加一些新元素

2023-11-30 14:34:57 浏览数 (2)

论文原文

图形解读

❝作为常见的相关性热图,此图在细节方面做了调整,用星代替以往的点来表示R值,同时通过透明度来调节梯度,线条也根据P值大小做了调节。 ❞

加载R包

代码语言:javascript复制
library(tidyverse)
library(linkET)
library(psych)
library(reshape2)
library(magrittr)
library(ggtext)
library(ggstar)
library(ggnewscale)

导入数据

代码语言:javascript复制
env <- read_tsv("env.tsv")
spe <- read_tsv("spe.tsv")

数据清洗

代码语言:javascript复制
pp <- corr.test(spe,env,method="pearson",adjust = "fdr")
cor <- pp$r
pvalue <- pp$p
代码语言:javascript复制
df <- melt(cor) %>% mutate(pvalue=melt(pvalue)[,3],
                           p_signif=symnum(pvalue, corr = FALSE, na = FALSE,  
                                           cutpoints = c(0, 0.001, 0.01, 0.05, 0.1, 1), 
                                           symbols = c("***", "**", "*", "", " "))) %>% 
  set_colnames(c("env","genus","r","p","p_signif"))
代码语言:javascript复制
cordata <- df %>% 
  filter(genus %in% c("N","K","Ca")) %>% 
  select(2,1,3,4,5) %>% 
  mutate(rd = cut(r, breaks = c(-Inf, 0, 0.4, Inf),
                  labels = c("< 0", "0 - 0.4", ">= 0.4")),
         pd = cut(p, breaks = c(-Inf, 0.05, Inf),
                  labels = c("< 0.05",">= 0.05")))

数据可视化

代码语言:javascript复制
qcorrplot(correlate(spe,method = "pearson"),diag=F,type="lower") 
  geom_star(aes(size=r,fill=r,alpha=r),color=NA) 
  scale_fill_gradientn(colours = rev(RColorBrewer::brewer.pal(11,"RdBu"))) 
  new_scale_fill() 
  geom_couple(aes(colour=pd,linetype=pd),data=cordata,label.colour = "black",
              curvature=nice_curvature(0.1),nudge_x=0.3,
              label.fontface=2,
              label.size =3,drop =T) 
  scale_colour_manual(values =c("red","#1B9E77"))  
  scale_linetype_manual(values = c("solid","dashed")) 
  guides(size = guide_legend(title = " ",override.aes = list(colour = "grey35"), order = 2),
         colour = guide_legend(title = "Mantel's p",override.aes = list(size = 3), order = 1),
         linetype=guide_legend(title = "Mantel's p",override.aes = list(size = 3), order = 1),
         alpha = "none") 
  theme(plot.margin = unit(c(0,-3,0,-2),units="cm"),
        panel.background = element_blank(),
        plot.background = element_blank(),
        axis.text=element_markdown(color="black",size=8,face="bold"),
        legend.background = element_blank(),
        legend.key = element_blank(),
        legend.title = element_text(margin = margin(b= 5)),
        legend.spacing.y = unit(0,"cm"),
        legend.key.height = unit(0.5,"cm"))

0 人点赞