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, liters), used to explain or predict the outcome.hwy, miles per gallon), the value we want to understand.Higher x tends to go with higher y.
Higher x tends to go with lower y.
As x increases, y shows no clear upward or downward pattern.
geom_point() and geom_smooth(). geom_smooth() shows the predicted average y for a given x.method = "lm"geom_smooth(method = "lm") specifies that a linear model (lm), called a linear regression model
x), the line gives an estimated average highway mpg (y) based on these data.se = FALSEse = FALSE hides the shaded ribbon around the fitted line.x it gives a 95% confidence interval for the average y.x and y variables falls within the grey ribbon.–>
Two variables tend to vary together.
A change in one variable helps produce a change in the other.
A scatterplot can show an association, but it cannot tell us whether one variable causes changes in the other.
color identifies groupscolor = drv inside aes() maps groups to colors. scale_color_tableau() chooses those colors.alpha ranges from 0 (invisible) to 1 (fully opaque). At alpha = 0.5, points are partly transparent, so overlaps appear darker.shape also identifies groupsshape = drv inside aes() gives each drive-system group a different point symbol.
shape applies to geom_point(), not geom_smooth().linetype also identifies groupsggplot(data = mpg,
mapping = aes(x = displ,
y = hwy,
color = drv,
fill = drv,
linetype = drv)) +
geom_smooth() +
scale_color_tableau() +
scale_fill_tableau() +
labs(x = "Engine size (liters)",
y = "Highway fuel economy (mpg)",
color = "Drive system",
fill = "Drive system",
linetype = "Drive system")linetype = drv inside aes() gives each drive-system group a different line.
linetype applies to geom_smooth(), not geom_point().fill controls the color of the ribbon for geom_smooth().| 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.| 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.lifeExp is life expectancy at birth in years.read_csv() imports the CSV file as a data frame in R.filter(year == 2007) keeps a single year of 2007, so each country contributes one observation.aes()log10(gdpPercap) calculates the horizontal positions from the GDP values.lifeExp stays in its original units: years.gdpPercap column.log10(income) in ny_customers. Then plot each against age with a default smooth curve and a fitted straight line.| 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.tech_october <- read_csv(
"https://bcdanl.github.io/data/tech_stocks_2026_10.csv"
)
tech_two <- tech_october |>
filter(Ticker == "AAPL" | Ticker == "NVDA")
apple_october <- tech_two |>
filter(Ticker == "AAPL")
msft_october <- tech_october |>
filter(Ticker == "MSFT")
msft_2026 <- read_csv(
"https://bcdanl.github.io/data/msft_2026.csv"
)read_csv() may already recognize a column such as "2026-10-01" as the Date type. geom_point() marks the observed values. geom_line() connects them in x-axis order.msft_2026 contains 187 trading days from January–September 2026. The points and line show day-to-day price changes.geom_smooth() adds an predicted average trend by smoothing out variability in the observations.Ticker variable to color creates a separate line for each company.linetype = Ticker also distinguishes the companies when colors are hard to tell apart.aes() in ggplot() supplies x = Date and y = Close to both layers.aes() in geom_line() maps color = Ticker for the lines. geom_point() uses the shared positions and a single default color.| 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() + geom_smooth() |
| How does a quantity change through time? | Date + numeric variable | geom_line() |
aes(), and add the geom layers that show the comparison you need.