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)))

9 comments:

  1. Thank you for a fantastic package! I'm wondering how to label only specific nodes on a phylogeny based on the labels from the function nodelabels()? I'm interested in only labeling bootstrap support for the deeper nodes in my phylogeny and have a vector of nodelabels I'm interested in but am unsure how to access the bootstrap support for those labels. For example :
    how would I access the bootstrap support in t$tip.label for nodes labeled : c(1093, 1073, 1068)?

    nodelabels(t$tip.label?? , adj=c(1,-0.4),frame="none", cex=0.7,
    node = c(1093, 1073, 1068))

    Thank you!

    ReplyDelete
    Replies
    1. Hi Katie. Why are you bootstrap support values stored in the tip labels? Normally I would expect them to be stored as node labels. In which case you should be able to do something like:

      nodes<-c(1093,1073,1068)
      bs<-tree$node.label[nodes-tree$Nnode]
      nodelabels(bs,nodes,...)

      in which ... are the other arguments you want to send to nodelabels.

      Let me know if this gets you on the right track.

      - Liam

      Delete
  2. Excellent, thank you! Yes, my bootstrap support values were stored as node labels. In the future, is there a place where node numbers are stored? Is there a way to access them other than visualizing them on the tree with the nodelabels function? Thank you!

    ReplyDelete
    Replies
    1. They should be in the vector tree$node.label.

      All the best, Liam

      Delete
  3. It is really a great work and the way in which you are sharing the knowledge is excellent.Thanks for your informative article

    web designing course in chennai

    ReplyDelete
  4. I admire this article for the well-researched content and excellent wording. I got so involved in this material that I couldn’t stop reading. I am impressed with your work and skill. Thank you so much.
    HTML5 courses in chennai

    ReplyDelete
  5. Bootstrap is one of the best framework among all and it is being used almost everywhere. thanks for sharing this article. https://www.technologist360.com/

    ReplyDelete
  6. Hello, can you help?. I wrote my question in https://www.biostars.org/p/338474/ I have a problem to show bootstrap in my tree using R.

    Thanks a lot

    ReplyDelete
  7. Indeed Bootstrap is the best and very popular framework for website design. Thanks for the article. I really enjoyed this. Thanks for the dotTree code! hire magento developer

    ReplyDelete