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:

  1. Write code and examine the resulting scatter plot.

  2. Identify at least three problems associated with this visualization.

  3. Explain why comparing multiple vehicle classes in one plot may make interpretation difficult.

    # Load ggplot2
    library(ggplot2)
    
    # Load the mpg dataset
    data(mpg)
    
    # Create a scatter plot
    ggplot(mpg, aes(x = displ, y = hwy, color = class)) +
      geom_point() +
      labs(
        title = "Engine Displacement vs. Highway MPG",
        x = "Engine Displacement (L)",
        y = "Highway Miles Per Gallon",
        color = "Vehicle Class"
      )

  1. There are too many different colors, making it difficult to distinguish between vehicle classes.

  2. Several points overlap, making it hard to see individual observations.

  3. Comparing patterns between vehicle classes is difficult because all the data is displayed in one graph.

Displaying all vehicle classes in one scatter plot makes the graph overcrowded. The different colors can become confusing, and overlapping points make it difficult to identify trends within individual vehicle classes.

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.

    # Create separate panels for each vehicle class
    ggplot(mpg, aes(x = displ, y = hwy)) +
      geom_point() +
      facet_wrap(~ class) +
      labs(
        title = "Engine Displacement vs. Highway MPG by Vehicle Class",
        x = "Engine Displacement (L)",
        y = "Highway Miles Per Gallon"
      )

Using facet_wrap() separates the vehicle classes into individual panels. Instead of displaying every vehicle class in one graph, each class has its own scatter plot.

This makes the visualization easier to read because there is less visual clutter. It also makes it easier to compare the relationships between engine displacement and highway MPG for different vehicle classes.

Patterns that are easier to recognize:

Vehicles with larger engine displacements generally have lower highway MPG.

Compact and subcompact cars generally have better highway fuel efficiency.

SUVs and pickup trucks tend to have larger engines and lower highway MPG.

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.

# Create panels using vehicle class and drive type
ggplot(mpg, aes(x = displ, y = hwy)) +
  geom_point() +
  facet_grid(class ~ drv) +
  labs(
    title = "Engine Displacement vs. Highway MPG by Class and Drive Type",
    x = "Engine Displacement (L)",
    y = "Highway Miles Per Gallon"
  )

Does organizing the data by two categorical variables provide additional insights?

Yes. It allows us to compare vehicles based on both their class and drive type. We can see how front-wheel drive, rear-wheel drive, and four-wheel drive vehicles differ within different vehicle classes. This provides more information than simply comparing vehicle classes because we can also examine whether drive type is associated with differences in engine displacement and highway MPG.

Part 4: Critical Thinking and Comparison

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

The original scatter plot is overcrowded because it displays all vehicle classes in one graph. Having many different colors makes it difficult to distinguish between groups. Overlapping points also make it harder to identify individual observations and recognize patterns.

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

facet_wrap() separates the data into individual panels based on vehicle class. This reduces visual clutter and makes it easier to identify patterns within each vehicle class. It also makes comparisons between groups easier.

3. What additional information does facet_grid() provide?

facet_grid() provides additional information by organizing the data using two categorical variables instead of one. In this example, it separates vehicles by class and drive type. This allows us to compare different drive types within each vehicle class.

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 facet_wrap() because it separates the vehicle classes into individual panels without making the visualization too complicated. It makes it easier to recognize patterns and compare fuel efficiency between different vehicle classes. It also avoids the extra complexity of organizing the data by two categorical variables.

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

Yes. Using too many facets can make each individual panel too small, making the data difficult to see. It can also make the overall visualization overwhelming because there are too many separate graphs to compare. Therefore, it is important to choose meaningful categories when creating facets.