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
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)
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)
Diverging Color Schemes A typical diverging scheme pairs sequential schemes based on two different hues so that they diverge from a shared light color, for the critical midpoint, toward dark colors of different hues at each extreme.
Tableau has two ways of coloring continuous measures: sequential and diverging gradients. The common understanding is that sequential palettes vary the intensity of one color while diverging palettes use two contrasting colors.
2 Use diverging shades to emphasize the extremes “Sequential tells a different story.” If your story emphasizes the highest (=darkest) values, go for a sequential color scale. If your story is about the lowest and highest values, go for a diverging scale.
A diverging palette shows two ranges of values using color intensity to show the magnitude of the number and the actual color to show which range the number is from. Diverging palettes are most commonly used to show the difference between positive and negative numbers.
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()
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)
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With