Wednesday, February 8, 2012

New version of phenogram with user control of axis dimensions

An anonymous user request was recently made to add the potential to control the axis dimensions in phenogram. I guessed (correctly) that this would not be too hard, and so I have added this capability to the newest version of the function (direct link to code here).

To accomplish this, I have just added one additional argument:

> args(phenogram)
function (tree, x, fsize = 1, ftype = "reg", colors = NULL, axes = list())
NULL


This is specified as a list containing two components: trait and time. The former should be a vector of length 2 specifying the dimensions of the vertical (trait) axis; and the latter a vector of length 2 giving the desired dimensions on the horizontal (time since the root) axis.

Within the function itself, I have now added the following lines of code (before calling plot.window to create the graph):

plot.new()
if(is.null(axes$trait)) ylim<-c(min(x),max(x))
else ylim<-axes$trait
if(is.null(axes$time))
   xlim<-c(min(H),max(H)+fsize*max(strwidth(tree$tip.label)))
else
   xlim<-c(axes$time[1],axes$time[2]+fsize*max(strwidth(tree$tip.label)))
plot.window(ylim=ylim,xlim=xlim)


This code just uses the vectors provide in axes to specify the dimensions in x & y for the plot - or, if none have been provided, it uses the range of x and nodeHeights(tree) (plus a little bit of extra space to print tip labels).

Let's see how it works:

> source("phenogram.R")
> tree<-pbtree(n=10)
> x<-fastBM(tree)
> phenogram(tree,x) # default settings

> phenogram(tree,x,axes=list(trait=c(-2,2))) # change vertical

> # change horizontal
> phenogram(tree,x,axes=list(trait=c(-2,2), time=c(1.5,max(nodeHeights(tree)))))


It also works with a mapped discrete character. For instance:

> Q<-matrix(c(-2,2,2,-2),2,2)
> mtree<-sim.history(tree,Q)
> cols<-c("blue","red"); names(cols)<-c(1,2)
> phenogram(mtree,x,colors=cols,axes=list(trait=c(-2,2)))


Cool.

1 comment:

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