Skip to content

Instantly share code, notes, and snippets.

@chrishanretty
Created April 6, 2023 08:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrishanretty/77b096928fa38c0a083344ca2e54ab34 to your computer and use it in GitHub Desktop.
Save chrishanretty/77b096928fa38c0a083344ca2e54ab34 to your computer and use it in GitHub Desktop.
Plot proportion of female parliamentarians over time
library(tidyverse)
### Write a function to download the data from the IPU
dl_data <- function(y, m) {
url <- paste0("https://data.ipu.org/api/women-ranking.csv?load-entity-refs=taxonomy_term%2Cfield_collection_item&max-depth=2&langcode=en&month=",
m,
"&year=",
y)
download.file(url, destfile = paste0("ipu_", y, m, ".csv"))
}
### Generate all year/month combinations
holder <- expand.grid(y = 2019:2023, m = 1:12)
holder <- holder |>
filter(!(y == 2023 & m > 3))
### Download the data
apply(holder, 1, function(x) dl_data(x["y"], x["m"]))
### Read it in
filelist <- list.files(pattern = "ipu_")
holder <- list()
for (f in filelist) {
holder[[f]] <- read.csv(f, skip = 5,
col.names = c("rank", "country",
"elex", "seats",
"women_count", "women_pct",
"upper_elex", "upper_seats",
"upper_women", "upper_women_pct"),
na.strings = c("-", "NA")) |>
mutate(filename = f)
}
### Tidy up a little
holder <- bind_rows(holder)
holder$year <- sub("ipu_", "", holder$filename)
holder$year <- substr(holder$year, 0, 4)
holder$month <- sub("ipu_[0-9]{4}", "", holder$filename)
holder$month <- gsub(".csv", "", holder$month)
holder$date <- paste0(holder$year, "-", holder$month, "-01")
holder$date <- as.Date(holder$date, format = "%Y-%m-%d")
### Calculate the month/year average
plot_df <- holder |>
group_by(date) |>
summarize(mean_women_pct = mean(women_pct, na.rm = TRUE))
ggplot(plot_df, aes(x = date, y = mean_women_pct)) +
geom_point() +
scale_x_date("Date") +
scale_y_continuous("Average percentage of women in parliament") +
geom_smooth(method = "lm", se = FALSE) +
theme_bw()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment