Friday, May 11, 2012

Painting different clades with different colors in phenogram function

A user recently submitted the following question:

is it possible to create a phenogram (that is, a projection of the tree into a two dimensional space defined by morphology on the vertical axis and time, see here) with different clades as different colors?

The function phenogram already allows a mapped discrete character, so if we want to color arbitrary clades on our tree different colors, we just need to combine phenogram with the phytools function paintSubTree, described here.

Let's try it in the following illustrating example:

> # first simulate a random tree
> tree<-pbtree(n=20,scale=100)
> # now plot the tree with node labels
> # so we can select the clades we want to paint
> plotTree(tree,pts=F,node.numbers=T)

OK, this tree has two main clades: one descended from node number "22", and the other descended from node number "26". Let's first paint the former clade with state "2" and the latter clade with state "3", we can leave the stem branches of each clade in state "1" (although we need not).

> tree<-paintSubTree(tree,node=22,state="2")
> tree<-paintSubTree(tree,node=26,state="3")
> # now let's plot using plotSimmap to ensure
> # that the correct branches were painted
> cols<-c("black","blue","red"); names(cols)<-1:3
> plotSimmap(tree,cols,pts=F,lwd=3,node.numbers=T)

Finally, let's simulate some data (normally, of course, we would read our data from file) and plot a traitgram using the phenogram function in phytools:

> x<-fastBM(tree)
> phenogram(tree,x,colors=cols,fsize=0.8)

That's it.

1 comment:

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