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).
- Read the file with
read_csv()and look at it withglimpse(). - Clean it: the survey codes missing answers as 77, 88 and 99, which
mutate()andif_else()turn intoNA. Then check the range. - Model left-right position on education with
lm(), and read the coefficients and their confidence intervals withparameters::model_parameters(). - 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 andtheme_minimal()the grid. The lines of code and the card they draw light up together. - 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.
- 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.