Lab 1: Plotting

Do Wealth and Democracy Go Together?

How labs work

You are encouraged to work together and get started in class—talk through the problems with the people around you. You will finish the lab on your own time and submit your own work. You may use AI to ask questions or debug, but not to write your code for you; the point is to think it through yourself. At the end, add a one-line note saying whether and how you used AI.

Overview

In 1959, Seymour Martin Lipset published a famous article arguing that economic modernization—wealth, urbanization, education, health—makes democracy more likely. It has been cited more than 11,000 times. In this lab your group will put his argument to the test with three charts.

The data are already prepared for you, so you can focus entirely on the visualization. Run the chunk below to load them.

library(tidyverse)
library(scales)

dem_data <- read_csv("data/dem_data.csv")

glimpse(dem_data)
Rows: 6,025
Columns: 15
$ country       <chr> "Afghanistan", "Afghanistan", "Afghanistan", "Afghanista…
$ iso3c         <chr> "AFG", "AFG", "AFG", "AFG", "AFG", "AFG", "AFG", "AFG", …
$ year          <dbl> 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 19…
$ region        <chr> "Asia", "Asia", "Asia", "Asia", "Asia", "Asia", "Asia", …
$ polyarchy     <dbl> 0.094, 0.094, 0.095, 0.093, 0.093, 0.093, 0.076, 0.073, …
$ libdem        <dbl> 0.045, 0.041, 0.033, 0.023, 0.023, 0.022, 0.019, 0.022, …
$ partipdem     <dbl> 0.025, 0.025, 0.028, 0.027, 0.027, 0.024, 0.013, 0.013, …
$ delibdem      <dbl> 0.035, 0.035, 0.031, 0.027, 0.027, 0.027, 0.009, 0.007, …
$ egaldem       <dbl> 0.099, 0.096, 0.066, 0.044, 0.044, 0.042, 0.036, 0.038, …
$ gdp_pc        <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 308.3, 277.1, 33…
$ life_exp      <dbl> 45.1, 45.5, 46.6, 51.0, 51.0, 52.1, 52.8, 53.2, 52.5, 54…
$ inf_mort      <dbl> 145.9, 141.4, 137.2, 133.1, 129.4, 126.0, 122.7, 119.6, …
$ urban_pct     <dbl> 17.3, 17.4, 17.5, 17.6, 17.7, 17.8, 18.0, 18.1, 18.2, 18…
$ school_enroll <dbl> 11.0, 17.2, NA, 17.4, 23.5, 23.1, NA, NA, NA, NA, NA, 14…
$ internet_pct  <dbl> 0.0, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 0.0, 0.0, 0…

Every row is one country in one year, from 1990 to 2023. Along with country, year and region, you have:

Measures of democracy Measures of modernization
polyarchy — electoral democracy gdp_pc — GDP per capita (US$)
libdem — liberal democracy life_exp — life expectancy (years)
partipdem — participatory democracy inf_mort — infant mortality (per 1,000)
delibdem — deliberative democracy urban_pct — population that is urban (%)
egaldem — egalitarian democracy internet_pct — population online (%)

All five democracy measures run from 0 to 1, where higher is more democratic.

Pick your variables

As a group, choose one democracy measure and one modernization measure to use for the whole lab. Different groups will choose differently, and that is the point—we will compare notes at the end.

Step 1: A line chart over time (30 pts)

a) Pick three or four countries you are curious about, at different levels of wealth, and filter() the data down to just those countries.

Country names have to match the data exactly. Run this to check a name before you use it—change "Kor" to the first few letters of whatever you are looking for:

dem_data |>
  distinct(country) |>
  filter(str_detect(country, "Kor"))
# A tibble: 2 × 1
  country    
  <chr>      
1 North Korea
2 South Korea
Warning

A few names are not what you would guess: United States of America, Türkiye, Czechia, Burma/Myanmar, and two separate Congos. Copy them from the check above rather than typing from memory.

Starter code

Copy this into the chunk below and add your countries inside the c().

line_data <- dem_data |>
  filter(country %in% c("", ""))

count(line_data, country)   # check you got what you expected

b) Make a line chart. You need year on the x-axis, your democracy measure on the y-axis, and country mapped to color so each country gets its own line.

Starter code

The aes() call is where you say which column maps to which part of the chart. Fill in all three, then add your labels inside labs().

ggplot(line_data, aes(x = , y = , color = )) +
  geom_line() +
  labs(x = , y = , title = , caption = )

c) In a sentence or two below this line, describe what you see. Which country changed the most?

Step 2: A bar chart comparing regions (30 pts)

a) Starting from dem_data, filter for a single recent year (2019 is a good choice), then group by region and take the mean of your democracy measure.

Starter code

Three verbs in a row. Inside summarize() you give the new column a name, like mean_polyarchy = mean(polyarchy).

bar_data <- dem_data |>
  filter(year == ) |>
  group_by() |>
  summarize()

bar_data

b) Make a bar chart with geom_col(), putting region on the x-axis and your summarized democracy measure on the y-axis. Add labels and a title, and finish with a theme such as theme_minimal().

c) Below this line, say which region scores highest and which scores lowest.

Step 3: A scatter plot (30 pts)

a) Filter dem_data for the same year you used in Step 2 and save it as scatter_data.

b) Build a scatter plot: your modernization measure on the x-axis, your democracy measure on the y-axis, points colored by region. Then add a trend line with geom_smooth(method = "lm"), a colorblind-friendly palette with scale_color_viridis_d(), and clear labels.

Build it up one layer at a time—run the chart after each + you add so you can see what each piece does.

Where you put color matters here

Anything you put in the top-level aes() is inherited by every layer that follows it. So if you map color = region there, geom_smooth() will split by region too, and you will get six separate trend lines instead of one overall trend.

You want the points colored by region but a single trend line through all of them. Think about which layer actually needs the color, and put it there. If you end up with six trend lines, that is the clue.

Tip

If you chose gdp_pc, most countries will bunch up against the left edge. Add this layer to spread them out:

scale_x_log10(labels = label_dollar())

c) Below this line, describe the relationship. Does it look positive, negative, or flat? If you used inf_mort, think about why its direction differs from the others.

Step 4: Conclusion (10 pts)

Render your document, then write a short paragraph below this line. Taken together, do your three charts support Lipset’s argument? Name one country or region that does not fit the pattern.

Bonus (1 pt each)

a) Add facet_wrap(~ region) to your scatter plot.

b) Mark something meaningful on your line chart with geom_vline() and annotate()—for example, the year a country held a key election.

c) Make your scatter plot interactive with ggplotly() from the plotly package.

AI note: (one line on whether/how you used AI)

Submission Instructions

Head over to Blackboard and go to the Lab 1 assignment. Click “Create Submission” and write a brief statement saying that you have submitted the lab and that all of the work is your own.

Then upload a compressed (zipped) version of your project folder including the rendered HTML file. To compress your project folder, right-click it and choose Compress (Mac) or Send to → Compressed (zipped) folder (Windows).