# Create a base plot with default labels and scales
p1 <-
ggplot(mpg, aes(x = displ, y = hwy)) +
geom_point(size = 3, aes(color = drv, shape = drv)) +
geom_smooth(method = "lm", color = "black")
p1Spring 2026 | Data 2 (399)
Jeffrey M. Girard | Lecture 06c
labs()



scale_ functions



ggplot(mpg, aes(x = displ, y = hwy)) +
geom_jitter(size = 3, aes(color = drv, shape = drv)) +
geom_smooth(method = "lm", color = "black") +
labs(
x = "Engine displacement (L)",
y = "Highway fuel economy (mpg)",
color = "Drivetrain",
shape = "Drivetrain",
title = "Fuel efficiency generally decreases with engine size",
subtitle = "Rear-wheel drive vehicles are often an exception due to sports cars",
caption = "Data from fueleconomy.gov"
) +
scale_y_continuous(
limits = c(0, 50),
breaks = seq(0, 50, by = 10)
) +
scale_x_continuous(
limits = c(1, 7),
breaks = seq(1, 7, by = 1)
) +
scale_color_manual(
breaks = c("r", "f", "4"),
labels = c(
"4" = "Four-Wheel",
"f" = "Front-Wheel",
"r" = "Rear-Wheel"
),
values = c(
"4" = "salmon",
"f" = "cornflowerblue",
"r" = "seagreen3"
)
) +
scale_shape_discrete(
breaks = c("r", "f", "4"),
labels = c(
"4" = "Four-Wheel",
"f" = "Front-Wheel",
"r" = "Rear-Wheel"
)
)