Created
February 12, 2025 16:21
-
-
Save andrewheiss/f7c6a9cc30d2a3e17f9acf77f71d6811 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidyverse) | |
library(readxl) | |
library(tinytable) | |
# Load data downloaded from https://www.systemicpeace.org/inscrdata.html | |
polity <- read_excel("~/Downloads/p5v2018.xls") |> | |
mutate(polity_change = polity2 - lag(polity2), .by = scode) |> | |
filter(year > 1800) | |
# Find the biggest drops in polity scores since 1985 | |
biggest_drops <- polity |> | |
filter(year > 1985) |> | |
filter(polity_change < -7) | |
polity_to_table <- biggest_drops |> | |
add_row(country = "United States", year = 2025, polity_change = -8) |> | |
arrange(polity_change) |> | |
mutate(year_range = glue::glue("{year - 1} → {year}")) |> | |
select(country, year_range, polity_change) | |
cbind( | |
as.data.frame(slice(polity_to_table, 1:11)), | |
as.data.frame(polity_to_table |> slice(12:21) |> add_row(country = "")) | |
) |> | |
setNames(c("Country", "Year", "Polity change", "Country", "Year", "Polity change")) |> | |
tt(caption = "Largest drops in Polity scores since 1985") |> | |
format_tt(replace = "") |> | |
style_tt(align = "llcllc") |> | |
style_tt(i = 10, j = 4:6, background = "yellow") |> | |
style_tt(j = 4, line = "l", line_width = 0.1, line_color = "#eeeeee") | |
# Plot the US over time | |
polity_breaks <- tribble( | |
~start, ~end, ~democracy, | |
10, 5.5, "Democracy", | |
5.5, -5.5, "Anocracy", | |
-5.5, -10, "Autocracy" | |
) %>% | |
mutate(democracy = fct_inorder(democracy)) | |
polity_to_plot <- polity |> | |
add_row(country = "United States", year = c(2021, 2024, 2025), polity2 = c(8, 8, 0)) | |
polity_to_plot |> | |
filter(country == "United States") |> | |
ggplot(aes(x = year, y = polity2, color = country)) + | |
geom_rect( | |
data = polity_breaks, | |
aes(xmin = -Inf, xmax = +Inf, ymin = end, ymax = start, fill = democracy), | |
alpha = 0.2, inherit.aes = FALSE | |
) + | |
geom_line(linewidth = 2) + | |
scale_color_manual(values = "#CC503E") + | |
scale_fill_manual(values = c("#009392", "#e9e29c", "#cf597e")) + | |
labs( | |
x = NULL, | |
y = "Polity score", | |
title = "Democracy in the USA (1800–2025)", | |
caption = "Source: Systemic Peace Polity Project", | |
color = NULL, fill = NULL | |
) + | |
theme_light(base_family = "Open Sans Light") + | |
theme( | |
legend.position = "bottom", | |
plot.title = element_text(family = "Open Sans Condensed Bold", size = rel(1.6)) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment