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() and facet_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.
The legend names are in raw form, and the chart is missing proper axis labels, a title, and a data source.
Even with the legend, overlapping dots and similar colors make it hard to read and spot clear patterns across the different vehicle classes
The units for the axes aren’t shown anywhere, so you can’t tell what engine displacement is measured in.
Explain why comparing multiple vehicle classes in one plot may make interpretation difficult. When all seven vehicle classes are stacked into one single graph, the dots overlap a lot and hide where each group starts and ends. Having to constantly look back and forth between the color legend and the crowded dots makes it tough to compare trends across different car types.
When all seven classes are piled onto one graph, the dots overlap and hide individual group trends. Having to match similar colors back to the legend also makes comparing car types slow and confusing.
library(ggplot2)data(mpg)head(mpg)
# A tibble: 6 × 11
manufacturer model displ year cyl trans drv cty hwy fl class
<chr> <chr> <dbl> <int> <int> <chr> <chr> <int> <int> <chr> <chr>
1 audi a4 1.8 1999 4 auto(l5) f 18 29 p compa…
2 audi a4 1.8 1999 4 manual(m5) f 21 29 p compa…
3 audi a4 2 2008 4 manual(m6) f 20 31 p compa…
4 audi a4 2 2008 4 auto(av) f 21 30 p compa…
5 audi a4 2.8 1999 6 auto(l5) f 16 26 p compa…
6 audi a4 2.8 1999 6 manual(m5) f 18 26 p compa…
ggplot(mpg, aes(x = displ, y = hwy, color = class)) +geom_point()
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, color = class)) +geom_point() +facet_wrap(~ class)
Your task:
Write code and compare this visualization with the original overcrowded scatter plot. Compared to the first plot where everything was piled on top of each other, this version splits the data into seven clean mini-plots. It uses the exact same data, just spaced out so nothing is blocking anything else.
Explain how faceting improves readability. Giving each vehicle class its own box stops the points from overlapping and takes away the need to constantly check the legend since every panel has its own label at the top.
Each vehicle class has its own panel, reducing visual clutter and making it easier to identify individual points and compare trends.
Identify patterns or relationships that are easier to recognize in the faceted version. It is much easier to see that almost every vehicle class follows a downward trend where bigger engines get lower highway mpg. You can also spot outliers right away, like 2seaters having huge engines with decent highway mileage compared to SUVs and pickups.
Most vehicle classes show a downward trend, meaning larger engines generally have lower highway MPG. Differences between vehicle classes are also easier to recognize.
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, color = class)) +geom_point() +facet_grid(class ~ drv)
Your task:
Write code and examine how the visualization is organized. The chart is organized into a strict grid where the rows are the vehicle classes and the columns are the drive types (4 for 4-wheel drive, f for front-wheel drive, and r for rear-wheel drive).
Explain how facet_grid() differs from facet_wrap(). facet_wrap() just wraps a single variable into a neat bunch of panels to fit the screen, while facet_grid() forces the plots into a strict row-by-column matrix using two variables—even if some boxes end up completely empty.
facet_wrap() arranges panels based on one variable, while facet_grid() organizes them into rows and columns using two variables.
Discuss whether organizing the data by two categorical variables provides additional insights. Yes, it shows combinations that don’t exist in the dataset, like how there are zero front-wheel drive pickups or SUVs, and all 2seaters are rear-wheel drive. It also shows that front-wheel drive cars generally get better highway mpg than 4-wheel drive ones in the same class.
Yes, it reveals missing combinations like zero front-wheel drive SUVs or pickups, and shows that front-wheel drive cars usually get better highway mpg than 4-wheel drive ones in the same class.
Part 4: Critical Thinking and Comparison
Answer the following questions:
What makes the original scatter plot overcrowded or difficult to interpret? Putting seven categories onto one plot causes points to overlap, uses colors that look too similar, and relies on raw variable names without units or clear titles.
The points overlap, and some colors are difficult to tell apart. It is also harder to compare trends between vehicle classes when everything is in one plot.
How does facet_wrap() help address the problem? It separates each vehicle class into its own panel so you can actually see the individual points and spot the downward trend inside each group without clutter.
It gives each vehicle class its own panel, making the points easier to see. It also makes it easier to compare trends without the groups overlapping.
What additional information does facet_grid() provide? It adds a second layer of detail by breaking each vehicle class down by drive type (drv), making it easy to see which combinations exist and how drive type affects fuel efficiency.
It breaks each vehicle class down by drive type (drv). This reveals which combinations exist and makes it easier to compare fuel efficiency across different drive types.
Which of the three visualizations would you recommend for communicating the relationship between engine displacement and highway MPG? Justify your answer. I would recommend the facet_wrap() plot because it clears up all the overlapping dots from the first plot while still keeping the panels large and easy to read. It shows the main relationship between engine size and mpg across vehicle classes without creating a bunch of empty boxes like facet_grid() does.
I would recommend facet_wrap() because it separates each vehicle class while keeping the plots easy to read. It shows the relationship between engine size and highway MPG without the extra complexity and empty panels of facet_grid().
Are there any disadvantages to using too many facets? Yes, if you use too many facets, each individual box shrinks down so much that the points get squished together and hard to read. You also end up with a lot of blank panels if certain combinations don’t have any data.
Too many facets can make each panel too small to read clearly. They can also create empty panels when certain category combinations have no data, making the overall chart harder to understand.