Independent Learning Activity

Author

Grigory Balasanov

From Overcrowded Scatter Plots to Effective Faceted Visualizations

Learning Objectives

  1. Identify problems associated with overcrowded scatter plots.

  2. Use faceting techniques to improve data visualization.

  3. Apply facet_wrap() and facet_grid() in ggplot2.

  4. 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.

  • Explain why comparing multiple vehicle classes in one plot may make interpretation difficult.

# Loading libraries and the dataset
library(tidyverse)
library(ggplot2)

data(mpg)

# Checking the records
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…
# Showing the count for myself (234)
#count(mpg)
# Creating the scatterplot 
ggplot(mpg, aes(x = displ, y = hwy, color = class)) +
  geom_point(size = 3, alpha = 0.5) +
  labs(title = "Engine Displacement vs. Highway MPG",
       x = "Engine Displacement (Liters)",
       y = "Highway MPG",
       color = "Vehicle Class") +
  theme_minimal()

Part 2: Improve the Visualization Using facet_wrap()

Now, improve your original scatter plot using facet_wrap().

Create separate panels for each vehicle class.

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.

# Basically, I'm using the previous scatterplot and dividing in several panels for each vehicle claass
ggplot(mpg, aes(x = displ, y = hwy, color = class)) +
  geom_point(size = 2, alpha = 0.5) +
  facet_wrap(~ class) +
  labs(title = "Engine Displacement vs. Highway MPG by Vehicle Class",
       x = "Engine Displacement (Liters)",
       y = "Highway MPG") +
  theme_minimal() +
  theme(legend.position = "none") # Here i'm removing the legend since we already have class name for each separate panel

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.

Your task:

  • Write code and examine how the visualization is organized.

  • Explain how facet_grid() differs from facet_wrap().

  • Discuss whether organizing the data by two categorical variables provides additional insights.

# grid of panels: vehicle class as rows, drive type as columns
ggplot(mpg, aes(x = displ, y = hwy, color = class)) +
  geom_point(size = 2, alpha = 0.5) +
  facet_grid(class ~ drv) +
  labs(title = "Engine Displacement vs. Highway MPG by Class and Drive Type",
       x = "Engine Displacement (Liters)",
       y = "Highway MPG") +
  theme_minimal()

Part 4: Critical Thinking and Comparison

Answer the following questions:

  1. What makes the original scatter plot overcrowded or difficult to interpret?

    Question 1 Answer:

    The original scatterplott puts 234 cars and 7 different colors in one plot. Some points overlap with each other, the colors very similar and it’s hard to distinguish them, and the vehicle classes mix together, so it’s pretty hard to see the pattern for a single class.

  2. How does facet_wrap() help address the problem?

    Question 2 Answer:

    The facet_wrap() gives each class its own small panel with the same axes. This way it removes the overlaping between the vehicle classes and we can compare the vehicle classes side by side even if we did not have set different colors.

  3. What additional information does facet_grid() provide?

    Question 3 Answer:

    The facet_grid() adds a second variable “drive type”. This way we can see the relationship between the MPG and both the vehicle class and the drive type. For example, it shows that front-wheel-drive cars have higher MPG and that some classes come with only one drive type.

  4. Which of the three visualizations would you recommend for communicating the relationship between engine displacement and highway MPG? Justify your answer.

    Question 4 Answer:

    I think I would recommend the facet_wrap() version since it shows the relationship clearly, and it’s not too cluttered like the first plot. Also, if we compare facet_wrap() with facet_grid(): facet_grid() has too much empty space. Therefore, I recommend facet_wrap() version.

  5. Are there any disadvantages to using too many facets?

    Question 5 Answer:

    Yes there are some disadvantages if we use too many facets. For example, if we use too many facets, we will have too many small panels, and they are becoming hard to read. Some of these panels may have just a few points or no points at all. Also, we will have to look across many panels to compare everything. All of this can become pretty confusing, hard to read, and cluttered even worse than the original plot.