# load data
library(ggplot2)
data(mpg)Independent Learning Activity Data110
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.
- It’s hard to differentiate between which dots belong to each engine displacement. It’s hard to see a trend for each class because they’re all on the same graph, and I think 7 legend colors is a little overcrowded.
Explain why comparing multiple vehicle classes in one plot may make interpretation difficult.
- Comparing multiple vehicle classes in one plot may make interpretation difficult because there are many different classes which makes the graph more cluttered, so it’s hard to focus on one individual color.
# make scatter plot
scatter_plot <- ggplot(mpg, aes(x = displ, y = hwy, color = class)) +
labs(title = "Relationship Between Engine Displacement and Highway Miles Per Gallon") +
xlab("Engine Displacement") +
ylab ("Highway Miles (per gallon)") +
theme_minimal(base_size = 12) +
geom_point() # add the points
scatter_plotPart 2: Improve the Visualization Using facet_wrap()
Now, improve your original scatter plot using facet_wrap().
Create separate panels for each vehicle class.
graph2 <- scatter_plot +facet_wrap(vars(class))
graph2
Your task:
Write code and compare this visualization with the original overcrowded scatter plot.
- This visualization separates each of the classes into their own window. In each window, there is only one class and color, so it’s easier to see the trends.
Explain how faceting improves readability.
- Faceting improves readability by separating each of the classes into their own boxes, so it’s easier to distinguish the trend for each class.
Identify patterns or relationships that are easier to recognize in the faceted version.
- Compact, SUV, subcompact, midsize, and pick up all seem to have downward trends. 2 seaters and mini vans have the least amount of data points, so it’s slightly harder to see the trend. Additionally, minivans seem to have lower engine displacement, while 2steaters seem to have higher engine displacement.
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.
graph2 + facet_grid(vars(class), vars(drv))
Your task:
Write code and examine how the visualization is organized.
- The visualization is organized with the bottom axis being engine displacement, the columns are divided by the drive type, the rows are each class, and the y-axis has the highway miles.
Explain how
facet_grid()differs fromfacet_wrap().- Facet wrap only separates by a single variable, while facet grid uses 2 variables and makes a grid out of that.
Discuss whether organizing the data by two categorical variables provides additional insights.
- Organizing the data by two categorical variables provides additional insights because you’re able to see more specific data points. For example, you can see that there is less rear wheeled driving types in all of the classes. Additionally, you can see that front wheeled driving generally has more highway miles per gallon for each class.
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 was difficult to interpret because there were too many class variables, which made it hard to see individual class trends.
How does
facet_wrap()help address the problem?Facet_wrap() addressed this problem by separating the data into their own window for each class. This makes it easier to compare and notice patterns/trends for each individual window.
What additional information does
facet_grid()provide?Facet_grid() provides additional information about driving type. For example, if it’s 4 wheeled, front wheeled, or rear wheeled.
Which of the three visualizations would you recommend for communicating the relationship between engine displacement and highway MPG? Justify your answer.
I would recommend using facet_wrap() because it separates each of the classes into their own separate windows, so it’s easier to recognize any trends or patterns. Additionally, for this situation, I think facet_grid() is slightly worse than facet_wrap() because I don’t believe adding the driving type is necessary for the relationship. Therefore, adding driving type makes the visualization more confusing for me.
Are there any disadvantages to using too many facets?
One disadvantage could be that it would make the window smaller the more facets someone adds because the graph tries to show all of them. If there are too many facets, which makes each window smaller, then it would be hard for the viewer to clearly see the data points.