Sunday, December 7, 2014

Inverting the color map on an object of class "contMap" or "densityMap"

In response to a recent user comment I have just posted code to automatically flip or invert the color map in an object of class "contMap" or "densityMap". The function is very simple, as follows:

setMap<-function(x,...){
    if(hasArg(invert)) invert<-list(...)$invert
    else invert<-FALSE
    n<-length(x$cols)
    if(invert) x$cols<-setNames(rev(x$cols),names(x$cols))
    else x$cols[1:n]<-colorRampPalette(...)(n)
    x
}

Here is how it works:

library(phytools)
## Loading required package: ape
## Loading required package: maps
## 
## Attaching package: 'phytools'
## 
## The following object is masked _by_ '.GlobalEnv':
## 
##     setMap
tree<-pbtree(n=26,tip.label=LETTERS)
x<-fastBM(tree)
obj<-contMap(tree,x)

plot of chunk unnamed-chunk-2

plot(setMap(obj,invert=TRUE))

plot of chunk unnamed-chunk-2

Remember, setMap can also be used to change the map arbitrarily, such as in:

obj<-setMap(obj,colors=c("white","black"))
plot(obj)

plot of chunk unnamed-chunk-3

That's it.

3 comments:

  1. Liam,
    Thanks this is a super cool to plot continuous characters.

    Any idea why the "type" argument isn't passed on through the setmap code?

    For instance, in the above:

    obj<-contMap(tree,x,type="fan") - works great produces circular phylogram

    But then:

    plot(setMap(obj,invert=TRUE)) - reverts back to square phylogram (with inverted colors!)

    Actually, any color changes don't seem to be passed on to a type "fan". Any idea whassa going' on?

    Ryan

    ReplyDelete
    Replies
    1. Nevermind, I just realized that you can set all of your parameters after during the "plot" call, I had no idea that "plot" would recognize these:

      plot(setMap(obj,invert=TRUE),type="fan",fsize=0.5,lwd=2)

      COOLIO!

      Delete
    2. Glad to hear you figured it out! - Liam

      Delete