基因家族分析(4)ggtree绘制高端进化树

系统发育树几乎是基因家族文章的必备图,一个好看的树图的确能给文章增色不少,一般的用户可能只使用MEGA绘图,进阶一点的便会使用iTOL,evolview等2款在线绘图平台,然而这并不是本文所要介绍的重点,本节来介绍如何使用ggtree来绘制系统发育树,自从有了ggtree进化树绘制从未如此简单

加载R包

library(tidyverse)
library(ggtree)
library(treeio)
library(ggsci)

读入数据

node.label = "support"将node label解析为support value,另存为树注释数据

tree <- read.newick("tree.nwk",node.label = "support")
group <- read.table("group.xls",header = T,row.names = 1)
group1 <- split(row.names(group),group$Group)

数据可视化

先绘制出树的主干

p <- groupOTU(tree,group1) %>%
  ggtree(branch.length = "none",
         linetype=1,size=0.8,ladderize = T,aes(color=group))

p

根据节点信息给分组添加背景颜色

p1 <- p + geom_text(aes(label=node), hjust=-3,size=3)+
  scale_color_nejm()+labs(color="")+
  geom_hilight(node=63,fill="#1E90FF",type="rect",alpha=0.6)+
  geom_hilight(node=36,fill="#FF8C00",type="rect",alpha=0.6)+
  geom_hilight(node=43,fill="#4DAF4A",type="rect",alpha=0.6)+
  geom_hilight(node=47,fill="#984EA3",type="rect",alpha=0.6)

添加置信度信息

p2 <- p1 + geom_point2(aes(subset=!isTip,
fill=support),shape=21,size=2)+
  scale_fill_continuous(low='green',high='red')

根据节点绘制分组色条,此处的参数很多,先无需全部调整只需要确定色条的起始与终止位点即可,因为后续我们需要将其转化为圆形

p3 <- p2 + geom_strip(62,49,label = "Group I", align = T, alpha=.8,family="Times",
             fontsize=5,offset = 7, color = "#984EA3",offset.text = 2 ,
             hjust="center",barsize = 5,extend = 1)+
  geom_strip(44,46,label = "Group II", align = T, alpha=.8,family="Times",
             fontsize=5,offset = 7, color ="#4DAF4A",offset.text = 2 ,
             hjust="center",barsize = 5,extend = 1,angle=66)+
  geom_strip(40,1,label = "Group III", align = T, alpha=.8,family="Times",
             fontsize=5,offset = 7, color = "#FF8C00",offset.text = 2 ,
             hjust="center",barsize = 5,extend = 1,angle=0)+
  geom_strip(63,66,label = "Group V", align = T, alpha=.8,family="Times",
             fontsize=5,offset = 7, color = "#1E90FF",offset.text = 2 ,
             hjust="center",barsize = 5,extend = 1,angle=-50)

经过上面的步骤我们得到了给分组添加背景及添加分组色条的位置信息,下面让我们从一开始就将图形布局展示为圆形

完整版代码

pp3 <- groupOTU(tree,group1) %>%
  ggtree(branch.length = "none",layout = "circular",
         linetype=1,size=0.8,ladderize = T,aes(color=group))+
  #geom_text(aes(label=node), hjust=-3,size=3) +
  geom_tiplab(size=3,family="Times",hjust = -0.1)+
  scale_color_nejm()+labs(color="")+
  geom_hilight(node=63,fill="#1E90FF",type="rect",alpha=0.6)+
  geom_hilight(node=36,fill="#FF8C00",type="rect",alpha=0.6)+
  geom_hilight(node=43,fill="#4DAF4A",type="rect",alpha=0.6)+
  geom_hilight(node=47,fill="#984EA3",type="rect",alpha=0.6)+
  geom_point2(aes(subset=!isTip,fill=support),shape=21,size=2)+
  scale_fill_continuous(low='green',high='red')+
  geom_strip(62,49,label = "Group I", align = T, alpha=.8,family="Times",
             fontsize=5,offset = 7, color = "#984EA3",offset.text = 2 ,
             hjust="center",barsize = 5,extend = 1)+
  geom_strip(44,46,label = "Group II", align = T, alpha=.8,family="Times",
             fontsize=5,offset = 7, color ="#4DAF4A",offset.text = 2 ,
             hjust="center",barsize = 5,extend = 1,angle=66)+
  geom_strip(40,1,label = "Group III", align = T, alpha=.8,family="Times",
             fontsize=5,offset = 7, color = "#FF8C00",offset.text = 2 ,
             hjust="center",barsize = 5,extend = 1,angle=0)+
  geom_strip(63,66,label = "Group V", align = T, alpha=.8,family="Times",
             fontsize=5,offset = 7, color = "#1E90FF",offset.text = 2 ,
             hjust="center",barsize = 5,extend = 1,angle=-50)+
  labs(fill = "bootstrap")
ggsave(pp3,file="tree.png",width =8,height =7,units="in",dpi=300)

pp3
library(ggpubr)
tibble(函数= c("geom_strip", "geom_tiplab()","geom_fruit()",
             "offset.text","extend","offset","hjust=center"),
         功能= c("根据节点添加外部条带,后跟节点位置信息", 
               "设置标签显示","绘制条形","调整label位置",
               "调整条带之间间距","设置距离节点的位置","将lable居中放置")) %>%
  ggtexttable(rows = NULL, theme = ttheme("mBlue"))

文件链接:https://mp.weixin.qq.com/s/icdgysLduSd2Q61HSDKhJg

你可能感兴趣的:(基因家族分析(4)ggtree绘制高端进化树)