Skip to content

Instantly share code, notes, and snippets.

@chrishanretty
Created November 20, 2022 13:10
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/7faabc9bdc3ed3c611efe63137ac9b30 to your computer and use it in GitHub Desktop.
Save chrishanretty/7faabc9bdc3ed3c611efe63137ac9b30 to your computer and use it in GitHub Desktop.
Plot average levels of democracy per 2022 World Cup group
library(vdemdata)
library(tidyverse)
library(ggflags)
library(countrycode)
library(hrbrthemes)
### Get the V-Dem data in
data("vdem")
dat <- vdem |>
distinct(country_name, country_text_id, year,
v2x_polyarchy, v2x_polyarchy_sd) |>
filter(!is.na(v2x_polyarchy)) |>
group_by(country_name, country_text_id) |>
arrange(desc(year)) |>
slice(1) |>
ungroup()
plot_func <- function(countries, dat, maintext) {
df <- dat
df <- df |>
filter(country_text_id %in% countries)
### Fix for England and Wales
if (any(countries %in% c("ENG", "WAL"))) {
df <- bind_rows(df,
dat |> filter(country_text_id == "GBR") |>
mutate(country_name = "England"))
df <- bind_rows(df,
dat |> filter(country_text_id == "GBR") |>
mutate(country_name = "Wales"))
}
df <- df |>
arrange(v2x_polyarchy) |>
mutate(country_name = fct_inorder(country_name),
country_flag = countrycode(country_text_id,
"iso3c",
"iso2c"),
country_flag = tolower(country_flag))
ggplot(df, aes(x = country_name,
y = v2x_polyarchy,
ymin = v2x_polyarchy - qnorm(11/12) * v2x_polyarchy_sd,
ymax = v2x_polyarchy + qnorm(11/12) * v2x_polyarchy_sd)) +
scale_x_discrete("") +
scale_y_continuous("Liberal democracy index [0-1]",
limits = c(0, 1)) +
geom_pointrange(shape = 21, col = "#df4ca3", fill = "#d6f079") +
geom_hline(yintercept = global_avg, col = "#df4ca377", linetype = 2) +
labs(title = maintext,
subtitle = "Plotted points indicate level of liberal democracy; lines show 83% confidence intervals. Dotted line shows average amongst all competing teams. ",
caption = "Data from the V-Dem project: v-dem.net. Graphic: @chrishanretty") +
coord_flip() +
theme_ft_rc(base_size = 14) +
theme(plot.title.position = "plot",
plot.caption.position = "plot")
}
grp_a <- c("QAT", "ECU", "SEN", "NLD")
grp_b <- c("ENG", "IRN", "USA", "WAL")
grp_c <- c("ARG", "SAU", "MEX", "POL")
grp_d <- c("FRA", "AUS", "DNK", "TUN")
grp_e <- c("ESP", "CRI", "DEU", "JPN")
grp_f <- c("BEL", "CAN", "MAR", "HRV")
grp_g <- c("BRA", "SRB", "CHE", "CMR")
grp_h <- c("PRT", "GHA", "URY", "KOR")
global_avg <- dat |>
filter(country_text_id %in% c(grp_a, grp_b, grp_c, grp_d,
grp_e, grp_f, grp_g, grp_h,
"GBR")) |>
pull(v2x_polyarchy) |>
mean()
grp_a_plot <- plot_func(grp_a, dat, "Group A")
grp_b_plot <- plot_func(grp_b, dat, "Group B")
grp_c_plot <- plot_func(grp_c, dat, "Group C")
grp_d_plot <- plot_func(grp_d, dat, "Group D")
grp_e_plot <- plot_func(grp_e, dat, "Group E")
grp_f_plot <- plot_func(grp_f, dat, "Group F")
grp_g_plot <- plot_func(grp_g, dat, "Group G")
grp_h_plot <- plot_func(grp_h, dat, "Group H")
ggsave(filename = "grp_a_plot.png", grp_a_plot, width = 1200 / 96, height = 675 / 96)
ggsave(filename = "grp_b_plot.png", grp_b_plot, width = 1200 / 96, height = 675 / 96)
ggsave(filename = "grp_c_plot.png", grp_c_plot, width = 1200 / 96, height = 675 / 96)
ggsave(filename = "grp_d_plot.png", grp_d_plot, width = 1200 / 96, height = 675 / 96)
ggsave(filename = "grp_e_plot.png", grp_e_plot, width = 1200 / 96, height = 675 / 96)
ggsave(filename = "grp_f_plot.png", grp_f_plot, width = 1200 / 96, height = 675 / 96)
ggsave(filename = "grp_g_plot.png", grp_g_plot, width = 1200 / 96, height = 675 / 96)
ggsave(filename = "grp_h_plot.png", grp_h_plot, width = 1200 / 96, height = 675 / 96)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment