Facets & Scales
Maya the neighbourhood baker did well enough to open two more branches. She now runs three: Downtown, Riverside, and the new one inside the Airport. Each branch logs the same two numbers every day, the foot traffic (people who walked in) and the revenue (dollars taken). When she draws all three branches on one chart, it turns into a tangle: the Airport branch is so much bigger that the other two flatten into the floor.
The fix is one of the most useful moves in all of data visualization: stop forcing every group onto one panel, and give each group its own little chart. Below is exactly that, the same data as one chart, then split into small multiples. Flip the toggle.
By the end of this lesson you will be able to:
- Say when small multiples beat cramming every group into one panel, and what
facet_wrap(~ var)does - Split a plot into one panel per group with
facet_wrap(), and one panel per combination withfacet_grid() - Choose fixed vs free panel scales without misleading your reader, and bend the axis, guides and legend to your will
Prerequisites: you can build a basic ggplot and map a column to colour, ggplot(data, aes(...)) + geom_*() (from Data Visualization with ggplot2, especially the grammar of graphics). Every new function is defined as it appears.
When one panel has too many stories
Let us put Maya's three branches into this fresh R session. Each lesson starts clean, so the data lives right here on the page, run this block once and the rest of the lesson builds on it:
The instinct is to map branch to colour and draw all 21 points on one panel. Try it and you hit a wall: Airport's revenue runs into the thousands while Riverside barely clears a few hundred, so the shared y-axis stretches to fit Airport and crushes the other two branches into a flat band at the bottom. Worse, the points pile on top of each other (that is overplotting), so even the colours cannot rescue it. The widget below is that crowded one-panel chart, three branches, one squashed scale.