Day 3 of 30DayMapChallenge: « Polygons » (previously).
A classic world choropleth map. We’ll use the Comparative Death Penalty Database (Anckar and Denk 2024) available on https://dataverse.harvard.edu/dataset.xhtml?persistentId=doi:10.7910/DVN/LI3WYK and the basemap from {rnaturalearth}.
library(dplyr) library(tibble) library(readr) library(glue) library(readxl) library(janitor) library(ggplot2) library(scales) library(ggspatial) library(sf) library(rnaturalearth) library(countrycode)
The CDPD doesn’t use ISO A3 country codes, so we need to translate them from the Correlates of War codes with {countrycode}.
codes <- tribble( ~deathpenalty, ~lib_deathpenalty, ~color, 0, "Abolished", "lightskyblue", 1, "Abolished for ordinary crimes only", "khaki1", 2, "Abolished for ordinary crimes only, but used during the last 10 years.", "gold1", 3, "Abolished in practice", "indianred1", 4, "Retained", "indianred4") if (!file.exists("cdpd.rds")) { download.file("https://dataverse.harvard.edu/api/access/datafile/10251764", "cdpd.xlsx") cdpd <- read_xlsx("cdpd.xlsx") |> clean_names() |> filter(year == 2022, country != "MICRONESIA") |> mutate(iso = countrycode(cowcode, "cown", "iso3c", custom_match = c("342" = "SRB", "348" = "MNE", "818" = "VNM"))) |> left_join(codes, join_by(deathpenalty)) |> select(country, iso, lib_deathpenalty) |> write_rds("cdpd.rds") } else { cdpd <- read_rds("cdpd.rds") } abolished <- nrow(filter(cdpd, lib_deathpenalty == "Abolished")) / nrow(cdpd)
country_map <- ne_countries(scale = 110, returnclass = "sf") |> select(sovereignt, sov_a3, admin, adm0_a3)
pal_dp <- codes |> select(lib_deathpenalty, color) |> deframe() cdpd_map <- country_map |> left_join(cdpd, join_by(adm0_a3 == iso)) cdpd_map |> ggplot() + geom_sf(aes(fill = lib_deathpenalty)) + scale_fill_manual(values = pal_dp, na.value = "grey") + coord_sf(crs = "+proj=eqearth") + labs(title = glue("Death penalty is still retained in {percent(1 - abolished)} of the countries"), subtitle = "2022", fill = "Death penalty\nstatus", caption = glue("data: Comparative Death Penalty Database map data: NaturalErth r.iresmi.net - {Sys.Date()}")) + theme_void() + theme(plot.background = element_rect(color = NA, fill = "white"), legend.position = "bottom", legend.text = element_text(size = 8), plot.caption = element_text(size = 5, color = "darkgrey"))