Data Visualization II: Relationships and Time Trends
November 1, 2026
aes(), and a geom layer to show distributions and compare groups.| Part of the plot | What stays familiar | What changes here |
|---|---|---|
data = mpg |
The data frame | We keep the same vehicle rows |
mapping = aes(...) |
Variables control visual features | Both x and y contain numeric variables |
geom_*() |
A layer draws the data | geom_point() draws a point for each row |
displ gives engine size in liters. hwy gives highway miles per gallon.mpg charts.drv |
Drive system |
|---|---|
f |
Front-wheel drive |
r |
Rear-wheel drive |
4 |
Four-wheel drive |
color = drv inside aes() maps groups to colors. scale_color_tableau() chooses those colors.alpha = 0.5 makes points partly transparent. Overlapping points appear darker.| Code | Meaning |
|---|---|
aes(color = drv) |
Let a variable determine each pointโs color |
geom_point(color = "steelblue") |
Give every point the same chosen color |
aes() when its values should control a visual feature.aes(). A color name inside aes() can create an unintended legend.facet_wrap(~ drv) panels use shared axes, just as in our histograms. The groups also cover different engine-size ranges.+ adds another layer to the same plot. method = "lm" requests a fitted straight line.se = FALSE hides the shaded uncertainty band. The line summarizes the overall direction, while the points show variation around it.| Country | GDP per person | Life expectancy |
|---|---|---|
| Afghanistan | 974.6 | 43.8 |
| Albania | 5937.0 | 76.4 |
| Algeria | 6223.4 | 72.3 |
| Angola | 4797.2 | 42.7 |
gdpPercap is GDP per person in inflation-adjusted dollars, a measure of economic output per person rather than personal salary.lifeExp is life expectancy at birth in years. These historical observations do not describe current conditions.read_tsv() reads a file whose columns have tab separators, rather than comma separators.filter(year == 2007) keeps a single year, so each country contributes one observation.mpg with a different data frame.| Original value \(x\) | \(\log_{10}(x)\) | Why |
|---|---|---|
| 100 | 2 | \(10^2 = 100\) |
| 1,000 | 3 | \(10^3 = 1{,}000\) |
| 10,000 | 4 | \(10^4 = 10{,}000\) |
log10().log10(x) uses base 10. log(x) uses the natural-log base \(e \approx 2.718\).aes()log10(gdpPercap) calculates the horizontal positions from the GDP values.lifeExp stays in its original units: years.gdpPercap column.log10(0) returns -Inf, and negative inputs produce NaN.| Date | Company ticker | Closing price (US$) |
|---|---|---|
| 2026-10-01 | AAPL | 330.32 |
| 2026-10-01 | NVDA | 230.86 |
| 2026-10-02 | AAPL | 333.69 |
| 2026-10-02 | NVDA | 233.95 |
Close gives the closing price per share in US dollars.as.Date() converts a value such as "2026-10-01" into Rโs Date type. read_csv() may already recognize this format.%in% checks membership in a set. Here it keeps two companies, and the final filter() keeps only Apple.geom_point() marks the observed values. geom_line() connects them in x-axis order.Ticker variable to color creates a separate line for each company.linetype = Ticker also distinguishes the companies when colors are hard to tell apart.group = Ticker explicitly tells ggplot which rows belong to the same series.facet_wrap(~ Ticker) places each companyโs history in its own panel, with shared scales by default.| Question | Variables | Useful layer |
|---|---|---|
| How are values distributed? | One numeric variable | geom_histogram() |
| How do distributions differ across groups? | Numeric + category | geom_boxplot() |
| How do categories compare? | Category counts or supplied values | geom_bar() or geom_col() |
| How do variables vary together? | Two numeric variables | geom_point() |
| How does a quantity change through time? | Date + numeric variable | geom_line() |
aes(), and + remain familiar. The question guides the mappings and layers.labs() changes displayed text without renaming the data columns.labs() is a convenient way to supply them.