Sunday, June 5, 2016

dotTree fix for bug due to plotrix

I recently reported a bug in dotTree that is caused (but for reasons beyond my comprehension) by updating the package plotrix to it's most recently version.

The bug basically causes all the circles plotted at the tips of the tree to merge into a huge smear. Here's what I mean, using some simulated data:

library(phytools)
dotTree(utree,xu)
## Warning in cos(angles) * radius[circle] + x: longer object length is not a
## multiple of shorter object length

plot of chunk unnamed-chunk-1

or, for multiple characters:

dotTree(utree,Xu)
## Warning in cos(angles) * radius[circle] + x: longer object length is not a
## multiple of shorter object length

plot of chunk unnamed-chunk-3

Woah! Here is what a discrete character looks like:

dotTree(tree,y)
## Warning in cos(angles) * radius[circle] + x: longer object length is not a
## multiple of shorter object length

plot of chunk unnamed-chunk-4

Obviously, this is no good - and the recommendation (such as it was) that I have been giving is to just not update plotrix, or to roll it back if you've already updated the package. What I did today, though, was “fixdotTree instead (although I'd contend it was never broken) to work around this feature.

The way I did it was by using mapply to call the plotrix function draw.circle multiply across the elements of several vectors, instead of all at once (which previously worked just fine).

Here's a quick demo. Not that reading the source in & setting phylogram<-phytools:::phylogram should not be necessary if you merely update phytools from GitHub:

library(plotrix) ## get plotrix into name space
phylogram<-phytools:::phylogram
source("https://raw.githubusercontent.com/liamrevell/phytools/master/R/dotTree.R")
dotTree(utree,xu)

plot of chunk unnamed-chunk-5

dotTree(utree,Xu)

plot of chunk unnamed-chunk-5

dotTree(tree,x)

plot of chunk unnamed-chunk-5

dotTree(tree,y)

plot of chunk unnamed-chunk-5

On the very last plot, it looks like the legend is also messed up. This must be because add.simmap.legend also uses draw.circle from plotrix internally. I'll have to check this out.

The data for this demo were simulated as follows:

utree<-pbtree(n=26)
utree$tip.label<-LETTERS[26:1]
xu<-fastBM(utree)
Xu<-fastBM(utree,n=6)

tree<-rtree(n=26)
tree$tip.label<-LETTERS[26:1]
x<-fastBM(tree)

y<-sim.history(tree,matrix(c(-1,1,1,-1),2,2))$states

1 comment:

  1. Already fixed add.simmap.legend so dotTree for data.type="discrete" should be good.

    ReplyDelete