ggplot2: Facets, Themes & Scales
This lesson takes ggplot2 beyond the basic plot: facets split a chart into small multiples, themes restyle every non-data element, and scales control your axes, labels, and colours — the tools that turn a rough plot into a polished, publication-ready graphic.
Learn ggplot2: Facets, Themes & Scales in our free R course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick…
Part of the free R course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
You'll use facet_wrap() / facet_grid() , apply themes like theme_minimal() and fine-tune with theme() , format axes with scale_*_continuous() , label everything with labs() , and export with ggsave() .
What You'll Learn in This Lesson
1️⃣ Faceting: Small Multiples
Faceting breaks a single plot into a grid of panels, one per group, all sharing the same axes so comparisons are honest. facet_wrap(~ var) wraps panels for one variable; facet_grid(rows ~ cols) crosses two variables into a matrix.
What renders: the first plot shows seven scatter panels (one per class ); the second a grid with drive type down the rows and cylinder count across the columns; the third frees each panel's axes so groups with different ranges aren't squashed.
2️⃣ Themes: Restyle Everything
A theme governs all the non-data ink. A complete theme swaps the entire look in one call; then theme() lets you adjust individual elements like legend position or title weight.
What renders: theme_minimal() removes the grey panel for a clean white background; theme_classic() draws bare axis lines; the final block keeps the minimal base but pushes the legend to the bottom, bolds the title, and hides the minor gridlines.
3️⃣ Scales, Labels & Saving
Scales map data to what you see. scale_x_continuous() and friends set axis ranges, breaks, and tick labels; colour scales pick the palette. The scales package supplies label helpers like comma and percent .
Finally, labs() centralises every text label and ggsave() exports the figure at a precise size and resolution.
Your turn. Fill in the # TODO blank, run it in RStudio, and check the rendered plot against the description.
Bring facets, a theme, labels, and export together into one finished figure. Build it from the outline, then run it in RStudio and inspect the saved PNG.
📋 Quick Reference — Facets, Themes & Scales
Practice quiz
What does faceting do in ggplot2?
- Splits one plot into a grid of panels by group
- Changes the colour palette
- Saves the plot to a file
- Adds a trend line
Answer: Splits one plot into a grid of panels by group. Facets create small multiples: one panel per group on identical axes.
Which function wraps panels by a SINGLE variable?
- facet_grid()
- facet_wrap()
- facet_split()
- facet_one()
Answer: facet_wrap(). facet_wrap(~ var) wraps panels for one variable into a tidy grid.
Which function lays panels out by TWO variables as rows ~ columns?
- facet_pair()
- facet_wrap()
- facet_grid()
- facet_matrix()
Answer: facet_grid(). facet_grid(rows ~ cols) crosses two variables into a matrix of panels.
What does facet_wrap() REQUIRE as its argument?
- A plain column name
- A character string
- A list
- A formula with a tilde, e.g. ~ class
Answer: A formula with a tilde, e.g. ~ class. Faceting needs a formula: facet_wrap(~ class), not facet_wrap(class).
Which is a COMPLETE theme that restyles the whole plot in one call?
- theme_minimal()
- theme(legend.position = "bottom")
- element_text()
- labs()
Answer: theme_minimal(). Complete themes like theme_minimal() reset every element at once.
Which call moves the legend to the bottom?
- labs(legend = "bottom")
- theme(legend.position = "bottom")
- scale_legend("bottom")
- guides("bottom")
Answer: theme(legend.position = "bottom"). theme() tweaks individual elements such as legend.position.
Which function sets axis range, breaks, and tick labels on a continuous x-axis?
- coord_x()
- axis_x()
- scale_x_continuous()
- xlim_set()
Answer: scale_x_continuous(). scale_x_continuous() controls limits, breaks, and labels for a continuous x-axis.
To colour points BY a variable, where must the colour mapping go?
- Outside aes(), in the geom
- In ggsave()
- In a separate theme() call
- Inside aes(), e.g. aes(colour = class)
Answer: Inside aes(), e.g. aes(colour = class). Data-driven aesthetics go inside aes(); a fixed colour goes outside aes().
Which function sets the title, axis labels, and caption in one place?
- labs()
- theme()
- annotate()
- ggtitle_all()
Answer: labs(). labs() centralises title, subtitle, axis labels, legend titles, and caption.
Which function exports the plot to a file at a chosen size and dpi?
- export()
- ggsave()
- write_plot()
- save.image()
Answer: ggsave(). ggsave() writes a plot to a file; width/height are inches and dpi sets resolution.