Lab 4: Time Series, Trends & Relationships

STAT 80B - Data Visualization

Lab 4 Overview

Today’s Focus:

  • Time series with trend lines
  • Scatter plots (bivariate and multivariate)
  • Multiple series: overlay vs. small multiples
  • Annotated, story-driven visualizations

Tools: Tableau and R β€” equally
Dataset: World Indicators (World Bank data)

🚨 Critical Reminder 🚨

This Course Is NOT About Learning Software

This course teaches you to THINK about visualizations, not just use tools.

  • How to choose the right chart for your story
  • How to design effective, readable graphics
  • How to communicate with data

The tool is just that β€” a TOOL.
Learn it through practice, trial and error, and experimentation.

The Dataset: World Indicators

Same data, two tools:

  • Tableau β†’ built-in World Indicators sample workbook
  • R β†’ wbstats package, pulling live from the World Bank API

Both draw from the same source: World Bank open data
Country-level indicators from the 1960s to the present

Health Β· Economics Β· Demographics Β· Environment Β· Technology

Your First Decision: Pick a Focus

Choose ONE region OR one theme β€” and stick with it.

By Region πŸ—ΊοΈ

  • Latin America & Caribbean
  • Sub-Saharan Africa
  • East Asia & Pacific
  • Europe & Central Asia
  • South Asia

By Theme πŸ”

  • πŸ’Š Health
  • πŸ’° Economic Development
  • 🌿 Environment
  • 🌐 Technology & Connectivity
  • πŸ‘₯ Demographics

Write your choice at the top of your submission.

Accessing the Data in Tableau

  1. Open Tableau Desktop
  2. From the Start screen β†’ Sample Workbooks β†’ World Indicators
    OR: Connect β†’ Saved Data Sources β†’ World Indicators.tds
  3. Explore the Data Source tab before building anything

Tip

Key fields to know: Country / Region, Year, Life Expectancy at Birth, GDP per Capita, CO2 Emissions, Fertility Rate, Internet Users %, Population Total

Accessing the Data in R

install.packages("wbstats")  # run once

library(wbstats)
library(dplyr)
library(ggplot2)

# Search for indicator codes
wb_search("life expectancy")
wb_search("GDP per capita")

# Download your data
data <- wb_data(
  indicator = c(
    life_exp  = "SP.DYN.LE00.IN",   # Life expectancy
    gdp_pc    = "NY.GDP.PCAP.CD",   # GDP per capita
    fertility = "SP.DYN.TFRT.IN",   # Fertility rate
    co2       = "EN.ATM.CO2E.PC",   # CO2 per capita
    internet  = "IT.NET.USER.ZS",   # Internet users (%)
    pop       = "SP.POP.TOTL"       # Population
  ),
  country = c("CRI", "BRA", "MEX", "CHL", "COL"),  # ISO3 codes
  start_date = 1990,
  end_date = 2022
)
# Find ISO3 codes for your region
wb_countries() |> 
  select(iso3c, country, region) |>
  filter(region == "Latin America & Caribbean")

Lab Structure

Complete at least 3 of 5 tasks.

Task Focus
1 Basic time series line graph
2 Add a trend line
3 Scatter plot β€” two indicators
4 Multiple series: overlay vs. small multiples
5 Annotated visualization β€” tell a story

Quality over quantity. Depth over breadth.

Task 1 & 2: Time Series + Trend Lines

Task 1: Line graph of one indicator over time for your countries
Task 2: Add a trend line β€” try at least two model types

In Tableau:

  • Year β†’ Columns, measure β†’ Rows, Country β†’ Color
  • Analytics pane β†’ drag Trend Line β†’ try linear vs. polynomial

In R:

ggplot(data, aes(x = date, y = life_exp, color = country)) +
  geom_line(linewidth = 1) +
  geom_smooth(method = "lm", se = TRUE) +   # swap for method = "loess"
  labs(title = "Life Expectancy Over Time",
       x = NULL, y = "Life Expectancy (years)", color = NULL) +
  theme_minimal()

Tip

Try span = 0.3 vs. span = 0.7 with LOESS β€” what changes?

Task 3: Scatter Plot β€” Two Indicators

The classic Gapminder view: relationship between two indicators for one year

In Tableau:

  • One indicator β†’ Columns, another β†’ Rows, mark type β†’ Circle
  • Population Total β†’ Size, Country β†’ Label
  • Filter to a single year; try Pages to animate across years

