[R] Dual colour ramps based on pos/neg values
Achim Zeileis
Achim.Zeileis at uibk.ac.at
Tue May 10 20:25:56 CEST 2011
On Tue, 10 May 2011, Tyler Hayes wrote:
> 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.
Note that despite computing the ramp in LAB space, you still get very
flashy and unbalanced palettes that are hard to decode for color-blind
viewers. The reason is that you start out from very flashy and unbalanced
colors as input values for your color ramps. This is illustrated in the
following:
## your two parts of the diverging palette
lowColFun <- colorRampPalette(
c("#800000","#FF0000","#FF82AB","#FFE4E1"), space = "Lab")
highColFun <- colorRampPalette(
c("#BDFCC9","#7FFF00","#00EE00","#008000"), space = "Lab")
## choose small set of values
cols <- c(lowColFun(4), highColFun(4))
## mapped to dichromatic vision
library("dichromat")
cols1 <- dichromat(cols, "deutan")
cols2 <- dichromat(cols, "protan")
## desaturated version (computed in HCL space)
library("colorspace")
desaturate <- function(x) {
x <- as(hex2RGB(cols), "polarLUV")
x at coords[,2] <- 0
hex(x)
}
cols0 <- desaturate(cols)
## display all four sets of colors
pal <- function(col, border = "light gray") {
n <- length(col)
plot(0, 0, type="n", xlim = c(0, 1), ylim = c(0, 1), axes = FALSE,
xlab = "", ylab = "")
rect(0:(n-1)/n, 0, 1:n/n, 1, col = col, border = border)
}
par(mfrow = c(4, 1), oma = rep(1, 4), mar = rep(0, 4))
pal(cols)
pal(cols0)
pal(cols1)
pal(cols2)
The first panel of the original colors shows that the colors are very
flashy and hence hard to look at for a longer time. The second panel shows
that the colors are unbalanced, i.e., the red colors are darker than the
green colors. And the third and fourth panel shows that the colors will be
hard to decode properly for dichromatic viewers.
The "Escaping RGBland" paper that I posted in my previous reply discusses
how all of these problems can be mitigated. The result is provided in
diverge_hcl() in "colorspace".
> 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
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
More information about the R-help
mailing list