Wednesday, February 24, 2016

Adding node labels (including bootstrap values) to a tree plotted using dotTree

Yesterday, a phytools blog reader asked the following:

Thank you very much for this great addition. May I ask if there's any way to add bootstrap value by each node on the tree plotted by dotTree?

The answer is “yes.” The internal plotting function used by dotTree (phylogram), though not in the namespace of phytools, nonetheless works in similar ways to the S3 plotting method plot.phylo in ape, and other phytools plotting functions such as plotTree, in that it sets the environmental variable "last_plot.phylo" & thus can be used with functions such as nodelabels and tiplabels from the ape package.

Here's a demo which assumes that the bootstrap values are stored as node labels on the object of class "phylo":

library(phytools)
tree$node.label
##  [1] ""   "76" "88" "88" "80" "91" "99" "95" "85" "89" "81" "83" "98" "98"
## [15] "99" "77" "80" "87" "95" "78" "76" "92" "79" "93" "79"
dotTree(tree,X,standardize=TRUE,colors="grey")
nodelabels(tree$node.label,node=2:tree$Nnode+Ntip(tree),
    adj=c(1,-0.2),frame="none")

plot of chunk unnamed-chunk-1

Here, I am going to show the bootstrap proportion as a pie chart at each node:

dotTree(tree,X,standardize=TRUE,colors="grey")
nodelabels(node=1:tree$Nnode+Ntip(tree),
    pie=cbind(as.numeric(tree$node.label),100-as.numeric(tree$node.label)),
    piecol=c("black","white"),cex=0.5)

plot of chunk unnamed-chunk-2

Note that we have to be very careful with things like bootstrap values & posterior probabilities in R because though they are usually stored as node labels, they are actually associted with edges (i.e., splits) not with nodes! This distinction become important if we re-root the tree. Perhaps there should be more on this topic in a future post.

Data for this demo were simulated as follows:

tree<-pbtree(n=26,tip.label=LETTERS) ## random tree
X<-fastBM(tree,nsim=2) ## random traits
## random bootstraps
tree$node.label<-c("",round(runif(n=tree$Nnode-1,min=75,max=100)))

No comments:

Post a Comment