Grammar of Graphics Foundations
Almost every plot you have ever seen in R, no matter how simple or complicated, is built from the same small set of ingredients: some data, a description of how variables map onto visual properties, and a choice of geometry that draws the actual shapes. ggplot2 calls this recipe the grammar of graphics.
In this tutorial you will:
- See why plotting functions that look completely different in base R (
plot(),hist(),barplot(), …) can all be expressed as one consistent recipe inggplot2. - Build your first
ggplot2plot. - Add labels and themes to make a plot presentable.
Throughout, we’ll use the mpg dataset (built into ggplot2), which contains fuel economy data for 234 cars.