Themes & Accessibility
In Lesson 1, Maya the baker had three branches, Downtown, Riverside and the big Airport shop, and you split her crowded chart into small multiples with facets, then bent the scales so every branch was readable. The numbers were finally clear.
Now she wants the chart to look the part: clean, on-brand, and, because one of her investors is colourblind, readable by everyone in the room. None of that means changing a single data point. It means changing the theme (the fonts, gridlines and background) and the colours. Below is exactly that, the same bar chart, restyled live. Flip the Palette and Theme controls and watch the look change while the bars keep their values.
By the end of this lesson you will be able to:
- Restyle a whole plot with a built-in
theme_*()and explain that a theme changes appearance, never the data - Pick the right colour scale for the data type: a discrete scale for categories, a continuous scale for a number
- Choose a colourblind-safe palette (viridis or Okabe-Ito) and never rely on hue alone
Prerequisites: you can build a basic ggplot and map a column to colour, ggplot(data, aes(...)) + geom_*() (from Data Visualization with ggplot2 and Lesson 1 of this course, Facets and Scales). Every new function is defined as it appears.
The data, and swapping the whole look with a theme
Each lesson starts in a fresh R session, so let us put Maya's three branches back on the page. Run this block once and the rest of the lesson builds on it:
Every ggplot is two separate things stacked together. There is the data ink, the bars, points and lines that encode your numbers, and there is everything else: the grey background, the gridlines, the tick labels, the legend box, the fonts. That second part, the non-data furniture, is what ggplot2 calls the theme. The whole point of this lesson is that you can completely change the theme and the colours and the data ink stays exactly where it was. Styling is reversible decoration; it never edits a value.
ggplot2's default look, the grey panel with white gridlines, is fine for exploring but rarely what you want in a report. You change the theme by adding a theme function as one more layer with +, exactly like you add a geom. Each one is a complete, pre-built look:
theme_minimal()strips the panel border and background for a clean, modern feeltheme_bw()keeps gridlines but on white, good for printtheme_classic()gives you bare axis lines and no gridlines, a traditional looktheme_void()removes nearly everything, for maps and standalone graphics
Here is Maya's daily revenue for one branch, drawn once with the default theme and once with theme_minimal(). Only the layer at the end changes:
The bars are identical; the background, gridlines and spacing are what moved. The widget below makes that tangible across all three branches: switch the Theme control and watch the panel restyle while the bars hold steady. (Ignore the Palette control for one more step.)
theme_*() layer restyles the non-data parts of a plot in one move. It changes how the chart looks, never what it says: the same data drawn under theme_void() and theme_bw() reports identical numbers.