Writing

A ggplot2 chart, deconstructed layer by layer

How I would explain ggplot2 in 2026.

Published
Tags
R, ggplot2, dataviz

This slide deck showcases how I would explain ggplot2 today. Just take one chart and take it visually apart, line by line. Each + adds a layer, and the chart is those layers stacked: basically the central concept behind the grammar of graphics.

This example goes from a CSV file to a publication-ready figure.

Open the slides full screen (arrow keys or space to step through)

What the deck shows

The data is only there to have something to plot. European Social Survey, years of education and left-right self-placement for 39,514 respondents (CSV).

  1. Read the file with read_csv() and look at it with glimpse().
  2. Clean it: the survey codes missing answers as 77, 88 and 99, which mutate() and if_else() turn into NA. Then check the range.
  3. Model left-right position on education with lm(), and read the coefficients and their confidence intervals with parameters::model_parameters().
  4. Plot, deconstructed. Each card is one block of code: the mapping sets up the axes, stat_summary() adds the mean per year of education, geom_smooth() the regression line, scale_x_continuous() the tick labels, labs() the axis titles and theme_minimal() the grid. The lines of code and the card they draw light up together.
  5. Make it publication ready, one change at a time, with the changed lines highlighted: grey dots so the data sits in the background, a 95% confidence interval on each mean, a black line for the result, only horizontal grid lines, and the full 0 to 10 scale with a baseline at 0. The last step is the one that matters most for reading the chart: on the whole scale, the drop of 0.7 points over 20 years of education looks as small as it is.
  6. Export at the size it will be printed, as a PDF with the fonts embedded.

Credit and how it’s built

The animation follows the ggplot2-deconstructed style from Emil Hvitfeldt’s tidy-animations.

Here the cards are drawn in R. Each one is the same ggplot with every other part made transparent, so all cards share the exact same layout and line up when they’re stacked. R saves them as transparent PNGs, reveal.js fragments step through the stages, and anime.js does the motion. The deck is a Quarto file in the same folder as this post, rendered to one self-contained HTML file.