Dhivyan.G - 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)
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…
dim(mpg)
[1] 234  11

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.

ggplot(data = mpg, aes(x = displ, y = hwy, color = class)) +
  geom_point() +
  labs(
    title = "Engine Displacement vs. Highway MPG for all the Vehicle Class",
    x = "Engine Displacement (L)",
    y = "Highway Miles per Gallon (MPG)",
    color = "Vehicle Class"
  ) +
  theme_minimal()

Part 1: Answers

The problem with above plot is:

1) Having all the vehicles in one plot makes it hard to read and detect the correlation for each.

2) Different vehicle plots are overlapping with each other

3) Readers have to constantly map the color to the right vehicle.

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.

ggplot(data = mpg, aes(x = displ, y = hwy, color = class)) +
  geom_point() +
  facet_wrap(~ class, nrow = 3) +
  labs(
    title = "Engine Displacement vs. Highway MPG for all the Vehicle Class",
    x = "Engine Displacement (L)",
    y = "Highway Miles per Gallon (MPG)"
  ) +
  theme_minimal() 

Part 2: Answer

Facets make it easy to read each vehicle in its own plot and makes it easy to see the correlation for each vehicle. The plot is not overcrowded.

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.

ggplot(data = mpg, aes(x = displ, y = hwy, color = class)) +
  geom_point(size = 2, alpha = 0.8) +
  facet_grid(class ~ drv) +
  labs(
    title = "Engine Displacement vs. Highway MPG for all the Vehicle Class",
    x = "Engine Displacement (L)",
    y = "Highway Miles per Gallon (MPG)"
  ) 

Part 3: Answers

facet wrap breaks down the correlation for each category and provides a clear visualization of correlation

facet grid gives the ability to add another category to see how this category affects the correlation.

Part 4: Critical Thinking and Comparison

Answer the following questions:

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

    Having all the vehicles in one plot makes the data overcrowded and too many color overlaps and hard to read and interpret.

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

    It creates one grid for each vehicles making it easy to read.

  3. What additional information does facet_grid() provide?

    It provides the ability to add another category (drv) and see how that affects the correlation

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

    For this data, facet wrap is good - this data is too little for facet grid and hence some of the grids are empty.

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

Too many facets makes it hard to read and also if there is not enough data, then the grids may be empty or may not provide enough meaning.