Just now messing around in R (as one does), I discovered a new hack for plotting clades as triangles (a la phylo.toBackbone
) for fan or arc style trees.
Let me give a quick & lightly annotated demo.
First, I’ll simulate a 80 taxon tree. I’m going to set my computer seed first, just to ensure we all end up with a tree with the same node indices (for those following along at home).
## load packages
library(phytools)
## set seed
set.seed(99)
## simulate tree
tree<-pbtree(n=80)
tree
##
## Phylogenetic tree with 80 tips and 79 internal nodes.
##
## Tip labels:
## t5, t73, t74, t38, t53, t54, ...
##
## Rooted; includes branch length(s).
## graph our tree in arc style
plotTree(tree,type="arc",arc_height=1,fsize=0.6)
nodelabels(bg="white",cex=0.6)
Let’s say I want to replace the clade descended from node “125” with a triangle in my plot.
My “hack” to do so is to first identify all the tips descended from the node, which can be done as follows:
node<-125
tips<-getDescendants(tree,node)
tips<-tips[tips<=Ntip(tree)]
tips
## [1] 44 42 43 45 46 47 48 49 50 51 52 55 53 54
Then we create a paint a “regime” on our tree for the clade descended from node “125”.
tree<-paintSubTree(tree,node=node,state="1",anc.state="0")
Then we re-plot our tree using a transparent color for the mapped “regime,” and add the polygon in a standard way. Here, I use chull
only to sort my coordinates into a closed polygonal shape. All of them will appear in the polygon!
plot(tree,setNames(c("black","transparent"),0:1),type="arc",
arc_height=1,fsize=0.6)
pp<-get("last_plot.phylo",envir=.PlotPhyloEnv)
dd<-c(node,tips)
mh<-chull(pp$xx[dd],pp$yy[dd])
polygon(pp$xx[dd][mh],y=pp$yy[dd][mh],lwd=1,col="red")
OK, it looks a bit more interesting than planned – I warned you it was a hack!
Let’s add the other major clades of the tree.
nodes<-c(85,93,95,108,118,125,138,146,152)
for(i in 1:length(nodes))
tree<-paintSubTree(tree,node=nodes[i],state="1")
plot(tree,setNames(c("black","transparent"),0:1),type="arc",
arc_height=1,ftype="off")
pp<-get("last_plot.phylo",envir=.PlotPhyloEnv)
for(i in 1:length(nodes)){
tips<-getDescendants(tree,nodes[i])
tips<-tips[tips<=Ntip(tree)]
dd<-c(nodes[i],tips)
mh<-chull(pp$xx[dd],pp$yy[dd])
polygon(pp$xx[dd][mh],y=pp$yy[dd][mh],lwd=1,
col=hcl.colors(n=length(nodes))[i])
}
That’s kind of fun.
We can do one more experiment in which we do the same thing, but we include all the nodes in our polygon – instead of just the MRCA and the tips.
par(bg="black")
nodes<-c(85,93,95,108,118,125,138,146,152)
for(i in 1:length(nodes))
tree<-paintSubTree(tree,node=nodes[i],state="1")
plot(tree,setNames(c("white","transparent"),0:1),type="arc",
arc_height=1,ftype="off")
pp<-get("last_plot.phylo",envir=.PlotPhyloEnv)
for(i in 1:length(nodes)){
tips<-getDescendants(tree,nodes[i])
dd<-c(nodes[i],tips)
mh<-chull(pp$xx[dd],pp$yy[dd])
polygon(pp$xx[dd][mh],y=pp$yy[dd][mh],lwd=1,
col=hcl.colors(n=length(nodes))[i],border="white")
}
That’s too far.
No comments:
Post a Comment
Note: due to the very large amount of spam, all comments are now automatically submitted for moderation.