Last active
April 11, 2024 22:34
-
-
Save danweitzel/591fd6f0de7325d8f939aabd5d956eab to your computer and use it in GitHub Desktop.
Plotting change and level of electoral democracy in R
This file contains 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
# Libraries | |
# If vdemdata is not installed run below: | |
#install.packages("devtools") | |
#devtools::install_github("vdeminstitute/vdemdata", force = TRUE) | |
library(tidyverse) | |
library(vdemdata) | |
# Overhead | |
set.seed(1904) | |
theme_set(theme_bw()) | |
## Lineplot | |
vdem |> | |
filter(e_regiongeo %in% c(1,2,3,4)) |> # set the region based on V-Dem codebook | |
filter(year > 2010) |> # set the years | |
drop_na(v2x_polyarchy) |> | |
ggplot(aes(x = year, y = v2x_polyarchy)) + | |
geom_line() + | |
facet_wrap(~country_name, ncol = 8) + | |
labs(title= "Electoral Democracy in Europe between 2010-2023", | |
x = "Year", | |
y = "V-Dem Polyarchy") | |
# Diverging bar chart | |
df_vdem <- | |
vdem |> | |
dplyr::select(country_name, country_text_id, | |
year, v2x_polyarchy,e_regiongeo) |> | |
filter(year %in% c(2010, 2023)) |> # set the years | |
filter(e_regiongeo %in% c(1,2,3,4)) |> # set the region based on V-Dem codebook | |
group_by(country_name) |> | |
mutate(delta_democracy = v2x_polyarchy - lag(v2x_polyarchy), | |
democracy_type = ifelse(delta_democracy < 0, "below", | |
ifelse(delta_democracy == 0, "unchanged", "above"))) |> | |
drop_na(delta_democracy) | |
df_vdem <- df_vdem[order(df_vdem$delta_democracy), ] # sort | |
df_vdem$country_name <- factor(df_vdem$country_name, levels = df_vdem$country_name) # convert to factor to retain sorted order in plot. | |
# Diverging Barcharts | |
df_vdem |> | |
ungroup() |> | |
ggplot(aes(x=country_name, y=delta_democracy, label=delta_democracy)) + | |
geom_bar(stat='identity', aes(fill=democracy_type), width=.5) + | |
scale_fill_manual(name="Democracy", | |
labels = c("Improved", "Decreased"), | |
values = c("above"="#00ba38", "below"="#f8766d")) + | |
labs(subtitle="Changes in Electoral Democracy between 2010-2023", | |
title= "Democratic Declines", | |
x = "Democratization", | |
y = "Country Name") + | |
coord_flip() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment