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.

#load libraries
library(ggplot2)
#examine data
str(mpg)
tibble [234 × 11] (S3: tbl_df/tbl/data.frame)
 $ manufacturer: chr [1:234] "audi" "audi" "audi" "audi" ...
 $ model       : chr [1:234] "a4" "a4" "a4" "a4" ...
 $ displ       : num [1:234] 1.8 1.8 2 2 2.8 2.8 3.1 1.8 1.8 2 ...
 $ year        : int [1:234] 1999 1999 2008 2008 1999 1999 2008 1999 1999 2008 ...
 $ cyl         : int [1:234] 4 4 4 4 6 6 6 4 4 4 ...
 $ trans       : chr [1:234] "auto(l5)" "manual(m5)" "manual(m6)" "auto(av)" ...
 $ drv         : chr [1:234] "f" "f" "f" "f" ...
 $ cty         : int [1:234] 18 21 20 21 16 18 18 18 16 20 ...
 $ hwy         : int [1:234] 29 29 31 30 26 26 27 26 25 28 ...
 $ fl          : chr [1:234] "p" "p" "p" "p" ...
 $ class       : chr [1:234] "compact" "compact" "compact" "compact" ...
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…
#create scatterplot
plot1 <- ggplot(mpg, aes(x = displ, y = hwy, color = class)) +
  labs(x = "Engine Displacement", y = "Highway (miles per gallon)",
       title = "Engine Displacement v. Highway (miles per gallon)") +
  geom_point()
plot1

The plot is overcrowded, making it unclear. Additionally, it is hard to tell what the relationship between engine displacement and highway miles per gallon is for cars. It can also be a little flat, it is difficult to figure out what the graph convey.

The colors, while they do differenciate the points by class, do not help much to see an particular to show the relationship between displacement and highway miles per hour. Having them all on a single graph prevents viewers from understanding the relationship.

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.

#plot with facet wrap
plot2 <- plot1 + facet_wrap(vars(class))
plot2

The facet wrapped plots separate each plot by class, while still showing relationship between engine displacement and highway miles per gallon. The regular scatter plot did not separate class into different plots.

Faceting improves readability by making variables into their own graphs. By doing so, relationships of different variables can be compared side-by-side.

It can be seen that some car types have a clearer relationship between engine displacement and highway miles per hour. For example, it can be seen the subcompact seems to have a greater engine displacement at lower highway miles per gallon. However, there seems to be no clear relationship for others. For example, the midsize seems seems to have differing engine displacement at the same highway miles per gallon

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.

#supply variables to rows and columns
plot3 <- plot1 + 
  facet_grid(rows = vars(class))
plot3 <- plot1 +
  facet_grid(cols = vars(drv))
plot3 <- plot1 + facet_grid(vars(class), vars(drv))
plot3

The visualization is organized into multiple plots, with each graph showing the relationship between engine displacement and highway miles per gallon for each type of car class and drive type.

Facet wrap only separates the graph with a single variable type, while facet grid separates the graph into 2 variable types.

Separating the graph into multiple variables provides some additional insight. It can be vaugely seen that front drive type results in higher highway miles per gallon.

Part 4: Critical Thinking and Comparison

Answer the following questions:

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

    The vehicle classes are not well organized, it is hard to see how vehicle class influences the relationship between engine displacement and highway miles per gallons.

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

    Facet wrap helps to address this problem by separating the vehicle class types into separate graphs.

  3. What additional information does facet_grid() provide?

    Facet grid provides the additional variable drv to compare, providing additional insight into factors that influence relationship between engine displacement and highway miles per gallon.

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

    I would use facet wrap. Both facet wrap and facet grid organize class type, making the graph more clear. However, between facet wrap and facet grid, I believe that facet wrap is the best. While facet grid adds the additional variable to consider, it doesn’t provide enough additional information to justify the messiness of the extra graphs.

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

    Having too many facet could make the visualization less comprehensible. It can be better to keep visualizations simple, rather than adding too many variables and becoming hard to interpret.