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.

library(ggplot2)
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
✔ lubridate 1.9.5     ✔ tibble    3.3.1
✔ purrr     1.2.1     ✔ tidyr     1.3.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(dplyr)
library(dslabs)

data(mpg)

ggplot(mpg, aes(x = displ, y = hwy, color = class)) + 
  geom_point() + 
  labs(
    title = "Engine Displacement vs Highway MPG",
    x = "Engine Displacement (Liters)",
    y = "Highway MPG",
    color = "Vehicle Class"
  ) 

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.

  1. The points are close to each other, so when you try to pinpoint it you lose track
  2. The colors make the graph overwhelming to watch
  3. Its hard to find a pattern for a class becuase all the classes of the vehicles are in the graph.
  • Explain why comparing multiple vehicle classes in one plot may make interpretation difficult.

    When multiple vehicle classes are in one plot, the points are clustered and hard to identify relationships.

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)) + 
  geom_point(color = "blue") + 
  facet_wrap(~ class) + 
  labs(
    x = "Engine Displacement (Liters)",
    y = "Highway MPG"
  )

Your task:

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

  • Explain how faceting improves readability.

Faceting separates classes into different plots reducing overlaping from different classes

  • Identify patterns or relationships that are easier to recognize in the faceted version.

Its easier to see that vehicles with larger engines have lower MPG. The gaps in MPG between vehicles is also clear. ## 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)) + 
  geom_point(color = "green") +
  facet_grid(class~drv) +
  labs(
    x = "Engine Displacement (Liters)",
    y = "Highway MPG"
  )

Your task:

  • Write code and examine how the visualization is organized.

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

facet_grid() organizes data using two categorical variables

facet_wrap() separates data using one categorical variable and arranges panels across multiple rows and columns

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

It does. It allows us to examine whether the relationship between engine displacement and highway MPG differs depending on vehicle class. It also reveals which combinations of vehicle class and drive type are represented in the dataset.

Part 4: Critical Thinking and Comparison

Answer the following questions:

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

    Several vehicles represented by different vehicles, making it hard to identify vehicles and recognize patterns with each classes of vehicles.

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

facet_wrap() separates each vehicle class into its own panel. This reduces overlapping points from different classes and makes it easier to compare patterns in engine displacement and highway MPG.

  1. What additional information does facet_grid() provide?

facet_grid() Allows us to compare two categorical variables simultaneously. By organizing vehicle classes into rows and drive types into columns, we can examine how both factors relate to highway MPG.

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

Honestly, I would use an interactive scatterplot because as useful as the facet_wrap() and facet_grid() are, I think it’s easier to toggle between classes, and it saves way more time coding when you have an interactive chart.

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

Yes, using too many facets can make individual panels too small and difficult to read. It can also make comparisons harder because the viewer has to examine many separate paragraphs. Also, in some of my facets, some of the dots are still clustered close, even worse than the one we had at the scatterplot.