Making a colour palette pseudo-package in R

Published
2020-04-16
Tagged

Late last year, I got sick. Stuck at home, I had nothing to do, and I decided to amuse myself by making a colour palette package in the R programming language. The goal of this package was to make it as easy as possible to use a pre-defined colour palette in data visualisation.

While I tend not to use this in its package form in my day-to-day, a lot of the functions I go into in this article get copied-and-pasted from project to project. One of these days I’ll have enough time to package them up properly and get them into our local library, and then I’ll never have to type a hex code in ever again.

While making a package is tricky, making a set of functions in one file which you port from project to project is an admirable stop-gap. So in the interest of sharing knowledge, this is what I’ve been doing.

First, catch your palette

Designing a good colour palette is almost as much work as coding the framework around it. A good data viz colour palette makes its individual elements distinguishable, from the background and from each other, even when the viewer is colour-blind or the visualisation is printed in greyscale. VizPalette is a great tool to determine how your colour palette looks for those with colour-blindness, while contrast-ratio allows you to quantify contrast ratios.

I’m not going to go into that kind of detail in this post - instead, I’m going to grab some colours from this nice “flat UI” colour palette site:

#eb3b5a #fa8231 #f7b731 #20bf6b #0fb9b1 #2d98da #3867d6 #8854d0

I haven’t tested this palette for colour-blindness at all, but it’ll do for our examples below.

The basics

The first thing we need to do is set these colours up as a vector. My preferred method is to set them up as a named vector, and then access them through an accessor function. While we’re here, let’s give us a nice “namespace” for these functions - if you’re doing this for a company, you might want to put the company name in all the functions, to show that they’re part of one set of functions. Let’s make these part of Acme Corporation, objectively the best generic company name.

1
.acme_colours <- c(
2
  red = "#eb3b5a",
3
  orange = "#fa8231",
4
  yellow = "#f7b731",
5
  green = "#20bf6b",
6
  topaz = "#0fb9b1",
7
  light_blue = "#2d98da",
8
  dark_blue = "#3867d6",
9
  purple = "#8854d0"
10
)
11
12
# This function takes a character or integer index
13
acme_colours <- function(index = NULL, named = FALSE) {
14
  # Default to everything
15
  if (is.null(index)) {
16
    index <- names(.acme_colours)
17
  }
18
19
  # This works with integer or character values
20
  return_value <- .acme_colours[index]
21
22
  if (!named) {
23
    names(return_value) <- NULL
24
  }
25
26
  return(return_value)
27
}
28
29
# Another convenience function
30
acme_colour_names <- function() {
31
  names(.acme_colours)
32
}

Starting .acme_colours with a period doesn’t do anything magical: it’s just a kind of indicator that we’re not supposed to use the value directly. If we were making a package, we could store this in the innards of the package proper, so it’s never visible to the user. For this kind of work, though, hiding behind a full-stop is fine.

At this stage you may think we’re almost done - after all, you could easily sub this in to your favourite ggplot code as follows:

1
ggplot(mpg) +
2
  geom_point(aes(x = displ, y = hwy, colour = as.character(cyl))) +
3
  scale_colour_manual(values = acme_colours()) +
4
  theme_minimal()

However, R’s default strategy for picking colours is to work its way through the list. This means that even though we have the whole rainbow to pick from, R will just start from the first option and plod its way through, often stopping before we get to the blues and purples:

All the colours of the rainbow. Well, yellow.

We could rearrange our colour vector so we pick colours in a nicer order, but we can work smarter as well, with palettes.

Running the gamut

ggplot uses palette functions to pass off colour selection logic. There’s nothing that really makes a palette function a palette function - it doesn’t need a special class or anything. That means we can make our own palette functions, if we want.

There are two broad categories of palette, depending on whether we’re looking at a discrete or a continuous scale. We’ll start by examining how R generates discrete colour scales.

A discrete scale is a scale where we map a finite (often small) series of set categories to colours. When you tell R it needs to plot a discrete colour scale with a set of colours, it’ll create a palette function that takes one argument (the number of colours you need) and returns a vector of colours. Actually, the palette function is a factory function (i.e. a function that returns functions) - this is a bit complex, but it means you can set up one function to do all this work and call it over and over again.

Replace…

We can mimic the standard R behaviour - and, in turn, to make that code above read more nicely - pretty easily:

1
# Our new palette function.
2
acme_palette <- function() {
3
4
  acme_colour_length <- length(acme_colours())
5
6
  function(n) {
7
    stopifnot(n <= acme_colour_length)
8
    return(acme_colours(1:n))
9
  }
10
}
11
12
# This is the function we use when plotting
13
scale_colour_acme <- function(...) {
14
    ggplot2::discrete_scale(
15
      aesthetics = "colour",
16
      scale_name = "acme",
17
      palette = acme_palette(),
18
      ...
19
    )
20
}

Now we don’t have to use scale_colour_manual() everywhere - our code above can be rewritten as:

1
ggplot(mpg) +
2
  geom_point(aes(x = displ, y = hwy, colour = as.character(cyl))) +
3
  scale_colour_acme() +
4
  theme_minimal()

…and improve

But the reason we’re doing this is to pick better colours! There’s a couple of ways we could go about this.

First, let’s see how we can automate the colour selection process. We’re going to build a little script that will automatically pick the colours furthest from each other - so if we know we need to map three categories, it’ll take our basic red colour, something greenish, and something near the blue end of the spectrum:

