基因组数据绘图群体遗传学R语言作图

ggtree绘制进化树

2020-09-26  本文已影响0人  R语言数据分析指南

绘制进化树的方法有很多,入门的MEGA,iTOL,evolview等,但是没有一个如ggtree这般高效简单,轻轻松松绘制高端进化树,废话不多说,直接看代码

rm(list = ls())
library(pacman)
pacman::p_load(ggplot2,ggstar,ggtreeExtra,ggtree,
               treeio,ggnewscale,ggsci); options(warn = -1)

tree <- read.newick("tree.nwk",node.label = "support")
#node.label = "support"将node label解析为support value,另存为树注释数据
group <- read.table("tree_group.txt",header = T,sep="\t")
#读入分组文件用于绘制条行图,并按分组添加颜色
group_file <- read.table("tree_group.txt",header = T,row.names = 1)
groupInfo <- split(row.names(group_file), group_file$Group)
tree <- groupOTU(tree, groupInfo)

ggtree(tree,branch.length = "none",layout = "circular",
       linetype=1,size=1,ladderize = F,aes(color=group))+
  #geom_text(aes(label=node), hjust=-3,size=1) +
#展示节点信息,并根据节点信息添加外圈分组
  scale_color_npg()+guides(color=FALSE)+
#guides(color=FALSE) 移除图例
geom_fruit(data=group,
               geom=geom_bar,
               mapping=aes(y=Sample, x=Length,fill=Group),
               orientation="y",stat="identity",color="white")+
  scale_fill_npg()+labs(fill = "")+new_scale_fill()+
  geom_strip(53,116, #geom_strip()根据节点添加外部条带,后跟节点位置信息
                  label = "Group I", align = T, alpha=.8,family="Times",
fontsize=4,offset = 3, color = "orange",offset.text = 2 ,
                  hjust="center",barsize = 4,extend = 0.5)+
#offset.text调整label位置;extend调整条带之间间距;offset设置距离节点的位置;
#hjust="center"将lable居中放置
  geom_strip(35,52,
             label = "Group II", align = T, alpha=.8,family="Times",
             offset = 3, color = "green",offset.text = 2 ,fontsize=4,
             hjust="center",barsize = 4,extend = 0.5,angle = 45)+
  geom_strip(20,34,
             label = "Group III", align = T, alpha=.8,family="Times",
             offset = 3, color = "red",offset.text = 2 ,fontsize=4,
             hjust="center",barsize = 4,extend = 0.5)+
  geom_strip(1,19,
             label = "Group IV", align = T, alpha=.8,family="Times",
             offset = 3, color = "blue",offset.text = 2 ,fontsize=4,
             hjust="center",barsize = 4,extend = 0.5,angle=-60)+
  #geom_tiplab(hjust = -.5,size=3,fontface="plain")+
 #设置标签显示
  geom_point2(aes(subset=!isTip,fill=support),
              shape=21,size=2)+
  scale_fill_continuous(low='green', high='red')+
  labs(fill = "bootstrap")
tree.jpeg
参考:https://rdrr.io/bioc/ggtree/man/geom_strip.html
https://bioconductor.org/packages/devel/bioc/vignettes/ggtreeExtra/inst/doc/ggtreeExtra.html
http://yulab-smu.top/treedata-book/
示例文件: https://pan.baidu.com/s/1V2xo7rQQMDFIlXY5tJCUQQ
提取码: gr2t
上一篇下一篇

猜你喜欢

热点阅读