In R:

data_2019 <- data |> filter(date == 2019)

ggplot(data_2019, aes(x = gdp_pc, y = life_exp, 
                       size = pop, label = country)) +
  geom_point(alpha = 0.6, color = "steelblue") +
  geom_text(size = 2.5, vjust = -0.8, check_overlap = TRUE) +
  scale_x_log10(labels = scales::label_dollar(suffix = "K", scale = 1/1000)) +
  scale_size_continuous(range = c(2, 14), guide = "none") +
  labs(title = "GDP per Capita vs. Life Expectancy (2019)",
       x = "GDP per Capita (log scale)", y = "Life Expectancy (years)") +
  theme_minimal()

Task 4: Overlay vs. Small Multiples

Same data, two layouts β€” which works better?

Overlay

  • All countries on one plot
  • Color = country
  • Good for: direct comparison of levels
ggplot(data, aes(x = date, y = life_exp, 
                  color = country)) +
  geom_line(linewidth = 1) +
  scale_color_brewer(palette = "Set2") +
  theme_minimal()

Small Multiples (Facets)

  • One panel per country
  • Good for: seeing each trend clearly
ggplot(data, aes(x = date, y = life_exp)) +
  geom_line(color = "steelblue") +
  facet_wrap(~ country, ncol = 3) +
  theme_minimal()

Tip

Try scales = "fixed" vs. scales = "free_y" β€” when does each make sense?

Task 5: Tell a Story with Annotations

Take one of your visualizations and add annotations to highlight a specific pattern, event, or finding.

In Tableau: Right-click a data point β†’ Annotate β†’ Mark; drag Reference Lines for key years

In R:

highlight <- data |> filter(country == "Costa Rica", date == 2020)

ggplot(data, aes(x = date, y = life_exp, color = country)) +
  geom_line(linewidth = 1, alpha = 0.7) +
  geom_vline(xintercept = 2020, linetype = "dashed", color = "gray40") +
  annotate("text", x = 2020.3, y = 68, label = "COVID-19", 
           hjust = 0, size = 3, color = "gray40") +
  geom_point(data = highlight, aes(x = date, y = life_exp),
             color = "red", size = 4, shape = 21, fill = "white", stroke = 2) +
  labs(title = "Life Expectancy with Annotation",
       x = NULL, y = "Life Expectancy (years)", color = NULL) +
  theme_minimal()

Reflection Questions

As you work, keep asking yourself:

  1. What story am I trying to tell?
  2. Does the trend line fit well β€” and what does a poor fit mean?
  3. Is overlay or small multiples clearer for my data?
  4. Did my annotation help focus the viewer, or just add clutter?
  5. What did Tableau make easy that R didn’t β€” and vice versa?

Common Issues & Solutions

wb_data() returns empty β†’ Check ISO3 codes with wb_countries() and indicator codes with wb_search()

Lots of NAs β†’ Some indicators are sparse before ~1990; narrow your date range

Spaghetti chart β†’ Filter to 4–6 countries, or switch to small multiples

Scatter plot outliers compress everything β†’ Try scale_x_log10() or scale_y_log10()

Tableau trend lines won’t show β†’ Make sure Year is set as a continuous date

Deliverable

Submit ONE polished visualization β€” from Tableau or R, whichever told your story better.

Include:

  1. The visualization (exported image or screenshot)

    • Tableau: Worksheet β†’ Export β†’ Image
    • R: ggsave("myplot.png", width = 10, height = 6, dpi = 300)
  2. Your region/theme choice and which countries/indicators you used

  3. 3–5 sentences on: what pattern you found, why this chart type, and what you noticed comparing Tableau vs. R

Grading: Technical execution (30%) Β· Design quality (40%) Β· Reflection (30%)

Resources

Tableau: Public Gallery Β· How-to Videos Β· Help β†’ Sample Workbooks β†’ World Indicators

R: wbstats docs Β· World Bank Data portal Β· R Graph Gallery Β· ggrepel for labels

Inspiration: Gapminder Β· Our World in Data

Let’s Get Started!

Remember

The goal is not to master Tableau or R today.

The goal is to think carefully about how to visualize change over time and relationships between variables β€” and to develop your own sense of when each tool serves you better.

Experiment. Break things. Fix them. Learn.

Any questions before we begin?