The Grammar of Graphics

There are hundreds of named chart types — the bar, the line, the scatter, the bubble, the box, the violin — and learning them one by one, as a zoo of special cases, is a miserable way to live. In 1999 Leland Wilkinson proposed something far better in his book The Grammar of Graphics: don't memorise charts, learn the grammar that generates them. Just as a handful of grammatical rules produces endless sentences, a handful of graphical components produces endless charts. Hadley Wickham later turned this grammar into the wildly influential ggplot2, and its vocabulary now underlies visualisation everywhere.

The core idea: a chart is not a type, it is a recipe of composable layers. Change one layer and you get a different chart from the same data, deliberately rather than by rummaging through a menu.

The layers of the grammar

Every statistical graphic is built by stacking a small set of independent components:

LayerWhat it doesExample
DataThe tidy table you are drawing.One row per country-year
Aesthetic mappingsWhich variable drives which visual channel: position, colour, size, shape.x = income, y = lifespan, colour = continent, size = population
Geoms (geometries)The marks that represent the rows.point, line, bar, boxplot
ScalesHow data values become pixels, colours, sizes; axes and legends.log x-axis, a colour palette
FacetsSplit the data into a grid of small multiples.one panel per decade
Stats & coordinatesTransform data before drawing; choose the coordinate system.bin into a histogram; polar coords

The power is in the independence. A histogram and a bar chart share a geom (the bar) but differ in their stat (binning vs none); a scatter and a bubble chart share everything except that the bubble adds a size aesthetic. You are no longer choosing chart types — you are choosing mappings.

Aesthetic mappings in one picture

A scatter plot already uses two aesthetics — x and y position. The grammar lets you keep adding channels to show more variables on the same marks. Below, the same points also map a categorical variable to colour and a third numeric variable to size — four variables shown on one geom, just by adding aesthetic layers:

Read it as a sentence in the grammar: "map income to x, lifespan to y, region to colour, population to size, and draw the rows as points." That sentence is the chart. Swap "points" for "text" and you have a labelled plot; add a facet on region and the one panel becomes several. Nothing else changes.

Choosing the right geom for the question

The grammar frees you to choose, so choose on purpose. The geom should match the question, not your habit:

QuestionNatural geom
How is one variable distributed?histogram / density
How do two numeric variables relate?point (scatter)
How does a value change over time?line
How do a few categories compare?bar
How does a distribution differ across groups?boxplot / violin

Before the grammar of graphics, plotting software worked like a vending machine: press the "bar chart" button, get a bar chart, and pray it was what you wanted. Wilkinson's insight was that this hid the real structure. His grammar re-cast every chart as a formal composition of data, mappings, geoms, scales and facets — an idea so clean that Wickham's ggplot2 implementation became one of the most-used tools in all of statistics, and its "map variables to aesthetics" mental model was copied by Python's plotnine and Altair, by Tableau's "show me," and by the Vega-Lite grammar behind countless web charts. When you hear a data scientist say "map colour to species," they are speaking Wilkinson's grammar, usually without knowing whose it was.

A composable grammar makes it just as easy to build a dishonest chart as an honest one. Three classics to police in your own work:

The grammar gives you power; these are the responsibilities that come with it.

Learn this on Kaggle

Kaggle Learn's free Data Visualization course has you build charts layer by layer — mapping variables to colour and shape, choosing the geom that fits the question, and avoiding the misleading-axis traps above — which is the grammar of graphics put straight into your hands.