warwickplots is an R package with colour palettes and a ggplot2 theme that are consistent with The University of Warwick’s branding, especially its colours and typography.
The palettes are built using the palettes package. The ggplot2 theme is heavily inspired by the work of Cara Thompson, particularly this talk on applying a unifying aesthetic to your plots.
The package offers six colour palettes: one discrete, two divergent and three sequential:
warwick_palettes
is essentially a list, and so
individual palettes can be accessed by name:1
warwick_palettes$primary
#> <palettes_colour[6]>
#> • #3C1053
#> • #6DCDB8
#> • #CB333B
#> • #F1BE48
#> • #E87722
#> • #00A9CE
Each palette is essentially a vector, so can be subsetted in the usual way to create new palettes:
The palettes package provides a palettes::pal_ramp()
function for interpolating between colours. For example, consider the
warwick_palettes$aubergine
palette:
To generate a new palette of five colour values between the darkest and mid-point of the full palette, and reverse it (then visualise it), we can do the following:
ends <- warwick_palettes$aubergine[c(1, 4)] # get new end-points
pal_ramp(ends, n = 5, direction = -1) |> plot()
For more on methods for working with palettes, see the palettes vignette in the palettes package.
Loading warwickplots also loads the palettes package, which provides colour and fill scales for using the palettes in ggplot2. These are:
scale_colour_palette_d()
scale_colour_palette_c()
scale_colour_palette_c()
scale_fill_palette_d()
scale_fill_palette_c()
scale_fill_palette_c()
The _d
variants are for discrete scales, the
_c
variants are for continuous scales and the
_b
variants are for binned scales.
theme_warwick()
theme_warwick()
is an extension of
ggplot2::theme_minimal()
, offering the following in
addition:
ggtext::element_textbox_simple()
for the plot
title and subtitle, to enable use of markdown and CSS styling, and
text-wrapping if the title/subtitle is longBelow is a plot that uses the warwick_palettes::primary
palette and theme_warwick()
.
library(ggplot2)
library(palmerpenguins)
ggplot(penguins, aes(flipper_length_mm, body_mass_g, group = species)) +
geom_point(aes(colour = species, shape = species), alpha = 0.8, size = 2) +
scale_color_palette_d(warwick_palettes$primary) +
labs(title = "Penguin Size, Palmer Station LTER",
subtitle = "Flipper length and body mass for **<span style = 'color:#3C1053;'>Adelie</span>**, **<span style = 'color:#6DCDB8;'>Chinstrap</span>** and **<span style = 'color:#CB333B;'>Gentoo</span>** Penguins. This is based on data made available in the **palmerpenguins** R package.",
caption = "Visualization: Ella Kaye, Data: Gorman, Williams & Fraser (2014) DOI: 10.1371/journal.pone.009008",
x = "flipper length (mm)",
y = "body mass (g)") +
theme_warwick() +
theme(legend.position = 'none')
Note that any theme_warwick()
defaults can be overridden
with a subsequent call to theme()
.
See the using warwickplots with ggplot2 vignettes for more information and examples and also the using palettes with ggplot2 vignette in the palettes package. The palettes package also has vignettes demonstrating how to use palettes with the gt package and the biscale package.
The University of Warwick’s typography
brand guidance is to use the font Lato for all online text and the
font Avenir Next for all print material. theme_warwick()
has a use
argument, that can be one of
"online"
or "print"
(defaults to
"online"
), which will ensure the appropriate font for the
use, if your system is set up for it, i.e. you have the fonts
installed and the packages to render them in a plot. This section
contains guidance on making sure your system is suitably set-up.
You need to have the ragg, systemfonts, and textshaping packages installed. Installing ragg should install the other two:
You then need to ensure that whenever you output a plot, you do so using the AGG graphics device.
In RStudio >= 1.4, go to Tools -> Global Options -> General -> Graphics and set the Backend to AGG.
In Quarto, set the following YAML:2
knitr:
opts_chunk:
dev: "ragg_png"
knitr::opts_chunk$set(dev = "ragg_png")
at the top of a
script.To use either of the fonts, you first need to check if they are installed on your system:
systemfonts::match_fonts(c("Lato", "Avenir Next"))
#> # A tibble: 2 × 3
#> path index features
#> <chr> <int> <list>
#> 1 /usr/share/fonts/truetype/dejavu/DejaVuSans.ttf 0 <font_ftr>
#> 2 /usr/share/fonts/truetype/dejavu/DejaVuSans.ttf 0 <font_ftr>
If the paths in the tibble show Lato and Avenir Next, then they are installed on your system. If they show another font, i.e. your system default, then they are not. For example, in the environment in which this vignette is rendered, there is access to Lato, but not to Avenir Next. If I tried to set the font family for a plot to Avenir Next, it would display the default DejaVuSans instead.
If you do not have Lato installed, you can install it, either directly from Google Fonts or using the sysfonts package:
For further excellent, detailed guidance on setting up and working with custom fonts in R, see the getting fonts to work in R post by Cara Thompson and the setting up and debugging custom fonts post by June Choe.
In the RStudio IDE and in the online vignette, the print method for palettes shows each bullet point in the relevant colour, but in the vignette built for R, all the bullet points are the same (blue) colour.↩︎
This is already set as the default in warwickpres↩︎