Skip to content

Instantly share code, notes, and snippets.

@resulumit
Last active March 28, 2022 06:13
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 resulumit/f42baf69ee2ce2a5b38bccf4eff221bb to your computer and use it in GitHub Desktop.
Save resulumit/f42baf69ee2ce2a5b38bccf4eff221bb to your computer and use it in GitHub Desktop.
# packages ----------------------------------------------------------------
library(dataverse)
library(tidyverse)
library(countrycode)
library(maps)
library(ggtext)
library(gganimate)
# note that the following packages must also be installed, but they don't
# need to be loaded: gifski, transformr
# data --------------------------------------------------------------------
# get boix-miller-rosato
bmr <- get_dataframe_by_name(filename = "democracy-v4.0-1.tab",
dataset = "https://doi.org/10.7910/DVN/FENWWR",
server = "dataverse.harvard.edu") |>
filter(year >= 1992) |>
# manually edit some country codes
mutate(
cntry_code = case_when(
country == "CONGO, DEM. REP." ~ "COD",
country == "ETHIOPIA" ~ "ETH",
country == "KOSOVO" ~ "KSV",
country == "MONTENEGRO" ~ "MNE",
country == "SERBIA" ~ "SRB",
country == "SWEDEN" ~ "SWE",
country == "ROMANIA" ~ "ROU",
# address two sudans
# both sides have the same score
country == "SUDAN" ~ "SDN",
country == "SUDAN, NORTH" ~ "SDN",
country == "SUDAN, SOUTH" ~ "SSD",
TRUE ~ abbreviation
)
)
# south sudan for the missing years
ssd <- bmr |>
filter(abbreviation == "SDN") |>
mutate(cntry_code = "SSD")
# join south sudan in
democracies <- rbind(bmr, ssd)
# get world map
world_map <- map_data("world") |>
select(-subregion) |> filter(region != "Antarctica") |>
mutate(
cntry_code = countrycode(region, origin = "country.name", destination = "iso3c"),
cntry_code = replace(cntry_code, region == "Greenland", "DNK"),
cntry_code = replace(cntry_code, region == "Kosovo", "KSV")
)
# merge boix-miller-rosato with map
df <- left_join(world_map, democracies, by = "cntry_code")
# plots -------------------------------------------------------------------
# static, for the latest year available (2020)
df |> filter(year == 2020) |>
ggplot(aes(
x = long,
y = lat,
group = group,
fill = factor(democracy)
)) +
geom_polygon(colour = "grey60", size = 0.1) +
theme_void() +
theme(
panel.grid = element_blank(),
axis.ticks = element_blank(),
panel.border = element_blank(),
axis.text = element_blank(),
plot.title = element_markdown(),
legend.position = "none"
) +
scale_fill_manual(
name = "",
breaks = c(0, 1),
values = c("#cc3232", "#2dc937")
) +
labs(title = "<span>&emsp; </span> A <span style='color:#2dc937;'>democracy</span> in 2020")
ggsave(filename = "democracies.png", dpi = 1000,
width = 7.5, height = 4, units = "in")
# animated
world_gif <- ggplot(df) +
aes(
x = long,
y = lat,
group = group,
fill = factor(democracy)
) +
geom_polygon(colour = "grey60", size = 0.1) +
theme_void() +
theme(
panel.grid = element_blank(),
axis.ticks = element_blank(),
panel.border = element_blank(),
axis.text = element_blank(),
plot.title = element_markdown(),
legend.position = "none"
) +
scale_fill_manual(
name = "",
breaks = c(0, 1),
values = c("#cc3232", "#2dc937")
) +
labs(title = "<span>&emsp; </span> A <span style='color:#2dc937;'>democracy</span> in {closest_state}") +
transition_states(year)
# animate
animate(
world_gif,
renderer = gifski_renderer(loop = FALSE),
width = 600,
height = 335
)
# save
anim_save("democracies.gif")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment