Tuesday, November 26, 2013

Bug fix for phenogram(...,spread.labels=TRUE) for when labels don't actually overlap

I just discovered a small bug in phenogram (the phytools function for plotting traitgrams in various ways) which causes phenogram(...,spread.labels=TRUE) to fail when labels actually don't overlap. So, for instance:

> library(phytools)
> tree<-pbtree(n=10,tip.label=LETTERS[10:1])
> ## no problem
> phenogram(tree,setNames(1:10,tree$tip.label))
> ## problem
> phenogram(tree,setNames(1:10,tree$tip.label), spread.labels=TRUE)
Error in optim(zz, ff, yy = yy, mo = mo, ms = ms, cost = cost, method = "L-BFGS-B", :
L-BFGS-B needs finite values of 'fn'

The bug is because if the labels do not overlap at all when we start our optimization to minimize overlap, the objective function becomes undefined. The fix just checks to ensure that there is some overlap, and if not spits back the original vertical positions of the tip labels.

> source("http://www.phytools.org/phenogram/v1.1/phenogram.R")
> ## fixed
> phenogram(tree,setNames(1:10,tree$tip.label), spread.labels=TRUE)

I also changed the default spread.cost, which controls how much to penalize overlap & deviation from the vertical position, x, respectively; although this is still totally under user control.

3 comments:

  1. Hi Liam,
    is there any chance you could post up an example on the Phytools blogspot of using phenogram, with 95% confidence intervals for the character state recon, and the 'spread labels' option (i.e. something like Figure 4 in your MEE article). I can get this to work without the 95% confidence intervals plotted, but as soon as I try to add them, the spread labels option doesn't seem to work. Any help much appreciated.
    cheers

    ReplyDelete
    Replies
    1. Hi. If you have a fairly recent version of phytools, then you should be able to run the following command:

      fancyTree(tree,x=x,type="phenogram95")

      (in which x is a vector of trait values with names(x) containing the tip labels in the tree) to reproduce Figure 4 from the MEE paper. You can also turn on or off spread.labels=TRUE/FALSE or adjust spread.cost (a vector indicating how overlap & vertical deviation are penalized.

      Let me know if this works for you.

      All the best, Liam

      Delete
    2. Hi Liam,
      thanks, that worked beautifully. Appreciate the help.

      Delete

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