Thursday, December 4, 2014

User control of the vertical range of tip labels in phenogram

A phytools user asked the following:

“I am plotting a phenogram (aka traitgram) of my tree, but it has many tips and my labels are overlapping. I have tried spread.labels=TRUE and spread.cost=c(1,0) and changed fsize, but some labels still overlap. I saw that the internal function spreadlabels sets the range for the spread labels to the range of my trait. Is it possible to expand that range to accommodate a large tree with many overlapping labels.”

The answer is both yes & no. Yes, it is possible to allow user control of the vertical range of tip labels - and I have coded this up and posted it here, as well as in a new non-CRAN phytools version (phytools 0.4-41). Unfortunately, it is also no in that this will usually not solve the problem that the user has identified. This is because the vertical range for tip labels is ultimately limited not by the scale of the plot - but by the size of the plotting device. The plotting device is roughly analogous to the “piece of paper” we are drawing our plot on. We can rescale our y-axis all we like, but ultimately, to have more space for our labels, we're going to need a bigger piece of paper!

There are some conditions, for instance when ancestral states are known, that drawing the tip labels on a broader range of y than the extant species values would be advantageous, so I have nonetheless added the feature. It also may help a bit if we just need a small amount of additional vertical space to fit out labels. However it is not a panacea for the problem of overlapping tip labels.

Here's a quick demo that illustrates what I mean:

library(phytools)
set.seed(1)
tree<-pbtree(n=40)
## realistic tip labels
tree$tip.label<-replicate(Ntip(tree),paste(sample(LETTERS,1),". ",
    paste(sample(letters,round(runif(1,3,8))),collapse=""),sep=""))
x<-fastBM(tree)
## original range
phenogram(tree,x,spread.labels=TRUE,spread.cost=c(1,0),ftype="i")

plot of chunk unnamed-chunk-1

## 'stretched' range
phenogram(tree,x,spread.labels=TRUE,spread.cost=c(1,0),ftype="i",
    spread.range=c(-5,5),ylim=c(-5.1,5.1))

plot of chunk unnamed-chunk-1

So you can see that although I have “expanded” the vertical space over which tip labels are written, without a bigger plotting device, my tip labels are equally overlapping.

Now let's use a bigger piece of paper:

phenogram(tree,x,spread.labels=TRUE,spread.cost=c(1,0),ftype="i")

plot of chunk unnamed-chunk-2

Much better! That's it.

No comments:

Post a Comment