nature microbiology图表复现之聚类条形图

2022-09-21 15:35:02 浏览数 (1)

❝本节来复现「nature microbiology」上的一张带聚类的条形图,图均基于「ggplot2」,「ggtree」,通过「jjAnno」很轻松的添加了阴影,下面来进行具体介绍 Fusarium fruiting body microbiome member Pantoea agglomerans inhibits fungal pathogenesis by targeting lipid rafts ❞

加载R包

代码语言:javascript复制
library(tidyverse)
library(RColorBrewer)
library(ggtree)
library(aplot)
library(grid)
library(ggplotify)
library(cowplot)
# devtools::install_github("junjunlab/jjAnno")
library(jjAnno)

「关于jjAnno」更多详细的内容可点击下方链接https://mp.weixin.qq.com/s/CVXsJPPx12saw0WYiReQag

定义Y轴文本颜色

代码语言:javascript复制
ycols <- rev(rep(c("#EDB749","#3CB2EC","#9C8D58"),time=c(13,13,13)))

绘制条形图

代码语言:javascript复制
p1 <- read_tsv("data.xls") %>% 
  ggplot(aes(Abundance,id,fill=Phylum),color="black") 
  geom_bar(stat="identity",position="fill") 
  labs(x=NULL,y=NULL) 
  theme_test() 
  theme(axis.title.x=element_blank(),
        axis.ticks.y=element_blank(),
        axis.ticks.x=element_blank(),
        axis.text.y=element_text(face = "bold",size = 10,color=ycols),
        axis.text.x=element_text(color="black",size=10),
        legend.title=element_blank(),
        legend.text = element_text(color=c(rep("#3CB2EC",4),rep("#EDB749",8))),
        legend.spacing.x=unit(0.2,'cm'),
        legend.key=element_blank(),
        legend.key.width=unit(0.5,'cm'),
        legend.key.height=unit(0.5,'cm'),
        plot.margin = margin(1,0.5,0.5,1,unit="cm")) 
  scale_fill_brewer(palette="Paired") 
  scale_x_continuous(expand=c(0,0)) 
  coord_cartesian(clip = 'off')

添加阴影

代码语言:javascript复制
p2 <- annoRect(object = p1,annoPos = 'left',aesGroup = T,
               aesGroName = 'Group',
               xPosition = c(-0.25,-0.005),
               pFill =rev(c("#FCFAD9","#FFF2E7","#E8F2FC","#BDE7FF","#EEECE1","#DDD9C3")),
               pCol =rev(c("#FCFAD9","#FFF2E7","#E8F2FC","#BDE7FF","#EEECE1","#DDD9C3")),
               rectWidth = 1)

数据清洗

代码语言:javascript复制
p <- read_tsv("data.xls") %>% select(-Group) %>% filter(Phylum !="Other") %>% 
  pivot_wider(names_from="Phylum",values_from ="Abundance") %>% 
  column_to_rownames(var="id")

导入分组信息

代码语言:javascript复制
group <- read_tsv("data.xls") %>% select(id,Group)

进行聚类

代码语言:javascript复制
hr <- hclust(dist(p)) %>% ggtree(layout="rectangular",branch.length="none") %< % group 
  geom_tippoint(aes(fill=Group,color=Group),shape=21,size=4) 
  theme(legend.title = element_blank(),legend.key = element_blank(),
        legend.text = element_text(color=c(rep("#EDB749",3)))) 
  scale_fill_manual(values =c("#BDE7FF","#EEECE1","#DDD9C3")) 
  scale_color_manual(values =c("#BDE7FF","#EEECE1","#DDD9C3"))

拼图

代码语言:javascript复制
p2 %>% insert_left(hr,width=.5) %>% as.grob() %>% ggdraw()

❝本节介绍到此结束,可以看到小细节还是蛮多的,使用「jjAnno」后添加阴影背景的确方便很

0 人点赞