Lee Bunce, Office of the Chief Statistician
Alice Byers, Data Innovation Team
19 June 2023
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 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
sgplot contributes to our wider priorities:
Quality, consistency and trust
Accessibility
RAP
Story-driven publications*
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
# 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
ℹ Default ggplot2 theme set to `theme_sg`.
ℹ Default colours set.
ℹ Default geom aesthetics set.
# 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
# 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
https://datasciencescotland.github.io/sgplot
Cookbook; lots of example code
Reference; help files for sgplot functions
Further guidance on accessibility, colours and saving charts
Guidance on how to ask for help or contribute
Open an issue on GitHub if you’ve found a bug
Please let us know if you’re using sgplot - we would love to see examples of publications using sgplot
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!
Open to anybody
Make a suggestion
Fix a typo
Write a new function
Become a package maintainer
Using sgplot does not guarantee accessibility
sgplot should be used in conjunction with Analysis Function data visualisation guidance
Their e-learning is a good place to start
Review the charts checklist before sharing or publishing your chart
sgplot package website has details of other things to consider for accessibility
Save charts in SVG image format
Include titles, subtitles and captions in the main body of the page (e.g. in html or Word document) rather than embedding within the chart image
Provide alternative text
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
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
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
Source: Chartr: Data Storytelling
Lee Bunce, Office of the Chief Statistician (OCS)
Alice Byers, Data Innovation Team
Creating accessible charts in R using sgplot