Skip to content
Module 08 of 1265 min readBeginner

ggplot2 grammar of graphics

aes, geom, facets, scales, themes. The layered grammar that makes R the plotting tool of choice.

67%

Listen along

Read “ggplot2 grammar of graphics” aloud

Plays in your browser using on-device text-to-speech — nothing leaves the page.

Learning objectives

By the end of this module, you should be able to:

  • 01Build a ggplot from data, aesthetic mappings, and geometric layers
  • 02Use facet_wrap and facet_grid for small-multiple plots
  • 03Apply scales (continuous, percent, log) and themes for publication-ready output
  • 04Recognise that ggplot expects long-format data — pivot_longer first if needed

ggplot2 is R's signature plotting library, built on the Grammar of Graphics. Once you internalise the grammar — data + aesthetics + geom + scales + themes — you can build virtually any chart with a small consistent vocabulary.

The grammar

r
library(ggplot2)
ggplot(bankrates, aes(x = month, y = lending_rate)) +
geom_line() +
labs(title = "Kenyan lending rates", y = "Rate (%)")

Every ggplot has at minimum: data (the data frame), an aes mapping (which columns control which visual properties), and one or more geoms (the geometric shapes — point, line, bar, etc.). You add layers with +.

Common geoms

  • geom_point — scatter plot
  • geom_line — line plot
  • geom_bar — bar plot (counts)
  • geom_col — bar plot (values)
  • geom_histogram — histogram
  • geom_boxplot — boxplot
  • geom_smooth — fitted regression curve with CI

Aesthetics in detail

r
ggplot(bankrates, aes(x = month, y = lending_rate, colour = bank)) +
geom_line()
# colour = bank means draw a separate line per bank, with auto-legend

Faceting — small multiples

r
ggplot(bankrates_long, aes(x = month, y = rate)) +
geom_line() +
facet_wrap(~ bank, scales = "free_y")
# One panel per bank, each with its own y-axis scale

Scales and themes

r
ggplot(bankrates, aes(x = month, y = lending_rate)) +
geom_line() +
scale_y_continuous(labels = scales::percent) +
theme_minimal() +
labs(title = "Lending rates", x = NULL, y = "Rate")

Plot in long format

ggplot expects long data. If you have wide data, pivot_longer first. Trying to draw multiple lines from wide data is the most common ggplot frustration for beginners.

Exercise

Plot a line chart of bankrates with month on x and lending_rate on y.

Key takeaways

  • Every ggplot: data + aes() + geom_*() + scales + theme. Add layers with +
  • aes(colour = bank) inside the geom maps colour to a variable; outside it sets a constant colour
  • Facets create small multiples — one panel per level of a grouping variable
  • scale_y_continuous(labels = scales::percent) is the cleanest way to format axis labels

Further reading

  1. 01

    ggplot2: Elegant Graphics for Data Analysis (3rd Edition)

    Hadley Wickham, Danielle Navarro & Thomas Lin Pedersen · Springer · 2023

  2. 02

    Fundamentals of Data Visualization

    Claus O. Wilke · O'Reilly · 2019

  3. 03

    The Grammar of Graphics

    Leland Wilkinson · Springer · 2005The book that inspired ggplot2.

Loading progress…
LeadAfrikPublic Economics Hub