## Tuesday, August 7, 2012

### Using phylomorphospace3d to plot 3D traitgrams

I realized after creating the three dimensional phylomorphospace function (phylomorphospace3d, described here) that one neat use of this function would be to do three dimensional traitgrams. That is, a projection of the tree into a space defined by time on one axis, and phenotype (in this case) on the other two.

This is relatively easy to do. This is because we've set up phylomorphospace3d to either estimate ancestral states or take them as input. To create a traitgram/phenogram - we just need to first estimate ancestral states for two characters, and then also compute the heights of internal and terminal nodes as our third "trait" (time from the root of the tree).

Both of these tasks are straightforward to accomplish. First, to estimate ancestral nodes for two characters encoded as columns in matrix X, we just do:

> require(phytools)
> A<-apply(X,2,function(x,tree) anc.ML(tree,x)\$ace,tree=tree)

Then, we can compute the heights of internal and terminal nodes and attach them as columns in X and A, respectively.

> X<-cbind(X,diag(vcv(tree))[rownames(X)])
> A<-cbind(A,nodeHeights(tree)[match(rownames(A)[1:nrow(A)], tree\$edge)])
> colnames(X)[3]<-colnames(A)[3]<-"time"

The final step, of course, just names our new trait axis what it is "time" since the root of the tree. With these parts, we can now use the new function phylomorphospace3d to plot our 3D traitgram.

I have built the steps above into the wrapper, fancyTree. To try it, just download the latest versions of the code for fancyTree (here) and phylomorphospace3d (here), load the source, and give it a try. The code below also uses the new simulation function sim.corrs, described here, but not yet a part of phytools.

> source("fancyTree.R")
> source("phylomorphospace3d.R")
> source("sim.corrs.R")
> require(phytools)
> tree<-pbtree(n=100,scale=10)
> X<-sim.corrs(tree,vcv=matrix(c(1,0.75,0.75,1),2,2))
> fancyTree(tree,type="traitgram3d",X)

And here's the result:

Cool.

1. Very cool function. Should specifying colours via control= work in fancyTree? For my data it works for phylomorphospace, but not for phylomorphospace3d.

- Roger

1. Hi Roger.

Unfortunately, this is not yet possible in phylomorphospace3d. I don't think it would be too hard to add, but I just have not done this yet. Sorry! When I add this feature, I will definitely note it in the blog.

Thanks for using phytools. - Liam

2. Hi Liam!

I have been using this function and really like it and like Roger would want to be able to change the color of the lines and the label size. Looking forward to being able to do this! Will look amazing in the little movie!

Thanks again!

Ricardo

3. I know it's been a few years since this blog post's been up, but I have a function that does the same thing, which you can colours the branches.
It's not in any R package yet, but the paper is here: http://journals.plos.org/plosone/article?id=10.1371/journal.pone.0039752#pone-0039752-g006

thanks,
Manabu

1. Cool. I like it! I was going to ask why you didn't cite phytools, but I see your paper & function are several years old. I should've been citing you! I also like how you show the phylomorphospace in two and three (with time) dimensions simultaneously. Thanks for sharing this.

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