加载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()