Wednesday, December 7, 2016

Custom color palettes in plotBranchbyTrait

I recently received the following user request:

“I have a small question regarding your phytools function plotBranchbyTrait that I was hoping you perhaps could help with. I am trying to visualize [my data with range 0 to 1] using the heat.color palette. However, as some species have a proportion of 0, and others have a proportion of 1, the branches leading to the latter are almost white and thus impossible to see. If I change the proportion of one of these species to, say 1.2, most of branches become more visible (except the branch leading to that particular species of course). However, this is not an ideal way to go about it. Is there any way I can make sure to only use parts of the color palette for the plot, excluding the brightest, white colors?”

Indeed, this effect can easily be verified using simulation:

library(phytools)
tree<-pbtree(n=100)
x<-fastBM(tree,scale=1)
plotBranchbyTrait(tree,x,"tips","heat.colors",show.tip.label=FALSE)

plot of chunk unnamed-chunk-1

We can see that tips & edges with the most extreme values of the trait essentially vanish.

[Note that plotBranchbyTrait is unusual among phytools functions in that it uses ape::plot.phylo internally instead of plotSimmap in my package - hence the argument show.tip.label=FALSE which would be ftype="off" in most other phytools functions.]

The easiest way to address this was to allow the argument palette to be supplied as a function as well as as a string. I pushed this update to GitHub already & it can be seen here.

Here's an example:

plotBranchbyTrait(tree,x,"tips",
    palette=colorRampPalette(c("red","orange","yellow")),
    show.tip.label=FALSE)

plot of chunk unnamed-chunk-2

We can even write our own custom function, for instance using the original heat.colors palette function internally, but cutting off the last (say) 5% of the spectrum:

foo<-function(n){
    obj<-heat.colors(round(1.05*n))
    obj[1:n]
}
plotBranchbyTrait(tree,x,"tips",palette=foo,show.tip.label=FALSE)

plot of chunk unnamed-chunk-3

Finally, because plotBranchbyTrait uses plot.phylo internally, we can use any of the plot.phylo options, such as:

plotBranchbyTrait(tree,x,"tips",foo,type="fan",show.tip.label=FALSE,
    legend=1.8)

plot of chunk unnamed-chunk-4

plotBranchbyTrait(tree,x,"tips",foo,type="unrooted",show.tip.label=FALSE,
    legend=1.8)

plot of chunk unnamed-chunk-4

and so on.

To get this update you can install phytools from GitHub using the package 'devtools' as follows:

library(devtools)
install_github("liamrevell/phytools")

No comments:

Post a Comment