[R] Dual colour ramps based on pos/neg values

Tyler Hayes hayes.tyler at gmail.com
Tue May 10 19:47:04 CEST 2011


My apologies for the late reply but I was out of town for a while. The
solution I wound up using is below. Sorry about the text if it didn't
wrap well. You should be able to pick out the code from the comments
though.

Thanks for all the help!

Cheers,

t.


## ----> Start hack
##
##                *** OLD COLOUR STYLES ***
## r2b <- c("#0571B0", "#92C5DE", "#F7F7F7", "#F4A582", "#CA0020") #red to blue
## r2g <- c("Red", "DarkRed", "Green", "Chartreuse")
## w2b <- c("#045A8D", "#2B8CBE", "#74A9CF", "#BDC9E1", "#F1EEF6")
#white to blue
## assign("col.sty", get(color))
## calendar.pal <- colorRampPalette((col.sty), space = "Lab")
##
##         *** NEW METHOD FOR ZERO CROSSING RAMP SCALES ***
##
## First low colour is the MIN value; second is closest to 0.0
## First high is the closest to 0.0; second is the MAX value.
## If names are not known to the graphics driver, use HEX values
## I tend to put the extremes as DARK and the low values as MUTED/LIGHT colours
lowColFun  <- colorRampPalette(c("#800000","#FF0000","#FF82AB","#FFE4E1"),
space = "Lab")
highColFun <- colorRampPalette(c("#BDFCC9","#7FFF00","#00EE00","#008000"),
space = "Lab")
## These are hard coded, but should be made available to tune
scaleFac   <- 1.001
ncolors    <- 99  # Should be odd for now b/c of tmpXEnd calculations below
## Define middle cutoff values, this could also be arbitrary for
## highlighting specific regions
tmpMid     <- c(-0.0001,0.0001)
## Cuts where to put the colors
tmpLowEnd  <- seq(from=min(values)*scaleFac,to=tmpMid[1]*scaleFac,length=((ncolors-1)/2))
tmpHighEnd <- seq(from=tmpMid[2]*scaleFac,to=max(values)*scaleFac,length=((ncolors-1)/2))
tmpAtVals  <- c(tmpLowEnd,tmpMid,tmpHighEnd)
## Create final values for levelplot
my.at      <- c(tmpLowEnd,tmpMid,tmpHighEnd)
my.col.reg <- c(lowColFun(length(tmpLowEnd)),
rep("black",length(tmpMid)), highColFun(length(tmpHighEnd)) )
my.cuts    <- length(my.col.reg)-1
##
## ----> End hack



More information about the R-help mailing list