ggtree改变半径范围

时间:2016-03-15 00:38:22

标签: r resize bioconductor phylogeny

我正在尝试用ggtree创建一个系统发育树。我无法调整树的中心大小,以便表示我的序列的边缘上的符号不重叠并增加半径,以便分支长度不会那么长。任何有关如何做到这一点的建议或提示将不胜感激。 提前谢谢!

我使用以下脚本来创建树...

ggtree(PhyMLAATree, layout = "circular") + 
    geom_tippoint(aes(x=x+.01), shape=Shape, fill=Fill, size=3)

这两个图像显示了我正在创建的内容以及我要创建的内容。 树出来看起来如下: This is how the output looks

希望最终的树看起来更像这样一个更大的中心: This is how I would like the tree to look

我尝试过分配一个空间更大的设备,但我在树上的比例相同(石英(宽度= 12,高度= 12)

2 个答案:

答案 0 :(得分:0)

在我的方法中,我按照层次级别从内边缘到外边缘的负指数分布重新创建边长。

library(ggtree) # install as in:
#source("https://bioconductor.org/biocLite.R")
#biocLite("ggtree")  
library(data.tree)
library(gridExtra)
library(rlist)
library(phangorn)

#create random tree
tree<-ape::rtree(251)
#its plot
originalplot<-ggtree(tree, layout="circular")+ geom_tiplab2() + labs(title="original tree") +
  theme(plot.title = element_text(hjust = 0.5))

transformtree<-function(tree,radialparameter,repeatparameter,tiplength){
  # radialparameter # # change this to collapse less(0.5) or more (3) and modify repeatparameter together
  # repeatparameter # # i.e. increase if there are very small branches (levels)
  #number of hierarchical levels in tree
  dfr0<-as.data.frame(tree$edge)
  tree2<-FromDataFrameNetwork(dfr0)# data.tree package
  levels<-ToDataFrameTable(tree2, "level")
  edgelevels<-max(levels)-1
  # establish the hierarchy of nodes looking for the children of the children nodes
  centralnode<-getMRCA(tree,1:length(tree$tip.label))
  childrenlist<-list()
  childrenlist[1]<-list(phangorn::Children(tree, centralnode))
  for (i in 2:edgelevels){
    childrenlist[i]<- list(unlist(lapply(unlist(childrenlist[i-1]), function(x) phangorn::Children(tree, x) ) ) )
  }
  # remove nodes of tips, we do not want to modify their length
  childrentipsremoved<-lapply(childrenlist, function(x) x[!is.element(x,1:length(tree$tip.label))])
  # list of inner nodes
  groupedinnernodes<-rlist::list.clean(childrentipsremoved, fun = function(x) length(x) == 0L)
  #this is the vector that will multiply the inner edges
  transfvector<- rep(((c(1:(length(groupedinnernodes)/repeatparameter))^(-radialparameter) )*5),
                     each=repeatparameter) 
  # check length of groups of inner nodes and the transformation vector
  lengths<-unlist(lapply(groupedinnernodes, function(x) length(x)) )
  if(length(lengths)-length(transfvector)>0) {
    for (i in 1:abs(length(lengths)-length(transfvector) )   ){
      transfvector <- c(transfvector,transfvector[length(transfvector)]) 
    } }
  if(length(lengths)-length(transfvector)<0) {
    for (i in 1:abs(length(lengths)-length(transfvector) )   ){
      transfvector <- transfvector[-1] }}
  # create the factor to transform the inner edges
  vector1<-unlist(mapply(rep, transfvector,lengths) )
  # discard length info, replace all edge length information by 1
  size<-length(tree$edge.length)
  tree$edge.length<-rep(1,size)
  # replace edge length for the connecting inner nodes only
  innernodes<-unlist(groupedinnernodes)
  tree$edge.length[unlist(lapply(innernodes,function(x,y) which(y==x),y=tree$edge[,2]) )]<-
    tree$edge.length[unlist(lapply(innernodes,function(x,y) which(y==x),y=tree$edge[,2]) )]*
    vector1
  # modify length of tip edges # optional decrease for big trees
  tree$edge.length[tree$edge.length==1]<-tiplength
  return(tree)
}

tree<-transformtree(tree,2.5,2,0.2)
# plot of modified tree aligned tips
newplot<-ggtree(tree, layout="circular")+ geom_tiplab2(align=T,linetype=1, linesize=.02, 
                                                       size=1.8, offset=0.5) + labs(title="modified tree align=T")+
  theme(plot.title = element_text(hjust = 0.5))

# plot of modified tree not aligned tips
newplot2<-ggtree(tree, layout="circular")+ geom_tiplab2(align=F,linetype=1, linesize=.02, 
                                                        size=1.8, offset=0.5) + labs(title="modified tree align=F")+
  theme(plot.title = element_text(hjust = 0.5))

#plots
gridExtra::grid.arrange(originalplot,newplot,newplot2,ncol=2)

enter image description here

答案 1 :(得分:0)

可以通过用xlim指定layout = "circular"来完成,如下所示:

nwk <- system.file("extdata", "sample.nwk", package="treeio")
tree <- read.tree(nwk)

library(ggtree)

ggtree(tree, layout = "circular") +
  geom_tippoint() +
  geom_tiplab2(offset = 7) +
  xlim(-150, NA)

enter image description here

如图4.3中的in the ggtree documentation所述。播放xlim值以更改中心的直径。