Independent Learning Activity

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.

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.

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.

Part 4: Critical Thinking and Comparison

Answer the following questions:

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

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

  3. What additional information does facet_grid() provide?

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

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


Assignment

Part 1

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.2.1     ✔ readr     2.2.0
✔ forcats   1.0.1     ✔ stringr   1.6.0
✔ ggplot2   4.0.3     ✔ tibble    3.3.1
✔ lubridate 1.9.5     ✔ tidyr     1.3.2
✔ purrr     1.2.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
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…
p = ggplot(mpg, aes(x = displ, y = hwy, color = class)) +
  geom_point() +
  labs(
    title = "Relationship Between Engine Displacement and Highway Miles per Gallon by Vehicle Class", 
    x = "Engine Displacement", 
    y = "Highway Miles per Gallon", 
    color = "Vehicle Class"
  ) +
  theme_minimal(base_family = "serif")
p

Though the visualization may look fine at first glance, it contains issues that complicate one’s ability to interpret the information. As the dataset is relatively small, the visual can look simple enough to read; however, an abundance of data would lead to data overlapping and an overall messy chart. Additionally, the x-axis labels engine displacement solely as whole numbers even though they are expressed as decimals as well, making it frustrating to understand the exact value of each point. Fortunately, this issue can be resolved by making this visualization interactive and displaying the exact value of each data point.

The data was color-coded by vehicle classification; however, since there are 7 different classifications, grasping the data of each one is difficult when they are compiled together on one singular visualization and overlapping one another.


Part 2

p + facet_wrap(vars(class), scales = "free", nrow = 3)

Incorporating the facet_wrap function into the code separates the scatter-plot by vehicle classification. Isolating the data in this manor allows for increased readability and comprehension. With the classifications separated, two clear patterns emerge:

1) pickup, subcompact, and SUV charts display a downward trend - as engine displacement goes up, highway miles per gallon decreases.

2) 2seater and minivan classifications do not exhibit a strong correlation.


Part 3

p + facet_grid(rows = vars(class), cols = vars(drv), space = "free") 

Incorporating the facet_grid function separates the data into a two-dimensional matrix, organizing the vehicle classifications into rows and drive types (4, f, and r) columns. Facet_grid keeps empty combinations (ex. 4 and 2seater), highlighting combination gaps within the dataset. Visualizing the data across two categorical variables makes it easier to analyze combination possibilities; in this case, vehicle class and drive type. For example 2seater vehicles in this dataset exclusively use rear-wheel drive (r).


Part 4

The original scatter-plot was overcrowded due to the volume of data making it difficult to read and comprehend. Utilizing the facet_wrap function isolated the data into separate vehicle class charts leading to easier comprehension by eliminating the issue of overcrowding and overlap. Facet_grid also isolated the data, but in a two-dimensional matrix while organizing the data into additional categorical variables. Doing this further analyzes additional combinations (in this case vehicle class and drive type) while continuing to evaluate the original relationship between highway miles per gallon and engine displacement. Among the three visualization options, I would recommend the facet_wrap visual for analyzing the relationship between highway miles per gallon and engine displacement. Although, the facet-grid visual provided additional data and details, the facet-wrap is easy to understand and analyze the relationship without an overwhelming amount of information.

A disadvantage to using too many facets is unintentionally incorporating an excessive amount of unnecessary data, muddying the analysis and making the visual harder to understand. Also, we previously saw that facet_grid preserves empty combinations by displaying empty charts. Though this can be useful in identifying data gaps, it can also make it difficult to directly compare the charts containing actual data with the empty charts sitting in between.