library(ggplot2)
data(mpg)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.
ggplot(mpg,aes(x =displ,y =hwy,color =class))+
geom_point(size= 2) +
labs(
title= "Engine Displacement vs. Highway MPG",
x ="Engine displacement (liters)",
y ="Highway miles per gallon",
color="Vehicle class")Your task:
Write code and examine the resulting scatter plot.
Identify at least three problems associated with this visualization.
Explain why comparing multiple vehicle classes in one plot may make interpretation difficult.
Problems with this plot:
Too many colors. Seven classes means seven colors, and several of themlook a lot alike. To figure out what a given dot is, you have to look back and forth between the point and the legend. It’s also rough for anyone with color problems.
Classes overlap in the same region. cars all cluster in roughly the same range of engine sizes and mpg, so their colors get tangled together and no single class stands out.
Hard to see any pattern within a class. The overall trend is that bigger engines get worse mileage, but it’s tough to tell whether that holds inside each class
Why comparing classes in one plot is difficult: You have to separate the points by color, ignore the points hidden underneath other points, and still try to follow a trend. Because the classes pile on top of each other, the differences between them get buried, and what you end up with is a blob that suggests a general downward slope but doesn’t tell you much about any particular type of vehicle.
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",size =2)+
facet_wrap(~class) +
labs(
title= "Engine Displacement vs Highway MPG by Vehicle Class",
x ="Engine displacement(liters)",
y= "Highway miles per gallon"
)Your task:
Write code and compare this visualization with the original overcrowded scatter plot.
Explain how faceting improves readability.
Identify patterns or relationships that are easier to recognize in the faceted version.
Comparison with the original: The faceted version splits the same data into seven small plots, one per vehicle class, so each class gets its own space instead of fighting for room with the others. All the panels share the same axes, which keeps them easy to compare side by side.
How faceting improves readability: Since the class is now shown by the panel, color isn’t needed anymore and the legend disappears. Points no longer sit on top of points from other classes, so the overplotting that was a mess before is much less of an issue. You can look at one class at a time, or glance across panels to compare them.
Patterns that are easier to see:
- SUVs and pickups sit low on the hwy axis with a range of larger engines, while compact and subcompact cars sit higher up with smaller engines.
- Most classes still show the same downward trend, where a larger engine means lower highway mpg, so the pattern isn’t just a result of mixing classes together.
- Minivans are packed into a narrow band of engine sizes and mpg, and 2seaters are a small group of cars with big engines that still get decent mileage for their engine size.
- Midsize and compact cars cover a wide spread of engine sizes, so they show the trend most clearly.
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", size= 2)+
facet_grid(class ~drv)+
labs(
title= "Engine Displacement vs. Highway MPG by Class and Drive Type",
x="Engine displacement (liters)",
y="Highway miles per gallon")Your task:
Write code and examine how the visualization is organized.
Explain how
facet_grid()differs fromfacet_wrap().Discuss whether organizing the data by two categorical variables provides additional insights.
How it’s organized: The plot is laid out like a table. Each row is a vehicle class and each column is a drive type, so every panel shows one specific class and drive type combination. Several panels are empty because those combinations don’t exist in the data.
How facetgrid differs from facetwrap: facet_wrap() takes one variable, makes a panel for each value, and then wraps the panels into rows and columns to fit the space, so the position of a panel doesn’t carry any meaning. facet_grid() puts one variable on the rows and another on the columns, so the position of each panel tells you something, and you can read across a row or down a column to compare.
Does the second variable add insight? yes, within a single class you can now see how drive type changes things, for instance that four-wheel-drive subcompacts and compacts tend to have lower highway mpg than their front-wheel-drive versions for a similar engine size. You can also see which combinations are missing altogether.
Part 4: Critical Thinking and Comparison
Answer the following questions:
What makes the original scatter plot overcrowded or difficult to interpret?
Everything is drawn in one panel. A lot of points share the same coordinates and hide each other, seven similar colors have to be matched to a legend, and the classes overlap in the middle of the plot. You can pick out the general downward trend, but it’s hard to say anything about an individual class.
How does
facet_wrap()help address the problem?It gives each vehicle class its own panel with the same axes, which separates the groups so they stop overlapping. There’s no need for color or a legend, and it’s much easier to see the shape and trend inside each class and to compare classes against each other.
What additional information does
facet_grid()provide?It brings in a second categorical variable, drive type, so you can see how the displacement and mpg relationship changes for each class and drive combination. It also makes clear which combinations exist and which don’t (the empty panels), and the grid layout makes it simple to compare across a row or down a column.
Which of the three visualizations would you recommend for communicating the relationship between engine displacement and highway MPG? Justify your answer.
I’d go with the
facet_wrap()version. It keeps the main message, that bigger engines get worse highway mileage, while still showing how that plays out in each class, and it stays readable at a glance. The original plot is too cluttered to trust, and thefacet_grid()plot is better suited to a more detailed look at drive type than to a general audience, since the panels get small and some have very few points.Are there any disadvantages to using too many facets?
Yes. Each panel gets smaller as the number of facets goes up, so the points and labels get hard to read. Panels with very few observations can make a pattern look stronger than it really is, and it takes more effort to compare panels that are far apart on the page.