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.
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)
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…
library(ggplot2)

mpg_plot <- ggplot(mpg, aes(x = displ, y = hwy, color = class)) +
  labs(title = "Engine Displacement vs. Highway Miles") +
  theme_minimal() +
  xlab("Engine Displacement (liters)") +
  ylab("Highway Miles (gallons)")
mpg_plot + geom_point()

  • Identify at least three problems associated with this visualization.

    1. Overcrowded
    2. Multiple trends
    3. Hard to compare different classes
  • Explain why comparing multiple vehicle classes in one plot may make interpretation difficult.

    Comparing multiple vehicle classes may make interpretation difficult on one plot as there is difficulty in visualizing trend of each subgroup individually.

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.
mpg_plot + 
  geom_point(size = 1) +
  facet_wrap(~class)

  • Explain how faceting improves readability.

    Using facet_wrap() improves readability as each subgroup can be visualized and analyzed individually.

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

    Thanks to the faceted version we can see that the engine displacement of a 2seater or minivan has little impact on its highway miles. Overall, there is a slight downward trend among the majority of cars however compact and subcompact have the most drastic ranges when compared to midsize, pickup, and suv.

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.
mpg_plot +
  geom_point(size = 1) +
  facet_grid(class~drv)

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

    The code facet_grid() differs from facet_wrap() because the former is useful in analyzing two variables and the later helps analyze the influence of one variable.

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

    Organizing the data by two categorical variables, in this case the type of car and drive type, gave additional insight on the proportion of data represented by each variable to which it could be seen that the trend of the scatterplot is not only influenced by the car’s class type but also drive type.

Part 4: Critical Thinking and Comparison

Answer the following questions:

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

    It is harder to interpret the individual trends of each subgroup in the original scatterplot.

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

    The code facet_wrap() helps address an overcrowded scatterplot by providing clearer subgroup comparison.

  3. What additional information does facet_grid() provide?

    The additional information .facet_grid() provides is that class is not the only influence on the trend observed, the scatterplot also represents drv.

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

    The visualization I would recommend for communicating the relationship between engine displacement and highway MPG is the facet_grid() plot. This is due to the individually represented subgroups additional to the clear distinction of the data’s trend not only being influenced by class but also drive type to which once can clearly see the proportion of data represented by class.

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

    The disadvantages to using too many facets may be clutter making the data harder to read and easily misinterpreted.