Creating accessible charts in R using sgplot

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

19 June 2023

Aims

  • Introduce sgplot

  • Explain why analysts should use sgplot

  • Demonstrate how to use sgplot

  • Offer some general guidance on creating good charts for Scottish Government analysis

sgplot


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

  • It works as an add-on for ggplot2 plots

  • Includes a ggplot2 theme and colour palettes

Why use sgplot

sgplot contributes to our wider priorities:

  • Quality, consistency and trust

  • Accessibility

  • RAP

  • Story-driven publications*

How to use sgplot

Installation

  • 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.

use_sgplot()

library(sgplot)

use_sgplot()
ℹ Default ggplot2 theme set to `theme_sg`.
ℹ Default colours set.
ℹ Default geom aesthetics set.

use_sgplot()

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

life_exp_chart

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

Other improvements

# Existing line chart
life_exp_chart <- 
  life_exp_chart +
  
  # Customise x and y axes
  scale_y_continuous(limits = c(0, 82),
                     breaks = seq(0, 80, 20),
                     expand = c(0, 0)) +
  scale_x_continuous(breaks = seq(1952, 2007, 5)) +
  
  # Add titles and labels
  labs(
    x = "Year",
    y = NULL,
    title = "Living Longer",
    subtitle = "Life Expectancy in the United Kingdom and China 1952-2007",
    caption = "Source: Gapminder",
    colour = NULL
  )

life_exp_chart

Other improvements

A multiple line chart using sgplot theme and main2 colour palette. A title, subtitle and caption are included and formatting has been applied to axis labels and titles.

Labels instead of legend

# Calculate label positions using data
life_exp_labs <- life_exp |>
  group_by(country) |>
  filter(year == max(year)) |>
  ungroup()

life_exp_chart <- life_exp_chart +

  # Remove legend
  guides(colour = "none") +
  
  # Adjust x axis to give extra room for labels  
  scale_x_continuous(limits = c(1952, 2017),
                     breaks = seq(1952, 2017, 5)) +
  
  # Add labels
  geom_label(data = life_exp_labs,
             aes(x = year, y = lifeExp, label = country, colour = country),
             hjust = 0,
             vjust = 0.5,
             nudge_x = 0.5,
             label.size = NA)

life_exp_chart  

Labels instead of legend

A multiple line chart using sgplot theme and main2 colour palette. There is no colour legend. Instead each data line is labelled within the chart.

Apply to individual charts

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

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

Apply to individual charts

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

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

Where to get help and
how to get involved

Package website

https://datasciencescotland.github.io/sgplot

Support and feedback

Future developments

  • Support for interactive chart packages

  • Add Scottish Government colour palettes when available

  • Use Roboto Google font as per Scottish Government design system

  • Anything that would make your life easier!

    • If your team has its own branding style and/or colour palettes, these can be added to sgplot (as long as they’re accessible!)

Get involved

  • Open to anybody

    • Make a suggestion

    • Fix a typo

    • Write a new function

    • Become a package maintainer

  • Contributing guidance

Accessibility

Analysis Function guidance

Important to remember

Story-driven publications

Telling stories with charts

  • Our best practice guidance discusses story-driven publications – charts are very much a part of this

  • Charts should have a clear key message you are trying to communicate

  • This key message should be interesting or notable in some way

  • Analysis Function guidance: If you can’t write down the message your chart is giving, you should think again about the chart you have chosen

Chart titles

  • Most effective way to communicate your message is in the title

  • Analysis Function Guidance recommends two titles for charts

    • A main title that describes the main message of the chart

    • A statistical subtitle which gives details about the data, geography, time period etc.

  • These should be included in the main text, above the image

  • Currently updating our standard Word Template for Statistical Publications to incorporate this

Data visualisation is actually about text

  • John Burn-Murdoch, Financial Times (YouTube)

    • Charts can be extremely effective at communicating information

    • However, to create effective charts you need to understand how people consume charts

    • People consume charts passively, you need to make your message clear and easy to digest

    • Evidence suggests users tend to focus on the title first

FT

A small multiples area chart with minimal style, a main title and statistical title.

BBC

A multiple line chart with minimal style, a main title and statistical title.

Economist

A multiple line chart with minimal style, a main title and statistical title.

Chartr

An area chart with minimal style, a main title and annotations to highlight interesting features.

Guardian

A multiple line chart with minimal style, a main title and statistical title.

ONS

A multiple line chart with minimal style, a main title and statistical title.

ONS - Census 2021

Two horizontal stacked bar charts with minimal style, a main title and statistical title.

Contact us