1
acme_palette <- function() {
2
  acme_colour_length <- length(acme_colours())
3
4
  function(n) {
5
    stopifnot(n <= acme_colour_length)
6
7
    # Shortcut: if n = 1, we can just return the first colour
8
    if (n == 1) {
9
      return(acme_colours(1))
10
    }
11
12
    # Pick additional colours. Make them as spread out as possible
13
    interval_between_picks <- acme_colour_length / n
14
15
    additional_colour_indices <- 1 + (1:(n-1)) * interval_between_picks
16
17
    # Work out which colours to return
18
    colour_indices <- c(1, round(additional_colour_indices))
19
20
    return(acme_colours(colour_indices))
21
  }
22
}

Let’s see how this looks:

1
ggplot(mpg) +
2
  geom_point(aes(x = displ, y = hwy, colour = as.character(cyl))) +
3
  scale_colour_acme() +
4
  theme_minimal()

Plotted with automatic colour picking.

However, you may have Opinions about which colours to use in conjunction. You might have three “primary” colours you want to use in where possible, even if they’re right next to each other on the spectrum. Or you could alter things so that when you have lots of colours, you pick alternating red-blue-yellow-purple for maximum contrast. Whatever your motivation, you can manually set the order of colours on a case-by-case basis, as follows:

1
acme_palette <- function() {
2
3
  acme_colour_length <- length(acme_colours())
4
5
  function(n) {
6
    stopifnot(n <= acme_colour_length)
7
8
    colour_indices <- 
9
      if (n == 1) { "red" }
10
      else if (n == 2) { c("red", "blue") }
11
      else if (n == 3) { c("red", "green", "blue") }
12
      else if (n == 4) { c("red", "green", "orange", "blue") }
13
      # ... etc. etc.
14
      else if (n == 8) {
15
        c(
16
          "red", "topaz", "orange", "blue",
17
          "yellow", "dark_blue", "green", "purple"
18
        )
19
      }
20
21
    return(acme_colours(colour_indices))
22
  }
23
}

While this is a lot of work, it gives you ultimate authorial control over which colours to pick. As a bonus, it lets you line contrasting colours up against one another, as we’ve done with the green nestled in between the red and orange in this plot:

Plotted with “manual” colour picking.

To see the real effect of those contrasting colours, check out something a bit more complex:

For maximum colour contrast

This example hurts your eyes, but there’s no way you’ll miss the boundary between two categories. As a bonus, this contrast may well help you if the plot is printed out in greyscale, where differences between colours are even harder to distinguish.

Continuous scales

We’ve dealt with discrete scales - that is, scales where we have a predefined number of categories, which we can map to discrete colours. What about continuous scales - that is, scales where we want to map a continuous range to a spectrum? To do this, we need to build a different kind of palette.

Unlike discrete colour palettes (which are passed a numeric vector, length 1, representing the number of categories required), continuous colour palettes can expect to receive a vector of variable length, where every value is between 0 and 1 (where 0 is the minimum value, and 1 is the maximum), representing all the values we need to plot on the plot itself.

We can offload most of the hard work here onto ggplot2, and its dependency, colorspace:1

1
scale_colour_acme_c <- function(index = 1, colour_range = 0.75, ...) {
2
  low_colour <- acme_colours(index)
3
  high_colour <- colorspace::lighten(low_colour, amount = colour_range)
4
5
  ggplot2::scale_colour_gradient(
6
    low = low_colour,
7
    high = high_colour,
8
    ...
9
  )
10
}

This will give us a nice range of colours from our colour of choice, to white. Here’s an example of it in use:

1
ggplot(mpg) +
2
  geom_point(aes(x = displ, y = hwy, colour = cty)) +
3
  scale_colour_acme_c() +
4
  theme_minimal()

A sequential colour palette

There’s another type of continuous scale we might encounter, however: the diverging sequence. This is when you want to have a central value (often zero) coded as something neutral - white or grey - while both positive and negative extremes are coded different colours. Thankfully, ggplot2 is here once again to do the heavy lifting.

1
scale_colour_acme_div <- function(high_index = 1, low_index = 5, ...) {
2
  high_colour <- acme_colours(high_index)
3
  low_colour <- acme_colours(low_index)
4
5
  ggplot2::scale_colour_gradient2(
6
    low = low_colour,
7
    high = high_colour,
8
    ...
9
  )
10
}

Let’s use this in an example: here we’re going to look at the fuel efficiency of all 2008-model Toyota cars in the mpg dataset, and use the diverging scale around the mean efficiency:

1
toyota_cars_2008 <-
2
  filter(mpg, manufacturer == "toyota", year == 2008)
3
4
mean_hwy <- mean(toyota_cars_2008$hwy)
5
6
ggplot(toyota_cars_2008) +
7
  geom_vline(xintercept = mean_hwy, linetype = 2) +
8
  geom_point(aes(y = model, x = hwy, colour = hwy)) +
9
  scale_colour_acme_div(midpoint = mean_hwy) +
10
  theme_minimal()

A diverging colour palette

Summing up

In this article, I’ve gone through:

  • How to set up your own custom colour palette
  • How to build your own palette, either using automatic or manual colour-picking
  • How to use that to build your own scale functions

In my next article2 I’ll show off some more advanced mucking about with ggplot‘s scale functions to let us do even more with even less.

The examples I’ve shown in this article were all generated through R. You can grab the script and data I used in the above examples, right here:

Additional links

Some additional reading that went into this post.


  1. Note I’m calling this function scale_colour_acme_c, to mark it as a continuous scale. 

  2. I say this with some kind of certainty that I’ll write a follow-up to this article, certainty which definitely shouldn’t exist.