Wednesday, July 10, 2013

Phylomorphospace with time since the root projected using a color gradient

Two different people (Marcio Pie and a student whose name has somehow slipped my mind - please identify yourself!) independently suggested to me recently that I could use a similar approach to that employed when overlaying a posterior density map from stochastic mapping on a phylomorphospace to project time since the root of the tree onto a phylomorphospace. In theory, this would help overcome the difficulty that all temporal information about the phylogeny is lost when it is projected into morphospace.

Well, this is quite easy** (**that is, easy only because I've already programmed all the pieces in phytools) to do. Here's a hack using simulated data.

First let's load phytools & simulate tree & data:

> require(phytools)
Loading required package: phytools
> packageVersion("phytools")
[1] ‘0.2.93’
> # simulate tree & data
> tree<-pbtree(n=30,scale=100)
> X<-fastBM(tree,nsim=2)
Next, we're going to create an object of class "contMap". This is just a placeholder into which we'll slot our temporal data:
# unfortunately, there's no way to prevent this from plotting
AA<-contMap(tree,X[,1])
Now, we'll swap our color palette (although we don't need to - this is just an arbitrary decision); and then we'll substitute information about the height from the root for our trait data mapped onto the tree:
> AA$cols[]<-rainbow(1001,start=0.7,end=0)
> H<-nodeHeights(tree)
> h<-max(H)
> for(i in 1:nrow(H)) names(AA$tree$maps[[i]])<- round((H[i,1]+cumsum(AA$tree$maps[[i]]))/h*1000)
> # check to verify that temporal information is correct
> plot(AA,legend=FALSE)
Finally, let's plot our phylomorphospace with color as a temporal axis:
> phylomorphospace(AA$tree,X,colors=AA$cols,lwd=3, node.by.map=TRUE,xlab="trait 1",ylab="trait 2")
> add.color.bar(2,AA$cols,title="time from the root", lims=c(0,h),digits=1)
Click where you want to draw the bar
(Click here for full size.)

The final step (add.color.bar(...)) is the biggest hack of all as it only works because I artfully positioned it close enough to the x axis so that the bottom legend text is hidden!

Try it out!

7 comments:

  1. Very useful, thank you Liam! A nice addition could be to change the color vector to reflect the official geologic timescale RGB codes (https://engineering.purdue.edu/Stratigraphy/charts/rgb.html).

    ReplyDelete
    Replies
    1. Lars. The best way to do this in phytools would be to combine make.era.map and phylomorphospace a mapped character. (Actually, this is also a better way to create the plot I just showed.) - Liam

      Delete
    2. Also - Lars, do you happen to have the start & end points (say, in mybp) of each of the named geological time periods and the RGB code in a text or .csv file? I would be very interested in that. Thanks! Liam

      Delete
  2. Have been watching this with interest (although I don't know why I'm interested. So I can have a tree that goes from Darker Green to Less Dark Green? Stupid lower Paleozoic color choices... grumble...). Anyway, I don't have a csv, Liam, but you should be able to grab what you want from the table here pretty easily:

    https://en.wikipedia.org/wiki/Period_%28geology%29

    Wiki's stuff is all based on the newest 2013 timescale.

    ReplyDelete
    Replies
    1. David. I need name,start,end,color. Normally, I would assemble that myself - but it is grant writing time right now, so it'll have to wait until after Aug. 2. - Liam

      Delete
  3. I'm happy to help compiling the data (unless David has already done it?). Thanks for looking into this!

    ReplyDelete
  4. No, I haven't compiled it. I forgot all about this, since I have also been busy working on a proposal, too.

    ReplyDelete

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