max_year <- max(gsdb$end, na.rm = TRUE)
gsdb_expanded <- gsdb %>%
# Create a sequence of years from 'begin' to 'end' for each row
mutate(year = map2(begin, end, ~seq(.x, .y))) %>%
# Expand the list of years into individual rows
unnest(cols = c(year))
# Aggregate the active sanctions per year
gsdb_type_counts <- gsdb_expanded %>%
group_by(year) %>%
summarise(across(
c(trade, arms, military, financial, travel, other),
~sum(.x, na.rm = TRUE)
)) %>%
# Pivot from wide to long format for ggplot2
pivot_longer(
cols = -year,
names_to = "sanction_type",
values_to = "count"
) %>%
mutate(sanction_type = fct_reorder(sanction_type, count, .fun = sum, .desc = TRUE))