Explanation 1
The live starter code is self-contained and uses only runner-compatible functions.
Loading
Lesson 8 of 22
Use layers, scales, facets, labels, and accessible encodings to make defensible statistical graphics.
Motivation
A chart is an analytical argument made with position, scale, colour, and annotation; design choices determine the comparisons readers can make.
Why this matters
Use layers, scales, facets, labels, and accessible encodings to make defensible statistical graphics.
Packages and data
ggplot2 4.0.3 in the book; base graphics in the live runner.
Explanation 1
The live starter code is self-contained and uses only runner-compatible functions.
Explanation 2
Book-only outputs are labelled when the separate source dataset or advanced package was not supplied.
Explanation 3
R code, paths, function names, and formulas remain left-to-right in every locale.
Terminology
A variable-to-visual relationship declared inside aes().
date to x, inflation to y
A fixed visual property outside aes().
colour = '#08747D'
Repeated aligned panels that avoid misleading dual axes.
One panel per series
Notation and formulas
graphic = data + mappings + geoms + scales + coordinates + facets + theme
Separating components makes graphical intent reviewable.
Worked example
Scenario
How does a zero baseline change perception of annual means?
R check
annual <- c('2022'=6.8,'2023'=3.9,'2024'=2.4,'2025'=2.2)
print(range(annual))
barplot(annual, ylim=c(0,7), col='#08747D', ylab='Inflation (%)')
abline(h=2, lty=2)A 2% reference supports policy context without implying a forecast. Limitation: Annual averaging hides within-year movement and does not explain causes.
Visual
The same four means shown from a zero baseline.
R connection
Run the self-contained starter code in the protected STATLAB R runner. The code prints an auditable result and avoids network or unrestricted file access.
Live R Lab
Run a self-contained example, verify its output, and explain one limitation for data visualization with ggplot2.
Ready to run
Common mistake
A truncated bar baseline exaggerates small differences because bar length implies ratios.
STATLAB Tip
Write the intended one-sentence takeaway before polishing; remove elements that do not support it or an essential qualification.
Guided practice
Scenario
Redesign the bars as a dot plot with a 2% reference line and draft alt text.
Compare your result with the definition, units, and model assumptions—not only with a target number.
Exercises
Complete these without looking at the selected solutions. More than one defensible program may exist.
Explanation 1
Create a labelled line chart.
Explanation 2
Compare fixed and free facet scales.
Explanation 3
Draft alt text naming chart type, variables, pattern, and reference.
Selected solutions
These are compact solution routes. Confirm dimensions, units, and any changed modelling choices.
Explanation 1
plot(dates, x, type='l'); abline(h=2,lty=2)
Explanation 2
Fixed scales compare magnitude; free scales reveal within-series shape.
Explanation 3
Good alt text communicates the main pattern without listing every point.
Chapter summary
Use layers, scales, facets, labels, and accessible encodings to make defensible statistical graphics.
Explanation 1
Build graphics from explicit components.
Explanation 2
Map variables; set constants.
Explanation 3
Axes determine valid comparisons.
Explanation 4
Contrast, labels, and redundant encodings improve access.
Terminology
initialize graphic
Use in Data Visualization with ggplot2.
map variables
Use in Data Visualization with ggplot2.
ordered path
Use in Data Visualization with ggplot2.
titles and units
Use in Data Visualization with ggplot2.
References and provenance
Safavi (2026), Chapter 8; ggplot2 documentation. Student notes: Mohammad Safavi, Ph.D., STATLAB Academy, Version 1.0.
Resource
The authoritative 125-page English PDF accompanies this native lesson.
DownloadReflection
Name the assumption, evidence you would seek, and how the recommendation might change.
Exit check
Reach 70% to complete the chapter. Explanations appear after submission.
Checkpoint
Question 1 of 2. Answered 0/2. Passing score: 70%.