library(ggplot2)
data(mpg)
ggplot(data = 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"
)Independent Learning Activity
From Overcrowded Scatter Plots to Effective Faceted Visualizations
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.
# Issues with the scatter plot
# The scatter plot contains many points and different colors, making it difficult to distinguish between vehicle classes. Some points overlap, which makes it hard to identify how many vehicles are in certain areas of the graph. The different colors can make the graph confusing. The legend takes up additional space and requires the viewer to constantly look back and forth.# Why is comparing multiple vehicle classes difficult?
# Comparing multiple vehicle classes in one scatter plot is difficult because all the data points are displayed in the same area. The overlapping points and multiple colors make it harder to identify patterns for individual vehicle classes.Part 2: Improve the Visualization Using facet_wrap()
Now, improve your original scatter plot using facet_wrap().
Create separate panels for each vehicle class.
# Create scatter plot using facet_wrap
ggplot(data = mpg, aes(x = displ, y = hwy)) +
geom_point(color = "blue", size = 2) +
facet_wrap(~ class) +
labs(
title = "Engine Displacement vs. Highway MPG by Vehicle Class",
x = "Engine Displacement (Liters)",
y = "Highway Miles Per Gallon"
) +
theme_minimal()# How does this visualization compare with the original?
# This scatter plot is easier to understand because each vehicle class has its own separate panel. Instead of displaying all the points together, the graph separates the information into smaller sections.# How does faceting improve readability?
# Faceting organizes the data into separate panels based on vehicle class. Each panel displays only the vehicles belonging to that category.# What patterns or relationships are easier to recognize?
# Vehicles with larger engine displacement generally have lower highway MPG.Compact and subcompact vehicles tend to have higher highway MPG. SUVs and pickup trucks generally have lower highway MPG. Different vehicle classes show different relationships between engine size and fuel efficiency.3: Improve the Visualization Using facet_grid()
Create another visualization using facet_grid().
Use vehicle class as rows and drive type (drv) as columns.
# Create scatter plot using facet_grid
ggplot(data = mpg, aes(x = displ, y = hwy)) +
geom_point(color = "green", size = 2) +
facet_grid(class ~ drv) +
labs(
title = "Engine Displacement vs. Highway MPG by Vehicle Class and Drive Type",
x = "Engine Displacement (Liters)",
y = "Highway Miles Per Gallon"
) +
theme_minimal()# How is the visualization organized?
# Each row represents a different vehicle class, while each column represents a different drive type: f = Front-wheel drive, r = Rear-wheel drive, 4 = Four-wheel drive. Each panel displays the relationship between engine displacement and highway MPG for a specific combination of vehicle class and drive type.# How does facet_grid() differ from facet_wrap()?
# facet_wrap() creates separate panels based on one categorical variable and automatically arranges them into rows and columns. facet_grid() organizes panels using two categorical variables. One variable determines the rows, and the other determines the columns.# Does organizing the data by two categorical variables provide additional insights?
# Using facet_grid() provides additional information because it allows us to compare vehicle classes and drive types at the same time. It helps identify whether different drive types have different relationships between engine displacement and highway MPG. It also makes it easier to see which combinations of vehicle classes and drive types have higher or lower fuel efficiency.Part 4: Critical Thinking and Comparison
What makes the original scatter plot overcrowded or difficult to interpret?
The original scatter plot is difficult to interpret because it displays all vehicle classes in one graph. The different colors and overlapping points make it difficult to distinguish between categories. It can also be challenging to identify patterns and compare individual vehicle classes because there is too much information displayed in one place.
How does
facet_wrap()help address the problem?facet_wrap()separates the scatter plot into individual panels for each vehicle class. This reduces overcrowding and makes the graph easier to read. It allows viewers to focus on one vehicle class at a time while still comparing the different categories.What additional information does
facet_grid()provide?facet_grid()provides additional information by organizing the graph using two categorical variables: vehicle class and drive type. This allows viewers to compare different combinations of vehicle classes and 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 choose
facet_wrap()because it provides the best balance between simplicity and readability. Each vehicle class has its own panel, making it easier to identify patterns and compare fuel efficiency. The original scatter plot is too crowded, whilefacet_grid()can create too many small panels.Are there any disadvantages to using too many facets?
Using too many facets can make each individual panel smaller and harder to read. It can also make the overall visualization look complicated. Some panels may contain very few data points, making comparisons less useful.