library(ggplot2)
ggplot(mpg, aes(x = displ, y = hwy, color = class)) +
geom_point(size = 3, alpha = 0.8) +
labs(
title = "Engine Displacement vs. Highway MPG",
x = "Engine Displacement (L)",
y = "Highway Miles per Gallon",
color = "Vehicle Class"
) +
theme_minimal()Independent Learning Activity
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.
- unequal group sizes, some categories have less points on the graph
- too many colors make it harder to interpret
- many points share the same values so they hide each other
Explain why comparing multiple vehicle classes in one plot may make interpretation difficult.
Trends within classes are hard to see in a single panel, and the visual pattern can hide the intended data.
Part 2: Improve the Visualization Using facet_wrap()
Now, improve your original scatter plot using facet_wrap().
Create separate panels for each vehicle class.
library(ggplot2)
ggplot(mpg, aes(x = displ, y = hwy)) +
geom_jitter(width = 0.05, height = 0.3, size = 2, alpha = 0.6, color = "limegreen") +
geom_smooth(method = "lm", se = FALSE, color = "grey30", linewidth = 0.8) +
facet_wrap(~ class, scales = "free_x") +
labs(
title = "Engine Displacement vs. Highway MPG by Vehicle Class",
x = "Engine Displacement (L)",
y = "Highway MPG"
) +
theme_minimal()`geom_smooth()` using formula = 'y ~ x'
Your task:
Write code and compare this visualization with the original overcrowded scatter plot.
Explain how faceting improves readability.
It breaks each category into a separate graph, which improves visibility and makes it easier to interpret.
Identify patterns or relationships that are easier to recognize in the faceted version.
Its easier to see that some categories like subcompact or compact cars have a much more significant downward trends.
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.
library(ggplot2)
ggplot(mpg, aes(x = displ, y = hwy)) +
geom_jitter(width = 0.05, height = 0.3, size = 1, alpha = 0.6, color = "limegreen") +
facet_grid(class ~ drv) +
labs(
title = "Engine Displacement vs. Highway MPG by Class and Drive Type",
x = "Engine Displacement (L)",
y = "Highway MPG"
) +
theme_minimal() +
theme(strip.text.y = element_text(angle = 0))
Your task:
Write code and examine how the visualization is organized.
Explain how
facet_grid()differs fromfacet_wrap().facet_grid()is for comparing two variables systematically, andfacet_wrap()is for laying out many levels of one variable compactly.Discuss whether organizing the data by two categorical variables provides additional insights.
Yes, the grid helps show that both vehicle class and drive type relate to mpg, instead of attributing everything to class.
Part 4: Critical Thinking and Comparison
Answer the following questions:
What makes the original scatter plot overcrowded or difficult to interpret?
Many categories are on one graph, the points overlap which makes it harder to interpret.
How does
facet_wrap()help address the problem?Yes, because It breaks each category into a separate graph, which improves visibility and makes it easier to interpret.
What additional information does
facet_grid()provide?It helps compare two variables systematically, and shows how drive type and class relate to mpg.
Which of the three visualizations would you recommend for communicating the relationship between engine displacement and highway MPG? Justify your answer.
facet_wrap()plot because it shows that larger engines mean lower highway MPG within every vehicle class, without the clutter of one crowded plot.Are there any disadvantages to using too many facets?
Yes, too many facets make each panel small, so patterns become hard to see and trends based on a few points can be misleading.