There are many resources on the use of colours in R, several packages, and a number of schemes already implemented in
ggplot2. In the previous part, we saw how
ggplot2 selects a default colour palette according to the type of variable, discrete or continuous. There are further options, illustrated below:
Choosing colours for a graphic is often some kind of a compromise. One one hand, you want the computer, some algorithm, to choose a sensible colour scheme and pick automatically the required number of colours from this scale. On the other hand, there are always external human preferences that constrain the choices, and are not always easy to formalise.
Some choices, even prevalent in the literature such as the rainbow color scale (also known as Matlab’s flashy colorjet),
are just not good enough. They introduce artefacts, highlight regions of the data that should have a smooth transition with their surroundings, and do not degrade gracefully in black-and-white print, or when viewed by colour-impaired people.
If good colours for scientific graphics are not (entirely) in the eye of the beholder, what are the guides to make the best choice?
The colour choice is not a bad one, but there’s something about the intensity of the colours that makes me want to find a new set of colours somewhat more soothing to my eyes.
and documents his heuristic search for satisfying colours,
I shuffled through many different colours on the Color Hex website, and nothing else seemed to work with me as I wasn’t selecting colours based on any theory
A good discussion is offered in the colorspace package and its accompanying vignettes and papers, e.g. Escaping RGBland: Selecting Colors for Statistical Graphics (pdf)
Despite this omnipresence of color, there is often only little guidance in statistical software packages on how to choose a palette appropriate for a particular visualization task
In this instance, I would argue that the
hcl colour scale of
ggplot2 is a good start for a well-balanced graphic that doesn’t draw the attention to a particular colour. If the colours are too flashy in bar plots (large areas), the saturation and luminosity can easily be muted by tuning the scale,
This basic idea of tuning the HCL colour scale to suit the application was discussed in more depth in Colour for Presentation Graphics (pdf).
Bar plots and maps can also benefit from trying a few different colour palettes from the excellent ColorBrewer website. An interface is provided in
ggplot2 through the
Easily accessed with
scale_colour_brewer(), it is trivial to choose among 35 palettes (see
Sequential palettes, suited to ordered data that progress from low to high. Lightness steps dominate the look of these schemes, with light colors for low data values to dark colors for high data values.
Qualitative palettes, do not imply magnitude differences between legend classes, and hues are used to create the primary visual differences between classes. Qualitative schemes are best suited to representing nominal or categorical data.
Diverging palettes, put equal emphasis on mid-range critical values and extremes at both ends of the data range. The critical class or break in the middle of the legend is emphasized with light colors and low and high extremes are emphasized with dark colors that have contrasting hues.
In the next post, we’ll look at some special cases where the user might want finer control over these scales, or define completely new colour palettes tailored for a specific graphic.
In this series of three posts, we’ll look at colours in R graphics produced with
ggplot2: what are the available choices of colour schemes, and how to choose a colour palette most suitable for a particular graphic?
In kindergarten, choosing a colour was easy, palettes were limited to a few classics. As cool kids grow older and use R, the spectrum expands to present us with overwhelming choice of millions of colours, most of them with poorly defined labels such as
"lavenderblush3". Inasmuch as scientific graphics resemble a paint-by-numbers game, R can help us design more elegant palettes with pertinent colour choices based on the data to display.
Base graphics rely mostly on the
grDevices package for the selection of colours, with a few palettes to choose from:
(some palettes can have many more colours, this image is only an illustration of their structure)
The package also provides a number of basic operations to convert colours (
adjustcolor, col2rgb, make.rgb, rgb2hsv, convertColor) and create interpolating palettes (
rgb, hsv, hcl, gray, colorRamp, colorRampPalette, densCols, gray.colors).
Beyond that, a good resource is the
colorspace package which provides further utilities to convert from one colorspace to another (
HLS, HSV, LAB, LUV, RGB, sRGB, XYZ) and perform various operations on colours.
A special note can be made of a few palette functions, “diverge_hcl”, “diverge_hsv”, “heat_hcl”, “rainbow_hcl”, “sequential_hcl”, “terrain_hcl”, which provide an easy way to produce colour palettes following a particular path in the colour space (varying hue with constant luminosity and saturation, for example).
While the combination of these tools is quite flexible, the user interface becomes a little bit chaotic. More recently, the
scales package has provided wrappers around these functions to provide some consistency in the naming schemes and organise the different categories of palettes in a structured way:
Utilities functions, such as
col2hcl, fullseq, muted, rescale, rescale_mid, rescale_none, rescale_pal, seq_gradient_pal, show_col
Palettes with consistent interface,
brewer_pal, dichromat_pal, gradient_n_pal , div_gradient_pal, hue_pal, grey_pal, identity_pal, manual_pal.
ggplot2 package uses
scales internally, and mirrors this structure. In this first part, we’ll review the basic commands to assign colours in ggplot2.
Let’s consider three plots for illustration:
p1 maps the colour of points to a continuous variable,
p2 maps the fill of bars to a discrete variable, and
p3 maps the fill of tiles to a continuous variable.
Fill and colour scales in ggplot2 can use the same palettes. Some shapes such as lines only accept the colour aesthetic, while others, such as polygons, accept both colour and fill aesthetics. In the latter case, the colour refers to the border of the shape, and the fill to the interior.
Another common source of confusion, general to
ggplot2, is the distinction between set values and mapped values in a layer. Consider the following example,
d = data.frame(x = 1:10, y = rnorm(10), z = gl(5, 2)) a = ggplot(d, aes(x, y, group=z)) grid.arrange(a + geom_path( colour = "red" ), a + geom_path( aes(colour = z )), nrow=1)
The default continuous scale in
ggplot2 is a blue gradient, from
low = "#132B43" to
high = "#56B1F7" which can be reproduced as
scales::seq_gradient_pal(low = "#132B43", high = "#56B1F7", space = "Lab")
The default discrete scale in
ggplot2 is a range of hues from hcl,
scales::hue_pal(h = c(0, 360) + 15, c = 100, l = 65, h.start = 0, direction = 1)
In the next post of this series we’ll describe how one can fine-tune or change altogether these default colours, and, perhaps more importantly, give some pointers on choosing an appropriate colour scheme for a particular graphic.
Next week I’ll present a glimpse of R and ggplot2 graphics at VUW. This is a MESA seminar on ‘Data analysis and plotting with free and open source tools’ where we’ll present spreadsheet alternatives based on gnuplot, Python, and R.