Accessible colour palettes in sgplot

Lee Bunce, Office of the Chief Statistician
Alice Byers, Data Innovation Team

11 April 2024

Aims

  • Explain why we created sgplot

  • Introduce sgplot and available colour palettes

  • Demonstrate how to use sgplot

Why we created sgplot

sgplot contributes to the wider priorities of the Statistics group:

  • Accessibility

  • Quality, consistency and trust

  • RAP and use of open-source software

  • Story-driven publications

sgplot


  • sgplot is an R package for creating Scottish Government themed accessible plots

  • It works as an add-on to ggplot2 plots

  • Includes a ggplot2 theme and colour palettes

Scottish Government colour palettes

  • From Design System data visualisation guidance

  • Palettes available for:

    • Categorical data (main and main-extended)

    • Sequential categorical data (sequential)

    • Focus charts (focus)

  • main palette used by default

Other colour palettes

Install the R package

  • On SCOTS, install in the same way you install other R packages

  • Easiest way is to use pkginstaller add-in

  • More guidance available on eRDM

  • Use remotes::install_github() if not working on SCOTS

Get some data

# Load packages
library(ggplot2)
library(dplyr)
library(gapminder)

# Create data set
life_exp <-
  gapminder |>
  select(year, country, lifeExp) |>
  filter(country %in% c("United Kingdom", "China"))

head(life_exp)
# A tibble: 6 × 3
   year country lifeExp
  <int> <fct>     <dbl>
1  1952 China      44  
2  1957 China      50.5
3  1962 China      44.5
4  1967 China      58.4
5  1972 China      63.1
6  1977 China      64.0

ggplot2 defaults

# Create line chart
ggplot(life_exp) +
  geom_line(aes(x = year, y = lifeExp, colour = country))

A multiple line chart using default ggplot2 theme and colours. The background is grey with white grid lines, and the data lines are turquoise and orange.

Apply sgplot theme

library(sgplot)

ggplot(life_exp) +
  geom_line(aes(x = year, y = lifeExp, colour = country)) +
  theme_sg()

A multiple line chart using sgplot theme. The background is white with grey horizontal grid lines. The data lines are turquoise and orange.

Apply sgplot colour palette

ggplot(life_exp) +
  geom_line(aes(x = year, y = lifeExp, colour = country)) +
  theme_sg() +
  scale_colour_discrete_sg()

A multiple line chart using sgplot theme and main colour palette. The background is white with grey horizontal grid lines. The data lines are dark blue and turquoise

Use alternative colour palette

ggplot(life_exp) +
  geom_line(aes(x = year, y = lifeExp, colour = country)) +
  theme_sg() +
  scale_colour_discrete_sg(palette_type = "af")

A multiple line chart using sgplot theme and main colour palette from analysis function palettes. The background is white with grey horizontal grid lines. The data lines are dark blue and orange.

  • The cookbook contains examples of other tweaks you might want to make.

  • Further information on annotations is in the cookbook. :::

Package website

https://scotgovanalysis.github.io/sgplot

Support and feedback

Contact us