inverted diverging colour palette
Asked Answered
P

2

9

Quite often I use a diverging colour palette mapped to a parameter that has an interesting turning point somewhere near the middle.

Unfortunately, available diverging palettes -- e.g. those from C. Brewer -- all seem to have saturated colours at the two extremes rather than in the middle, often defined as white or light grey. Here's one example for illustration

enter image description here

The continuous parameter indicated by the text labels passes through an optimum value where the peak of the associated curve reaches a maximum.

On a light white or grey background (typical for ggplot2), the most interesting part of my data becomes almost invisible; I would like it to "pop" more, while values for either side fade gradually away from the centre, with a different tint. Are there any sources of good diverging colour palettes that have a stronger saturation in the middle, and fade on both sides?

Here's a sample code for illustration, picking black as a neutral middle point (I'd prefer something less dramatic, but it's hard to have it blend well with both sides).

require(RColorBrewer)
grid.newpage()
grid.raster(brewer.pal(7,"PRGn"), 0.25, 0.5, 0.4, 1)

custom <- c(brewer.pal(3,"BuPu"), "black", rev(brewer.pal(3,"BuGn")))
grid.raster(custom, 0.75, 0.5, 0.4, 1)

enter image description here

Edit: to clarify, I'm familiar with colorRampPalette and scale_colour_gradientn, I seek advice in

1- choosing good colours for this purpose;

2- defining a colour scale mapping the hand-crafted palette to a variable, similar to what scale_colour_gradient2 does with the mid parameter (the central colour may not be at the exact centre of the parameter range)

Parnell answered 23/5, 2014 at 16:53 Comment(2)
doesn't colorRampPalette give you this? for example par(bg = 'grey97', xpd = NA);plot(1:1000, pch = 19, cex = 5, col = colorRampPalette(c('lightgreen','black','mediumpurple1'))(1000)) And if you want more lighter shades, just repeat the colors: plot(1:1000, pch = 19, cex = 5, col = colorRampPalette(c('lightgreen','lightgreen','black','mediumpurple1','mediumpurple1'))(1000))Cancel
@Cancel I've edited the question with an addendum to clarify the issueParnell
V
11

You might enjoy playing around with choose_palette(), a relatively recent addition to the excellent colorspace package.

The functions in that package give you complete and largely orthogonal control over the hue, chroma, and luminance characteristics of your colorspace. choose_palette() gives you a convenient way to explore the huge space of possibilities that opens up.

Here's the code that'll launch the palette chooser, followed by a screenshot of the GUI tool. (Note that, to make the middle of the range darker than the ends, you'll want to swap the diverging palette's default luminance values, here denoted as "L1" and "L2".)

library(colorspace)
library(grid)

custom <- choose_palette()

enter image description here

And here's an example of what it got me two within about a minute of playing around with it

custom <- diverge_hcl(20, h=c(-225,277), c=80, l=c(80,25))
grid.newpage()
grid.raster(custom, 0.25, 0.5, 0.4, 1)

enter image description here

Both the package and the thinking behind it are nicely documented in the package vignette (vignette("hcl-colors")) and in a companion article "Escaping RGBland: Selecting colors for statistical graphics" (Warning: pdf) published in the journal Computational Statistics and Data Analysis.

Vernissage answered 23/5, 2014 at 17:31 Comment(8)
thanks, that's a nice looking tool. (I wish Rstudio wouldn't die every time something requires tck).Parnell
@Parnell -- That is an unfortunate habit for RStudio to have! On rereading your question, I see now that my answer doesn't really address the most interesting part. Here's hoping that somebody else can point us to professional-quality diverging palettes with darker mid-ranges, like the set of palettes RColorBrewer already gives us.Fantast
The "isoluminant color maps" in Figure 6 in this article (Warning: pdf) look promising, and the discussion immediately following the figure's interesting as well.Fantast
yep, I was looking at this earlier today, but if I'm not mistaken all the examples have desaturated colours (grey-ish) in the middle. It's really hard to find anything that inverts the usual design of diverging scales.Parnell
The more I think about it, the more it just looks like a fundamentally difficult thing to achieve. IMHO, you're definitely not going to get a scale that looks even half as good as the normal diverging scale (even though it may be better to still go ahead and use it in your particular application).Fantast
it is tricky, I agree. I wish there was some interactive tool to draw bezier curves on a specific slice of a colour space (fixing either chroma, hue, luminance, or other better perceptual parameters), and then define a mapping along the curvilinear abscissa.Parnell
@JoshO'Brien How would you generate a palette to be used to color a map and embed it in scall_fill_gradientn call?Phocomelia
@Phocomelia -- Sorry, I'm not a ggplot2 user, so I've got no idea off-hand.Fantast
U
3

One way I have done it in the past to to reverse the diverging ramps (which is close to what you did, but not exactly) but then use the alpha channel to deemphasize the ends of the ramps.

library(ggplot2)
pal <- brewer.pal(7,"PRGn")
custom <- c(rev(pal[1:3]),rev(pal[5:7]))
alpha  <-  c(seq(3),rev(seq(3)))/3
x <- 1:6
y <- rep(1)
MyDF <- data.frame(custom,alpha,x,y)
MyPlot <- ggplot(data = MyDF, aes(x = x, y = y, fill=custom, alpha=alpha)) + geom_bar(stat = "identity", color="white") + 
          scale_fill_identity() + scale_alpha_continuous(range = c(0.6, 1))
MyPlot

enter image description here

The only article I am aware of that discusses flipped color ramps like this is The circular dataimage, a graph for high-resolution circular-spatial data (Morphet & Symanzik, 2010) (no online version, sorry). Basically they alter the value and saturation to make the ends of the ramps merge together - although they were a bit skimpy on the details. Here is an image of one of their graphs.

They had these ramps in the now deprecated package CircSpatial, so you may be able to cull how they made the ramps from there.

Ulla answered 23/5, 2014 at 18:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.