Lab 2

Wrangling and Visualizing Democracy Data

Author

YOUR NAME HERE

How to complete this lab

Fill in each ??? with the correct code. Once all placeholders are filled in, change completed: false to completed: true in the YAML header above and render to HTML. For your final submission, change format: html to format: pdf.

Overview

In this lab, you will practice wrangling data and creating visualizations using the V-Dem democracy dataset. You will:

  1. Load and explore democracy data
  2. Create a line chart showing trends in democracy over time
  3. Create a scatter plot examining the relationship between wealth and democracy
  4. Create a column chart of women’s political representation by region
  5. Write brief interpretations of your visualizations
  6. Render your document to PDF and submit

You are encouraged to have the lecture materials from Module 2.1 open while completing this lab. You should also have the V-Dem codebook available to help you choose variables.

Getting Started

Load the required packages.

library(vdemdata) # load the V-Dem package
library(tidyverse) # load the tidyverse
run <- isTRUE(params$completed)
some_of_vdem <- vdem |>
  filter(year >= 2010) |>
  select(country_name, year, v2x_polyarchy, v2x_libdem)
Installing vdemdata locally

If you are working on your own computer and don’t have vdemdata installed, you’ll need to install it from GitHub. First install the pak package, then use it to install vdemdata:

install.packages("pak")
pak::pkg_install("vdeminstitute/vdemdata")

The Data

Today we’ll work with two data sources:

  • The vdem dataset from the vdemdata package, which contains hundreds of democracy indicators for countries over time
  • dem_women.csv, which contains a subset of V-Dem data along with economic and women’s representation variables

Part 2: Scatter Plot of Wealth and Democracy (35 points)

For this part, you will explore the relationship between economic development (GDP per capita) and democracy (polyarchy) using the dem_women dataset.

Step 1: Load and Wrangle the Data (15 pts)

Load the data and calculate average values for each country across all years. Fill in the blanks:

dem_women <- read_csv("dem_women.csv")

gdp_polyarchy_ctry <- dem_women |>
  group_by(???, ???) |>              # group by country, keep region
  summarize(
    polyarchy = ???(polyarchy, na.rm = TRUE), # summarize by mean (or median)
    gdp_pc = ???(gdp_pc, na.rm = TRUE)        # summarize by mean (or median)
  )

Step 2: Create a Scatter Plot (15 pts)

Using the scatter plot code from Module 1.2 as a template, create a scatter plot with GDP per capita on the x-axis and polyarchy on the y-axis. Consider coloring the points by region to see if there are regional patterns.

# Write your scatter plot code here

Step 3: Interpret Your Scatter Plot (5 pts)

Write 2-3 sentences describing what you see. Is there a relationship between wealth and democracy? Do you notice any regional patterns?

YOUR INTERPRETATION HERE

Part 3: Column Chart of Women’s Representation (35 points)

For this part, you will create a column chart showing average women’s representation in parliament by region.

Step 1: Wrangle the Data (15 pts)

Summarize the data to get average women’s representation by region. Fill in the blanks:

women_rep_region <- dem_women |>
  ???(???) |>                                      # group by region
  ???(
    women_rep = ???(women_rep, na.rm = TRUE)       # summarize by mean (or median)
  )

Step 2: Create a Column Chart (15 pts)

Using the column chart code from Module 1.1 as a template, create a column chart showing women’s representation by region. For a cleaner visualization, try to arrange the columns in descending order by level of women’s representation.

Hint: To reorder the bars, you can use reorder(region, -women_rep) or fct_reorder(region, women_rep, .desc = TRUE) in your aes() for the x variable.

# Write your column chart code here

Step 3: Interpret Your Chart (5 pts)

Write 2-3 sentences describing what you see. Which region has the highest level of women’s representation in parliament? Which has the lowest? Are you surprised by any of the results?

YOUR INTERPRETATION HERE

Submission (Completion)

  1. Replace “YOUR NAME HERE” at the top with your actual name
  2. Make sure all your code chunks run without errors
  3. Click “Render” to create your PDF
  4. Submit the PDF to Blackboard

Hints

Only look at these if you’re stuck!

Hint 1 - Line chart structure:

ggplot(data_name, aes(x = year, y = ___, color = ___)) +
  geom_line() +
  labs(title = "___", x = "Year", y = "___", color = "___")

Hint 2 - Scatter plot structure:

ggplot(data_name, aes(x = ___, y = ___)) +
  geom_point() +
  labs(title = "___", x = "___", y = "___")

Hint 3 - Column chart with reordering:

ggplot(data_name, aes(x = reorder(region, -women_rep), y = women_rep)) +
  geom_col() +
  labs(title = "___", x = "___", y = "___")

Hint 4 - Common issues:

  • Make sure variable names match exactly what you see in the data (R is case-sensitive!)
  • If you get an error about missing values, check that you included na.rm = TRUE in your summary functions
  • If your line chart only shows one line, make sure you included color = country in your aes()