Independent Learning Activity

Author

Nathan Pak

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.

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
data(mpg)

ggplot(mpg, aes(x = displ, y = hwy, color = class)) +
  geom_point() +
  labs(
    title = "Engine Displacement vs. Highway Miles Per Gallon by Vehicle Class",
    x = "Engine Displacement (L)",
    y = "Highway Miles Per Gallon",
    color = "Vehicle Class"
  ) +
  theme_minimal()

Your task:

  • Write code and examine the resulting scatter plot.

  • Identify at least three problems associated with this visualization.

    • There are overlap points from having the same engine displacement and highway miles per gallon, which means it would not show the accurate number of points in the data.

    • The overlap and lack of distinct colors make it not possible to distinct individual trends from a single vehicle class.

    • Gives a misleading trend for all points, visually showing a negative slope even when it is made up of different individual trends.

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

    • Because points are overlapping in such plot, it is difficult to interpret each individual trend from each individual vehicle class when not all points are visible. Even without overlap, there could be many spread out points for one class that are difficult to visually interpret when there are so many other points from other 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.

ggplot(mpg, aes(x = displ, y = hwy, color = class)) +
  geom_point() +
  facet_wrap(~ class) +
  labs(
    title = "Engine Displacement vs. Highway Miles Per Gallon by Vehicle Class",
    x = "Engine Displacement (L)",
    y = "Highway Miles Per Gallon"
  ) +
  theme_minimal() +
  theme(legend.position = "none")

Your task:

  • Write code and compare this visualization with the original overcrowded scatter plot.

  • Explain how faceting improves readability.

    • This improves readability by having each point visible for each class, giving accurate visual trends by vehicle class
  • Identify patterns or relationships that are easier to recognize in the faceted version.

    • The compact, minivan, midsize, and subcompact class more grouped on the left, while suv and pickup are more evenly distributed, and 2seater is more grouped on the right.

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, color = class)) +
  geom_point() +
  facet_grid(class ~ drv) +
  labs(
    title = "Engine Displacement vs. Highway Miles Per Gallon by Class and Drive Type",
    x = "Engine Displacement (L)",
    y = "Highway Miles Per Gallon"
  ) +
  theme_minimal() +
  theme(legend.position = "none")

Your task:

  • Write code and examine how the visualization is organized.

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

    • facet_wrap is more for differentiating a single variable (vehicle class), while facet grid creates a matrix differentiating two variables (vehicle class and drive type).
  • Discuss whether organizing the data by two categorical variables provides additional insights.

    • This provides additional insights about the type of drive of each vehicle class, which can be correlated to the highway miles per gallon output.

Part 4: Critical Thinking and Comparison

Answer the following questions:

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

    • Overlapping datapoints and many different color categories mapped on a single grid cause many data points to not appear which creates misleading visual trends.
  2. How does facet_wrap() help address the problem?

    • facet_wrap isolates each vehicle class which prevents overlap between points with other classes to more accurately visualize the data.
  3. What additional information does facet_grid() provide?

    • facet_grid gives an additional variable to differentiate the data by, which allowed for further insights to further explain the data.
  4. Which of the three visualizations would you recommend for communicating the relationship between engine displacement and highway MPG? Justify your answer.

    • Specifically for the relationship between engine displacement and highway MPG, I would recommend facet_wrap for clean and simple differentiation of vehicle class. But if it were a full presentation delving deep into the topic I would use facet_grid to bring further insights to break down potential factors or causes, although it may be too clustered vs. a more simple facet_wrap.
  5. Are there any disadvantages to using too many facets?

    • Too many facets can make too many categories, so without a presentation or explanation it can make it lose what the actual comparison was in first place, as well as of course cluttering up the visuals to make it harder to read.