ggplot25 March 2026
Aims:
ggplot2During this session:
Session will be demonstrated in RStudio.
Each week you can access the data (and a README file) on GitHub.
We’ll be using a dataset from 2023 for these examples.
Load data using the tidytuesdayR package
Where do cats go?
How fast do cats run?
How long do they spend indoors?
Does that change as they get older?
Don’t jump straight into writing code for the final chart yet!
The number of cats in each unique combination of hrs_indoors and age_years.
We only need to use the cats_uk_reference data.
Is all of the data in the format that we expect?
Data wrangling
Initial draft of a plot
Have you ever spent ages tinkering with a plot you’re previewing in RStudio…
…and then used ggsave() to save an image, and ended up with something like this:
This happens because we preview the chart at 96dpi (low resolution) in RStudio, but save the chart at 300dpi (high resolution) using the defaults for ggsave().
We could set ggsave(dpi = 96) but we usually don’t want to save a low resolution image.
Instead we want to preview the chart in RStudio at the higher resolution we’ll be saving it at.
ggview: github.com/idmn/ggview
Preview the ggplot2 output directly with your specifications in RStudio IDE - you’ll get what you see
Use ggview to preview your plots at the desired size and resolution by adding the following to the end of your ggplot2 call:
ggplot2How do we choose good colours?
Appropriate palette types: colorbrewer2.org
Colour associations: data.europa.eu/apps/data-visualisation-guide/colour-connotations
Check palette accessibility: github.com/clauswilke/colorblindr
Palettes in R: emilhvitfeldt.github.io/paletteer
Create variable for colours
Update basic chart
ragg and systemfontsshowtextLoading fonts
For journal papers, you might want to use something similar to the font used for text the journal.
We could type out text for the annotations such as:
But this means that:
There are different ways to combine variables with text in R.
paste() / paste0()sprintf()glue()Making data-driven text with glue()
Adding annotation to chart
There are several arguments in labs() that we can use to add text to our chart:
Create text variables
title <- "Do older cats spend more time indoors?"
perc_indoor <- round(100 * sum(cats_uk_reference$hrs_indoors == "22.5") / nrow(cats_uk_reference))
st <- glue("Around {perc_indoor}% of cats in the study spend on average 22.5 hours per day indoors! There is a slight trend for cats to spend more time indoors as they age.")
cap <- "Data: McDonald JL, Cole H. 2020 | Graphic: Nicola Rennie"Add text to chart
What about the fonts we chose?
Where’s the subtitle text gone?!
The legend takes up a lot of space
That grey background…
Apply default font and size
Further styling
theme_plot2 <- theme_plot1 +
theme(
# legend styling
legend.position = "inside",
legend.position.inside = c(0.9, 0.25),
legend.background = element_rect(fill = alpha(bg_col, 0.6), color = text_col),
# text
text = element_text(color = text_col),
plot.title = element_text(family = title_font, face = "bold", size = rel(1.5)),
plot.subtitle = element_textbox_simple(),
plot.caption = element_textbox_simple(),
plot.title.position = "plot",
plot.caption.position = "plot",
# background and grid
panel.grid.minor = element_blank(),
plot.background = element_rect(fill = bg_col, color = bg_col)
)Save as an image
The legend box and the annotation box currently look quite different.
If we had joined the GPS data, we could have perhaps made a more informative plot showing the relationship between age and activity level.
Add more annotations and arrows pointing to the relevant data point.
Join the cats and the cats_uk_reference datasets using the tag_id column.
Instead of plotting the time spent indoors on the y-axis, plot the cat’s average ground speed (ignoring that the values are very unusual!)
Add another annotation that highlights the cat with the highest average speed.
The Art of Data Visualization with ggplot2: nrennie.rbind.io/art-of-viz
ONS Data Visualisation Guidance: service-manual.ons.gov.uk/data-visualisation
RSS Best Practices for Data Visualisation Guidance: rss.org.uk/datavisguide
FT Visual Vocabulary: ft-interactive.github.io/visual-vocabulary
Data visualisation resources: nrennie.rbind.io/data-viz-resources
Questions?