Classwork 14

Exploring Relationships with Scatterplots

Author

Byeong-Hak Choe

Published

October 28, 2026

NoteNote

Complete the questions and submit your responses on the corresponding Brightspace page.

Complete the code in an R script and keep your brief written interpretations with it. This page contains its own setup and data definitions; no earlier classwork is required.

Setup and data

library(tidyverse)

The built-in mpg data contains 234 vehicle model configurations from model years 1999 and 2008. Each row represents a configuration, not an individual driver or purchase.

Variable Meaning
cty City fuel economy in miles per gallon
hwy Highway fuel economy in miles per gallon
year Model year: 1999 or 2008

Higher miles per gallon means farther travel using one gallon of fuel. Use factor(year) to treat the two model years as categories in plots. Dataset reference: ggplot2 · mpg.

Filling-in-the-blank: 1. City and highway fuel economy

Make a scatterplot with city fuel economy on the horizontal axis and highway fuel economy on the vertical axis.

ggplot(data = mpg,
       mapping = aes(x = ____,
                     y = ____)) +
  ____()

Describe the direction of the relationship in one sentence. State what one point represents. Unlike a histogram, this graph maps an observed variable to y; its vertical axis is not a count.

Filling-in-the-blank: 2. Color identifies model year

Use color to distinguish the two model years. Use transparency to make overlapping points easier to see.

ggplot(data = mpg,
       mapping = aes(x = cty,
                     y = hwy,
                     color = factor(____))) +
  geom_point(alpha = 0.6)

Describe one difference or overlap between the 1999 and 2008 points. Do not treat this comparison of selected configurations as proof that fuel economy improved or worsened over time.

Filling-in-the-blank: 3. Separate panels

Make a separate scatterplot for each model year. Keep the default shared axis scales.

ggplot(data = mpg,
       mapping = aes(x = cty,
                     y = hwy)) +
  geom_point(alpha = 0.6) +
  ____(~ factor(____))

Use facet_wrap() in the blank. Which version makes your comparison easier: color on a shared plot or separate panels? Give one reason based on these graphs.

Interpretation: 4. Mapping or setting?

Explain the difference between mapping = aes(color = factor(year)) and geom_point(color = "blue"). Which assigns a color according to model year, and which gives every point one chosen color?

Filling-in-the-blank: 5. Add a visual summary layer

Add a straight fitted line to the original scatterplot. method = "lm" requests a straight line, and se = FALSE hides the uncertainty band.

ggplot(data = mpg,
       mapping = aes(x = cty,
                     y = hwy)) +
  geom_point() +
  ____(method = "lm", se = FALSE)

Does the line capture the general direction? Briefly explain why the pattern alone does not show that changing a vehicle’s city fuel economy causes its highway fuel economy to change.

Back to top