Skip to main content

Section 5.2 Colour

When choosing a colour palette, the first thing to consider is what kind of colour scheme we need. This will depend on the variable we are trying to visualise. Depending on the kind of variable we want to visualise, we might want a qualitative colour scheme (for categorical nominal variables), a sequential colour scheme (for categorical ordinal, or for numeric variables) or a diverging colour scheme (for categorical ordinal, or for numeric variables). For qualitative colour schemes, we want each category (each value for the variable) to have a perceptible difference in colour. For sequential and diverging colour schemes, we will want mappings from data to colour that are not just numerically but also perceptually uniform.
  • sequential scales (also called gradients) go from low to high saturation of a colour.
  • diverging scales represent a scale with a neutral mid-point (as when we are showing temperatures, for instance, or variance in either direction from a zero point or a mean value), where the steps away from the midpoint are perceptually even in both directions.
  • qualitative scales identify as different from each other the different values of your categorical nominal variable.
For your sequential and diverging scales, the goal in each case is to generate a perceptually uniform scheme, where hops from one level to the next are seen as having the same magnitude.
Of course, perceptual uniformity matters for your qualitative scales for your unordered categorical variables as well. We often use colour to represent data for different countries, or political parties, or types of people, and so on. In those cases we want the colours in our qualitative palette to be easily distinguishable, but also have the same valence for the viewer. Unless we are doing it deliberately, we do not want one colour to perceptually dominate the others.
The main message here is that you should generally not put together your colour palettes in an ad hoc way. It is too easy to go astray. In addition to the considerations we have been discussing, we might also want to avoid producing plots that confuse people who are colour-blind. Fortunately for us, almost all of the work has been done already. Different colour spaces have been defined and standardized in ways that account for these uneven or non-linear aspects of human colour perception.
A good resource is colorbrewer. We have come across the work of Cynthia Brewer in Chapter 3. Colorbrewer is a resource developed by Brewer and colleagues in order to help implement good colour practice in data visualisation and cartography ([151]). This site offers many colour schemes we can make use of for our maps, which are easily integrated into R using the RColorBrewer package.
library(RColorBrewer)
Once you have the package loaded, we can look at all the associated palettes with the function display.brewer.all().
display.brewer.all()
A list of names of colour palettes, which are often concatenated abbreviations of shortened colour names or a small descriptor. The names are accompanied by a series of wide rectangles coloured in according to the palette.
Figure 5.2.1. Palettes in RColorBrewer package
The above gives a wide choice of palettes, and while they are applicable to all sorts of data visualisations, they were created especially for the case of thematic maps. We might use the above code to pick a palette we like. We might then want to examine the colours more closely. To do this, we can use the display.brewer.pal() function, and specify n= — the number of colours we need, as well as the palette name with name =:
display.brewer.pal(n = 5, "Spectral")
Five wide rectangles side by side, coloured red, orange, light yellow, green, and blue.
Figure 5.2.2. The "spectral" palette in RColorBrewer package
Let’s go back to our choropleth map of the quantiles of total breath tests per county. We might be interested in this map to show distribution of policing activity, for example. We made this map earlier with the default colour scheme, which didn’t really communicate to us the graduated nature of our data we were visualising. To properly do this, we may imagine using a sequential scale. We can use one of the sequential scales available within RColorBrewer with adding the scale_fill_brewer() function to our ggplot. In this function we can specify the type= parameter, i.e., if we want to use sequential, diverging, or qualitative colour schemes (specified as either "seq" (sequential), "div" (diverging) or "qual" (qualitative)). We can then specify our preferred palette with the palette = argument. Let’s demonstrate here with the "YlOrRd" sequential palette:
ggplot(data = hu_dd) +
  geom_sf(aes(fill = total_quantiles),
          lwd = 0.5,
          col = "white") +
  scale_fill_brewer(type = "seq",   # pick palette type
                    palette = "YlOrRd") +  # specify palette by name
  theme_void()
A previously shown map of counties of Hungary, previously with four bright colours, with shaded counties and a white border between them. Now, the map has a more visually pleasing colour scheme, with colours ranging from yellow to orange to red. The legend to the right, labeled total quantiles, also reflects the new colours.
Figure 5.2.3. Thematic map with "YlOrRd" sequential palette
This looks much better, and communicates our message much more clearly. Is this accessible to our colour-blind colleagues? Earlier, when we asked to view all the palettes with the display.brewer.all() function, we did not specify any arguments. However, we can do so in order to filter only those palettes which are accessible for all audiences. We can include the parameter colorblindFriendly = to do so:
display.brewer.all(colorblindFriendly = TRUE)
Much like before, this is another list of names of colour palettes, which are still concatenated abbreviations of shortened colour names or a small descriptor. The names are yet again accompanied by a series of wide rectangles coloured in according to the palette.
Figure 5.2.4. Colour-blind friendly palettes in RColorBrewer package
You can see that there are a few palettes missing from our earlier results, when we did not specify this requirement. Our recommendation is to always use one of these palettes.
Another way to ensure that we are making accessible maps is to use greyscale (if your map is being printed, this may also save some money). To introduce a greyscale palette, you can use the function scale_fill_grey() from the ggplot2 package:
ggplot(data = hu_dd) +
  geom_sf(aes(fill = total_quantiles), lwd = 0.5, col = "white") +
  scale_fill_grey() + # use greyscale colour scheme for fill
  theme_void()
The previous map of Hungary and its counties, is now shaded in four shades of grey. The legend shows that the darkest grey corresponds to the smallest ’total quantiles’.
Figure 5.2.5. Map with greyscale palette
Sometimes you might prefer such a map. However, do keep in mind: a number of studies have shown the desirability of monochrome colour (over greyscale) thematic maps, as they are linked to less observer variability in interpretation ([8]). So you might want to use something like this instead:
ggplot(data = hu_dd) +
  geom_sf(aes(fill = total_quantiles), lwd = 0.5, col = "white") +
  scale_fill_brewer(type = "seq", palette = "Greens") +
  theme_void()
The same map, now in shades of light green to dark green. The lighter greens corresponds to the smaller ’total quantiles’ here.
Figure 5.2.6. Map with monochrome palette
Overall, the key thing is to be conscious the colours you choose represent your data. Make sure that they are accessible for all audiences, and best represent the patterns in your data which you want to communicate.