EDS 221 Day 6 PM
Grammar of graphics
August 17th, 2026
The grammar of graphics
ggplot2 builds a figure by layering independent pieces on top of each other, instead of picking one big “chart type.”
data
The data frame you’re plotting.
mapping
Which columns control which visual properties (aes()).
geometry
The shape used to represent the data (geom_*()).
scale
How the mapping shows up on the figure (scale_*()).
Geometries: start with data
ggplot() sets up a plot for a data frame — with nothing mapped yet, there’s nothing to draw.
Geometries: add a mapping
aes() maps columns to axes. This sets up the grid, but still no geometry has been added.
Geometries: add a geometry
A geom_*() layer draws the mapped data. geom_point() draws one point per row.
Geometries: choosing a geometry
The geometry you choose depends on the kind of relationship you want to show.
Geometries: comparing groups
A numeric variable by a categorical variable → geom_boxplot() shows the distribution within each group.
Aesthetics
aes() isn’t just for x and y — it can map columns to other visual properties too.
x, y — positioncolor — outline / line colorfill — interior colorshape — point symbolAesthetics: color vs. fill
Aesthetics: shape
shape gives each category of a point geometry a different symbol — useful when color alone isn’t enough (e.g., grayscale printing).
Aesthetics: combining color and shape
Mapping species to both shape and color encodes the same grouping two ways, making it easier to read.
Scales
Every mapped aesthetic has a scale controlling how data values become visuals — an axis, a legend, a color ramp. scale_*() functions let you customize them: titles, breaks, and the values themselves.
Scales: renaming and rebreaking axes
scale_x_continuous() and scale_y_continuous() control the x- and y-axis scales — here, a cleaner title and fewer tick marks (n.breaks).
Scales: choosing values manually
scale_shape_manual() lets you assign the exact shape (or color, or fill) for each category, instead of accepting the default.
The grammar of graphics
ggplot2 builds a figure by layering independent pieces on top of each other, instead of picking one big “chart type.”
data
The data frame you’re plotting.
mapping
Which columns control which visual properties (aes()).
geometry
The shape used to represent the data (geom_*()).
scale
How the mapping shows up on the figure (scale_*()).