library(ggplot2)
data(mpg)
ggplot(mpg, aes(x = displ, y = hwy, color = class)) +
geom_point() +
labs(
title = "Engine Displacement vs. Highway MPG",
x = "Engine Displacement (Liters)",
y = "Highway Miles Per Gallon (MPG)",
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.
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.
Part 1 Answers
Problems with the scatterplot:
One of the problems is that there are seven different vehicle classes. When we have all of those as different colors, it can make the graph feel a little crowded.
Another problem is that there are some points that are just overlapping, and so it’s just a lot harder to see the individual vehicles and how many observations are actually in that one area.
And another problem is that there are some vehicle classes that do have similar engine sizes and highway MPG values, so it is difficult to tell their individual patterns apart from one another.
Why comparing multiple vehicle classes is difficult:
Overall, I do see that the vehicles with larger engines do generally tend to have lower highway MPG. However, because I have all seven classes just combined into one graph, it is definitely a lot harder to compare the relationships within each class. If we separate them into individual graphs, it would make those patterns a lot easier to see and identify.
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, size = 2) +
facet_wrap(~ class) +
labs(
title = "Engine Displacement vs. Highway MPG by Vehicle Class",
x = "Engine Displacement (Liters)",
y = "Highway Miles Per Gallon (MPG)"
) +
theme_minimal()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.
Part 2 Answers
Comparison with the original scatterplot:
Comparing this to my original scatter plot, I do see that this version is easier to read because each vehicle class does have its own separate graph. Instead of having all seven colors mixed together, I can focus on one vehicle class at a time.
How faceting improves readability:
Using facet_wrap() separates the vehicle classes into different panels, and it also keeps the same axes. What this does is it makes it easier for me to see where the points are concentrated and be able to compare the different classes without having everything just overlap in one graph.
Patterns that are easier to recognize:
Now I am able to notice that compact and midsize vehicles generally have smaller engines and higher highway MPG, while the SUVs and pickups tend to have larger engines and lower MPG. I also can see that subcompact vehicles have a wide range of MPG values, and there’s even some that are above 40 MPG. Another thing that I noticed is that the two seaters have relatively large engines, but they still get around 23 to 26 MPG in this specific dataset.
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, 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 (MPG)"
) +
theme_minimal()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.
Part 3 Answers
How the visualization is organized:
My facet_grid() graph has seven rows that represent vehicle classes and also three columns that represent drive types. Each panel shows the relationship between engine displacement and highway MPG for that specific combination. There are some panels that are empty just because certain combinations of vehicle class and drive type aren’t in the dataset.
How facet_grid() differs from facet_wrap():
The main difference between the two is that facet_wrap() separates the graph using one category, which was vehicle class, while facet_grid() uses two categories, vehicle class and drive type, and uses those to create rows and columns. This then gives me more specific information, which is a good thing, but it’s also creating more panels.
Additional insights from using two categorical variables:
Some additional insights that I did notice are that all the pickups in this dataset are four wheel drive, all the minivans are front wheel drive, and all the two seaters are rear wheel drive. I also noticed that subcompact vehicles appear in all three of the drive types, with their front wheel drive versions generally having smaller engines and higher highway MPG than the rear wheel drive versions. Overall, it just makes it a lot easier to see the differences that just weren’t as obvious in the original scatterplot I created.
Part 4: Critical Thinking and Comparison
Answer the following questions:
What makes the original scatter plot overcrowded or difficult to interpret?
How does
facet_wrap()help address the problem?What additional information does
facet_grid()provide?Which of the three visualizations would you recommend for communicating the relationship between engine displacement and highway MPG? Justify your answer.
Are there any disadvantages to using too many facets?
Part 4 Answers
1. What makes the original scatter plot overcrowded or difficult to interpret?
The original scatter plot has all of the seven vehicle classes together in one graph, and the way it separates them is just with different colors. Some of the points also overlap, and several of the classes have similar engine sizes and highway MPG values. So, it just makes it harder to distinguish the individual classes and really understand the patterns.
2. How does facet_wrap() help address the problem?
facet_wrap() actually separates the vehicle classes into individual panels instead of putting everything all together. This is able to reduce the overlapping points from different classes, and it makes it a lot easier to compare their patterns. For example, I was able to clearly see that SUVs and pickups generally have larger engines and lower highway MPG than compact vehicles.
3. What additional information does facet_grid() provide?
facet_grid() is also separating the vehicles by both class and drive type, so now I’m able to examine how highway MPG and engine displacement actually differ between four wheel drive, front wheel drive, and rear wheel drive vehicles within each class. For example, here I was able to notice that front wheel drive subcompact vehicles generally had higher MPG than rear wheel drive subcompacts.
4. Which of the three visualizations would you recommend for communicating the relationship between engine displacement and highway MPG? Justify your answer.
I personally would recommend the facet_wrap() visualization because I think it just gives the best balance between detail and the actual readability. It’s able to separate the seven vehicle classes so that their patterns are easier to understand without creating as many panels as facet_grid(). The original graph is a lot more crowded, and while facet_grid() is useful for more detailed comparisons, it can be overwhelming.
5. Are there any disadvantages to using too many facets?
Yes. I think there are disadvantages because if you use too many facets, it can make the individual panels smaller and harder to read themselves. Another disadvantage is that it can create empty panels and spread the information across way too many sections, which makes the comparisons more difficult. I saw this in my facet_grid() graph, where several of the class and drive type combinations were empty.