本节来复现nature microbiology上的一张带聚类的条形图,图均基于ggplot2,ggtree,通过jjAnno很轻松的添加了阴影,下面来进行具体介绍,数据代码已经上传VIP交流群
Fusarium fruiting body microbiome member Pantoea agglomerans inhibits fungal pathogenesis by targeting lipid rafts
加载R包
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轴文本颜色
ycols <- rev(rep(c("#EDB749","#3CB2EC","#9C8D58"),time=c(13,13,13)))
绘制条形图
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')
添加阴影
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)
数据清洗
p <- read_tsv("data.xls") %>% select(-Group) %>% filter(Phylum !="Other") %>%
pivot_wider(names_from="Phylum",values_from ="Abundance") %>%
column_to_rownames(var="id")
导入分组信息
group <- read_tsv("data.xls") %>% select(id,Group)
进行聚类
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"))
拼图
p2 %>% insert_left(hr,width=.5) %>% as.grob() %>% ggdraw()
本节介绍到此结束,可以看到小细节还是蛮多的,使用jjAnno后添加阴影背景的确方便很多,有需要的观众老爷欢迎关注我的公众号R语言数据分析指南,请备注一下信息及来意,绝让你体会到物超所值