library(ggplot2)
data(mpg)
ggplot(mpg, aes(x = displ, y = hwy, color = class)) +
geom_point() +
labs(
title = "Engine Displacement and Highway MPG by Vehicle Class",
x = "Engine Displacement (L)",
y = "Highway MPG",
color = "Vehicle Class"
)Independent Learning Activity
Bryan Chillogalli
DATA-110
11/20/2026
From Overcrowded Scatter Plots to Effective Faceted Visualizations
Learning Objectives
Identify problems associated with overcrowded scatter plots.
Use faceting techniques to improve data visualization.
Apply
facet_wrap()andfacet_grid()in ggplot2.Compare different visualization approaches and explain which communicates patterns more effectively.
Part 1: Create an Overcrowded Scatter Plot
Use the built-in mpg dataset available in the ggplot2 package.
Create a scatter plot showing the relationship between engine displacement (displ) and highway miles per gallon (hwy).
Include all vehicle classes and use different colors to represent vehicle classes.
data(mpg)
Your task:
Write code and examine the resulting scatter plot.
Identify at least three problems associated with this visualization.
Some vehicles have the same engine displacement and highway MPG, so one point can hide another, which makes it hard to see how many vehicles are at a given spot.
All seven vehicle classes appear in the same panel, so I have to look back and forth between the points and the legend to check what class it is.
Different classes appear in some of the same areas of the graph. there is an overall trend, but it’s harder to tell whether highway MPG drops as engine size increases within each class or to compare classes at similar engine sizes.
Explain why comparing multiple vehicle classes in one plot may make interpretation difficult.
vehicles from different classes may have similar engine sizes and highway MPG, causing their points to overlap. Even with the different colors, it can be hard to see whether highway MPG decreases as engine size increases within each class or to compare MPG across classes at the same engine size.
Part 2: Improve the Visualization Using facet_wrap()
Now, improve your original scatter plot using facet_wrap().
Create separate panels for each vehicle class.
ggplot(mpg, aes(x = displ, y = hwy)) +
geom_point(color = "steelblue", alpha = 0.7) +
facet_wrap(~ class, ncol = 3) +
labs(
title = "Engine Displacement and Highway MPG by Vehicle Class",
x = "Engine Displacement (L)",
y = "Highway MPG"
)Your task:
Write code and compare this visualization with the original overcrowded scatter plot.
Explain how faceting improves readability.
Faceting gives each class its own panel, so even if there are matching MPG and displacement, it wont overlap. The labels make it easy to identify each class without checking a color legend. Since all panels use the same axis scales, it’s also easier to see the trend within each class and compare highway MPG across classes at similar engine sizes.
Identify patterns or relationships that are easier to recognize in the faceted version.
it’s easier to see that SUVs with larger engines get lower highway MPG. All five two-seaters also only have large engines and get 23–26 highway MPG, while pickups in the dataset get 12–22 highway MPG.
Part 3: Improve the Visualization Using facet_grid()
Create another visualization using facet_grid().
Use vehicle class as rows and drive type (drv) as columns.
ggplot(mpg, aes(x = displ, y = hwy)) +
geom_point(color = "steelblue", alpha = 0.7) +
facet_grid(class ~ drv) +
labs(
title = "Engine Displacement and Highway MPG by Class and Drive Type",
x = "Engine Displacement (L)",
y = "Highway MPG",
caption = "Drive type codes (4 = four-wheel, f = front-wheel, r = rear-wheel)"
)1.[1] 1
Your task:
Write code and examine how the visualization is organized.
Explain how
facet_grid()differs fromfacet_wrap().facet_wrapgives each vehicle class one panel and “wraps” the panels onto multiple rows, whilefacet_gridassigns vehicle classes to rows and drive types to columns. Each panel represents one class/drive type combination, so now you can read across a row to compare drive types with the same class.Discuss whether organizing the data by two categorical variables provides additional insights.
Organizing the data with 2 categorical variables makes it easier to see how engine size and highway MPG vary by drive type within the same class. For example, front-wheel-drive subcompacts have smaller engines and get higher highway MPG than rear-wheel-drive subcompacts. The empty panels also show which combinations are missing. For example, all pickups in this dataset have four-wheel drive.
Part 4: Critical Thinking and Comparison
Answer the following questions:
What makes the original scatter plot overcrowded or difficult to interpret?
The original scatter plot puts all the vehicle classes in one panel, which makes some points overlap and hide each other, and it’s hard to follow each color in the crowded areas. .
How does
facet_wrap()help address the problem?facet_wrap()gives each vehicle class its own panel, so it stops the points from overlapping. The panels use the same axis scales, which makes it easier to compare highway MPG at similar engine sizes across classes.What additional information does
facet_grid()provide?facet_grid()adds drive type as another grouping variable. Each row is a vehicle class, and each column is a drive type, so I can compare drive types within the same class.Which of the three visualizations would you recommend for communicating the relationship between engine displacement and highway MPG? Justify your answer.
I would use
facet_wrap()because it makes the relationship between engine displacement and highway MPG easy to see within each class and compare across classes.Are there any disadvantages to using too many facets?
using too many facets can make the panels so small that points and labels are hard to read. Some panels may also be empty or have too few observations to show a clear trend.