其他
ggtree优雅的绘制系统发育树(3)
欢迎关注R语言数据分析指南
❝本节来继续绍如何使用「Y叔开发的ggtree包」基于微生物多样性的丰度表来绘制进化树,后续将会继续介绍各种案例,下面来看具体操作
❞
加载R包
package.list=c("tidyverse","ggtreeExtra","ggtree","treeio","ggnewscale")
for (package in package.list) {
if (!require(package,character.only=T, quietly=T)) {
install.packages(package)
library(package, character.only=T)
}
}
加载数据
df <- read.delim("genus.xls",sep="\t",row.names = 1)
数据清洗
生成绘制热图的数据
dat2 <- df %>% mutate_if(is.numeric,function(x) x+ 1) %>%
log10() %>%
rownames_to_column(var="ID") %>% pivot_longer(-ID) %>%
mutate(name = trimws(str_remove(name,"(\\s+[A-Za-z]+)?[3-6-]+")))
生成绘制柱状图的数据
dat3 <- dat2 %>% arrange(desc(value)) %>% group_by(ID) %>%
filter(row_number()==1) %>% ungroup() %>%
mutate(name = trimws(str_remove(name,"(\\s+[A-Za-z]+)?[0-9-]+"))) %>%
sample_frac(.3)
绘制基础热图
p <- hclust(dist(df)) %>% ggtree(layout="fan", open.angle=10)
进化树美化
p + new_scale_fill() +
geom_fruit(data=dat2, geom=geom_tile,
mapping=aes(y=ID, x=name,alpha=value,fill=name),
color = "grey50",offset = 0.04,size = 0.02)+
scale_fill_manual(values=c("#FFC125","#87CEFA","#7B68EE","#808080",
"#800080", "#9ACD32","#D15FEE","#FFC0CB",
"#EE6A50","#8DEEEE", "#006400","#800000",
"#B0171F","#191970"))+
scale_alpha_continuous(guide=NULL)+
new_scale_fill()+
geom_fruit(data=dat3,geom=geom_bar,
mapping=aes(y=ID, x=value,fill=name),
pwidth=0.38,
orientation="y",
stat="identity",
)+
scale_fill_manual(values=c("#E64B35FF","#4DBBD5FF","#00A087FF","#3C5488FF","#F39B7FFF","#8491B4FF"))+
theme(legend.position=c(0.45, 0.5),
legend.background=element_rect(fill=NA),
legend.title=element_blank(),
legend.text=element_text(size=10)
)
数据获取
❝本节的内容到此结束,主要参考了「ggtree」官方文档的案例带入自己的数据做了可视化,建议大家多看官方文档内容更加丰富,喜欢的小伙伴欢迎转发此文档附上一句话到朋友圈「30分钟后台截图给我」,即可获取对应的数据及代码,如未及时回复可添加我的微信
❞
欢迎大家扫描下方二位码加入「QQ交流群」,与全国各地上千位小伙伴交流
「关注下方公众号下回更新不迷路」,如需要加入微信交流群可添加小编微信,请备注单位+方向+姓名
往期推荐