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.

library(ggplot2)
data(mpg)
mpg
# A tibble: 234 × 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… f        18    29 p     comp…
 2 audi         a4           1.8  1999     4 manu… f        21    29 p     comp…
 3 audi         a4           2    2008     4 manu… f        20    31 p     comp…
 4 audi         a4           2    2008     4 auto… f        21    30 p     comp…
 5 audi         a4           2.8  1999     6 auto… f        16    26 p     comp…
 6 audi         a4           2.8  1999     6 manu… f        18    26 p     comp…
 7 audi         a4           3.1  2008     6 auto… f        18    27 p     comp…
 8 audi         a4 quattro   1.8  1999     4 manu… 4        18    26 p     comp…
 9 audi         a4 quattro   1.8  1999     4 auto… 4        16    25 p     comp…
10 audi         a4 quattro   2    2008     4 manu… 4        20    28 p     comp…
# ℹ 224 more rows
mpg |>
  ggplot(aes(x = displ, y = hwy, color = class)) +
  geom_point()

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.

    With this scatterplot, it’s difficult to compare the difference in data by vehicle class. The graph is cluttered with the many different colors. Some values may overlap which you won’t be able to see in the graph as is. With all the values together one one graph, you can’t easily make conclusions on patterns relating to the vehicle class.

Part 2: Improve the Visualization Using facet_wrap()

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

Create separate panels for each vehicle class.

mpg |>
  ggplot(aes(x = displ, y = hwy)) +
  geom_point() +
  facet_wrap(~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.

    Here you can clearly differentiate the points by class, unlike the first scatter plot. Faceting improves readability by organizing data into different categories to improve analysis. For 2-seater vehicles, highway mpg seems to hover around 25 mpg. Compact vehicles are about 23 and up with engine displacement between 1.75 and 3.5. For subcompact, midsize, pickup vehicles and SUVs, their graphs show a negative linear association between engine displacement and highway miles per gallon. Minivans and 2-seaters have too few data points to tell.

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.

mpg |>
  ggplot(aes(x = displ, y = hwy)) +
  geom_point() +
  facet_grid(class ~ drv)

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.

    The facet grid function allows you to see more variables; engine displacement, highway mpg, vehicle class, and drive type. With this facet grid, you can compare vehicles within certain classes that have different drive types. Vehicles with front wheel drive tend to have a displacement between 1.5 and 4 (with the exception of an outlier midsize vehicle). Rear wheel drive vehicles have a higher displacement, averaging around 5.5. Vehicles with 4 wheel drive have varying values for engine displacement.

Part 4: Critical Thinking and Comparison

Answer the following questions:

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

    It’s difficult to make distinctions from the different colors.

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

    It splits the data up by creating different graphs for data points within each vehicle class.

  3. What additional information does facet_grid() provide?

    It splits the data up to be even more specific, providing information on vehicle drive type.

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

    I would recommend the facet grid. It covers other contributing factors such as vehicle class and drive type so that you can make general conclusions or more specific conclusions.

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

The graph can get messy and you might lose focus on the relationship you’re trying to visualize.