Unit 07.01: Five components change meaning; one changes looks
Six components, and only one of them is about appearance.
What changes meaning, and what changes looks
Data and aesthetics decide what is shown. Geoms decide how it is drawn. Facets split one chart into small panels by a category, so comparison happens between panels rather than between overlapping lines. Scales control how values map to positions and colours — including axis limits, which change what the reader concludes. Labels tell a reader what they are looking at.
Only themes are cosmetic. Everything else changes what the chart says, which is why time spent on theming before the mapping is right is time wasted.
Faceting is the component most often under-used. Three overlapping series become three readable panels for one line of code.
This block uses all six on one chart.
suppressPackageStartupMessages(library(ggplot2))
monthly <- data.frame(
ward = rep(c("North", "South", "East"), each = 3),
month = rep(c("Jan", "Feb", "Mar"), times = 3),
visits = c(412, 455, 431, 388, 401, 396, 502, 498, 511)
)
monthly$month <- factor(monthly$month, levels = c("Jan", "Feb", "Mar"))
plot <- ggplot(monthly, aes(x = month, y = visits, group = ward, colour = ward)) +
geom_line(linewidth = 0.8) + # geom: how to draw
geom_point(size = 2) + # a second layer on the same mapping
facet_wrap(~ ward) + # facets: one panel per ward
scale_y_continuous(limits = c(0, 600)) + # scale: how values map to the axis
labs(title = "Monthly visits by ward", # labels: what a reader needs
x = "Month", y = "Visits", colour = "Ward") +
theme_minimal() # theme: appearance only
cat("Layers :", length(plot$layers), "\n")
cat("Facets :", class(plot$facet)[1], "\n")
cat("Panels :", length(unique(monthly$ward)), "\n\n")
f <- file.path(tempdir(), "faceted.png")
ggsave(f, plot, width = 7, height = 3, dpi = 100)
cat("Saved", basename(f), "at", file.size(f), "bytes\n")
cat("Only theme_minimal() is cosmetic. Every other line changes meaning.\n")
Two geoms means 2 layers on a single mapping — lines and the points that mark each observation. The facet specification is a FacetWrap producing 3 panels, one per ward. The y scale is fixed from 0 to 600 so the panels are comparable, which is the whole reason for setting it. The labels name the measure and the axis, and theme_minimal() is the only line that could be removed without changing a single thing the chart claims.
The mistake this prevents
The mistake is faceting on a variable with many levels. Thirty panels are unreadable; faceting works when the number of categories is small enough to compare at a glance.
Takeaway
Get data, aesthetics and geoms right first, then facet if you are comparing groups, then set scales deliberately, then label. Theme last, and only if you have time.
