Thursday, May 23, 2013

New version of plotSimmap with type="fan"

Today I posted twice (1, 2) in response to Rafael Maia's early morning query about plotting stochastic mapped trees in a circular style. My earlier posts were about plotting the structure of a circular tree and adding a mapped discrete character using colors.

Well, all that was left was the labels. This is easier said that done. The first challenge was rotating the orientation of the labels to match the angle of their corresponding terminal edge. Here is my code to do that:

# plot labels
for(i in 1:n){
  ii<-which(cw$edge[,2]==i) # find edge
  aa<-Y[ii,2]/(2*pi)*360 # compute angle
  # fix angle & adjust to flip at 90 & 270 deg
  adj<-if(aa>90&&aa<270) c(1,0.5) else c(0,0.5)
  aa<-if(aa>90&&aa<270) 180+aa else aa
  # plot label

The second challenge is making sure that we have a plotting window with enough space for our labels. For this, I stole a trick that I used in the function phenogram and described here.

Code for the new version of plotSimmap is here. I have also posted a new phytools version (phytools 0.2-71). Note that I have not applied all relevant options in plotSimmap to type="fan" yet. This will come in future.

For now - let's check out the version we have. Note that we need to first install the package plotrix.

> require(phytools)
Loading required package: phytools
> packageVersion("phytools")
[1] ‘0.2.71’
> tree<-pbtree(n=50,scale=2)
> Q<-matrix(c(-1,1,1,-1),2,2)
> rownames(Q)<-colnames(Q)<-letters[1:2]
> tree<-sim.history(tree,Q)
> cols<-setNames(c("blue","red"),letters[1:2])
> plotSimmap(tree,cols,type="fan")
Note: type='fan' is in development. Most options not yet available.

A miracle - it works!

1 comment:

  1. well that was quick! :) thanks Liam, I'll give it a try!!