nature microbiology图表复现之聚类条形图
Editor's Note
jjAnno实践!
The following article is from R语言数据分析指南 Author ANERYAN
❝本节来复现「nature microbiology」上的一张带聚类的条形图,图均基于「ggplot2」,「ggtree」,通过「jjAnno」很轻松的添加了阴影,下面来进行具体介绍,「数据代码已经上传VIP交流群」
❞
Fusarium fruiting body microbiome member Pantoea agglomerans inhibits fungal pathogenesis by targeting lipid rafts
❝❞
有需要的观众老爷欢迎加入小编的VIP群
,目前已经上传「2021-2022两年公众号文档数据+代码约180篇左右」包含付费文档,扫描文末尾二维码加小编微信「付费99元」后邀请进群,「由于群名额有限人满之后将不在添加新成员」,有需要的请尽早加入,早进早享受;「一定让你感受到物超所值」,加入小编的VIP如果你有一些让我感兴趣的图表提供示例数据小编若有时间会写成推文发送
加载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」后添加阴影背景的确方便很多,「有需要的观众老爷欢迎加入小编的VIP交流群,请备注一下信息及来意」,绝让你体会到物超所值
❞
小编微信
关注下方公众号下回更新不迷路
往期推荐