Lab 5



Labs

Overview

This lab focuses on refining visualizations for comparing categories through effective ordering, theme customization, and strategic annotations. You’ll practice creating publication-ready visualizations that effectively communicate categorical comparisons. This is designed as an in-class 1-hour lab.

Learning Objectives

By the end of this lab, you will be able to:

Required Packages

library(tidyverse)
library(gapminder)
library(ggrepel)
library(gghighlight)
library(patchwork)

Data

We’ll be working with the gapminder dataset, focusing on countries in the Americas.


Part 1: Ordering Categories Effectively

Task 1.1

Using the gapminder dataset filtered to year 2007 and continent “Americas”, create a dot plot showing GDP per capita by country.

Requirements:

Hint: Use fct_reorder() to order countries by GDP.

Task 1.2

Convert your dot plot from Task 1.1 into a lollipop chart by adding line segments from 0 to each point.

Requirements:

Question: In 1-2 sentences, explain why ordering by value is more effective than alphabetical ordering for this visualization.


Part 2: Theme Customization

Task 2.1

Creat a plot showing life expectancy over time for countries in the Americas.

Requirements:

Task 2.2

Customize the theme of your plot from Task 2.1 by modifying at least 5 theme elements.

Requirements must include:

  1. Plot title (size, face, color)
  2. Axis titles (size or face)
  3. Panel grid (modify or remove major/minor)
  4. Plot or panel background color
  5. Legend position (if you add one)

Make intentional design choices that improve readability.

Task 2.3

Create a custom theme function called theme_yourname() that encapsulates your customizations from Task 2.2.

Requirements:

Example structure:

theme_yourname <- function(base_size = 14) {
  theme_minimal(base_size = base_size) +
  theme(
    # Your customizations here
  )
}

Part 3: Annotations

Task 3.1

Using your Americas life expectancy plot from Task 2.1, add labels to identify specific countries using gghighlight.

Requirements:

Task 3.2

Create an alternative version using ggforce::geom_mark_ellipse() instead of gghighlight to highlight outliers.

Requirements:

Hint: You’ll need to filter the data to just those countries for geom_mark_ellipse()

Question: Which annotation method (Task 3.1 with gghighlight or Task 3.2 with geom_mark_ellipse) do you prefer for this data and why? Write 2-3 sentences.


Part 4: Annotations with ggrepel

Task 4.1

Create a scatter plot using gapminder data filtered to 2007 and Americas showing GDP per capita (x-axis, log scale) vs. life expectancy (y-axis).

Requirements:

Task 4.2

Add labels to the plot from Task 4.1 using geom_text_repel().

Requirements:

Hint: Create a variable to identify which countries should be labeled, then use that in your aesthetics and geom_text_repel().


Part 5: Compound Figures

Task 5.1

Create two plots that tell a story about the Americas:

Combine them using patchwork side-by-side.

Requirements:

Task 5.2

Experiment with the layout from Task 5.1.

Requirements:

Question: Which layout (side-by-side, stacked, or different widths) works best for these two plots? Why?


Part 6: Saving Your Work

Task 6.1

Save your best compound figure from Part 5.

Requirements:

Include the ggsave() code in your Rmd file.


Reflection Questions (No need to write- we will discuss in class next week)

Answer the following (2-3 sentences each):

  1. Ordering: When visualizing categorical data, what are two situations where you would NOT want to order by value?

  2. Themes: How did creating a custom theme function help with consistency? What’s one advantage of using a theme function vs. copying theme code?

  3. Annotations: Compare direct labeling (like gghighlight) versus legends. What’s one situation where you’d prefer each approach?


Finishing Up

When you have finished the above, upload your rendered PDF file to Canvas.

What to Submit