Tuesday, June 10, 2014

Coloring edges in phylomorphospace3d

A recent R-sig-phylo query asked the following about phytools function phylomorphospace3d (for projecting a tree into a three-dimensional morphospace):

"[Is t]here is a possbility to color some specific edges in the phylomorphospace3d()? plot.phylo() has the argument "edge.color" to do that in classical tree plotting. I need it, however, in the above mentioned function."

This functionality (as well as the ability to plot a stochastic character map on a phylomorphospace) exists for two-dimensional phylomorphospace plotting, but it did not (until now) exist for phylomorphospace3d; however, this was not too difficult to add. I have added user control of plotted line colors via the control parameter col.edge (as in phylomorphospace), rather than via a new function argument.

Code for this new function version is here; but users may prefer to install the latest bleeding edge source version of phytools (phytools 0.4-14) to ensure that everything works as advertised.

Here is a demo that plots the internal edges of a 3D phylomorphospace in blue, while the terminal edges are plotted in red:

## load phytools
require(phytools)
packageVersion("phytools") ## should be >=0.4-14
## simulate tree
tree<-pbtree(n=12,tip.label=LETTERS[1:12])
X<-fastBM(tree,nsim=3)
## create color vector
colors<-sapply(tree$edge[,2], function(x,n) if(x>n) "blue"
  else "red",n=length(tree$tip.label))
## generate 3D phylomorphospace plot
phylomorphospace3d(tree,X,control=list(lwd=2,
  col.edge=colors))

BTW, to create this cool .gif, you just need to do something like:

movie3d(phylomorphospace3d(tree,X,control=list(lwd=2,
  col.edge=colors)),movie="phylomorph-3d",duration=10)

The same option is also available for method="static", e.g.:

phylomorphospace3d(tree,X,control=list(lwd=2,
  col.edge=colors),method="static",angle=20)

That's it.

2 comments:

  1. I should have mentioned that to create an animated .gif using movie3d you need to first install ImageMagick. - Liam

    ReplyDelete
  2. Hi Liam,

    I can not figure out how to change color of tips...
    I tried this:

    col.edge <- sapply(simmap.tree$mapped.edge[,2], function (x) if (x>0) "red"
    else "blue")

    phylomorphospace3d (simmap.tree, X = data[,1:3], control = list (ftype = "off", box = F, col.edge = col.edge), color = col.edge)

    The "color" argument should be passed to the "scatterplot3d" function, but it doesn't work.. Am I missed something here, or the "phylomorphospace3d" can only plot black tips?

    Thanks in advance. -Marko

    ReplyDelete