Friday, September 5, 2014

New function to ladderize tree with mapped discrete character

I just wrote a new utility function, ladderize.simmap, that can be used to “ladderize” a phylogenetic tree with a mapped discrete character. This function is analogous to the function ladderize in the ape package - in fact, it uses ladderize internally.

All ladderization does is rotate internal nodes to achieve an effect whereby right (or left) daughter clades tend to be more species rich than left daughter clades. Since node rotation is arbitrary, this can be done without any change in the phylogenetic information contained by the tree. The visual effect of ladderization will be strongest when the tree is highly imbalanced.

So, for instance, here is a ladderization of a simple pure-birth tree:

library(phytools)
packageVersion("phytools")
## [1] '0.4.33'
set.seed(890)
tree<-pbtree(n=200,scale=1)
plotTree(tree,ftype="off")

plot of chunk unnamed-chunk-1

plotTree(ladderize(tree),ftype="off")

plot of chunk unnamed-chunk-1

Now, using a new version of phytools (phytools_0.4-33) let's try ladderizing an object with a mapped discrete character. In this case, we'll just create it using sim.history to simulate a discrete character history on the tree:

Q<-matrix(c(-2,1,1,1,-2,1,1,1,-2),3,3)
rownames(Q)<-colnames(Q)<-letters[1:3]
mtree<-sim.history(tree,Q)
plotSimmap(mtree,ftype="off")
## no colors provided. using the following legend:
##        a        b        c 
##  "black"    "red" "green3"

plot of chunk unnamed-chunk-2

plotSimmap(ladderize.simmap(mtree),ftype="off")
## no colors provided. using the following legend:
##        a        b        c 
##  "black"    "red" "green3"

plot of chunk unnamed-chunk-2

Something else that's kind of cool is that we can also use the same method to ladderize objects of class "densityMap" and "contMap" since the tree is stored in the same way internally.

So, for instance:

set.seed(1)
x<-fastBM(tree)
obj<-contMap(tree,x,plot=FALSE)
plot(obj,ftype="off",lwd=3,outline=FALSE)

plot of chunk unnamed-chunk-3

obj$tree<-ladderize.simmap(obj$tree)
plot(obj,ftype="off",lwd=3,outline=FALSE)

plot of chunk unnamed-chunk-3

This could be especially handy if our tree was crowding the color legend in the bottom left corner. (Of course, we can also move that, but this is easier).

That's it!

3 comments:

  1. Hi Liam, I've been trying to plot a ladderize mapped tree, that was created from a tree and trait vectors with make.simmap, instead of sim.history. But I get this error :
    Error in tree$maps[[jj[i]]] : invalid subscript type 'list'
    Thanks, Martha Serrano

    ReplyDelete
    Replies
    1. Hi Martha. I cannot reproduce that error with simulation (i.e., simulated sim.history & make.simmap trees both ladderize fine on my system). Can you save & email me your R workspace with the data that produce the error? Then I will try and let you know what I find. My email is liam.revell@umb.edu. - Liam

      Delete
  2. This comment has been removed by the author.

    ReplyDelete

Note: due to the very large amount of spam, all comments are now automatically submitted for moderation.