World maps with ISO Alpha 2 & 3 codes for each country

A Simple world map, overlaid with ISO-3166 alpha-2 and alpha-3 codes for each country, sized by the geographical area of each country

#TidyTuesday
Governance
Geocomputation
Author

Aditya Dahiya

Published

November 15, 2024

The dataset focuses on the ISO 3166 standard, which assigns codes to countries, their subdivisions, and former territories. The ISO 3166 standard, developed by the International Organization for Standardization (ISO), defines codes that help standardize country names, administrative divisions, and historical country names across databases and applications. This dataset, derived from the {ISOcodes} R package, comprises three tables:

  1. countries (ISO 3166-1),

  2. country_subdivisions (ISO 3166-2), and

  3. former_countries (ISO 3166-3).

These tables facilitate the exploration of country and subdivision identifiers, such as 2- and 3-letter country codes, subdivision types (e.g., provinces or states), and details on formerly recognized countries and their withdrawal dates. By using functions like the quick_map() from the {countries} package, one can visualize country codes and subdivisions on maps.

The dataset offers a valuable resource for understanding global administrative regions, analyzing patterns in subdivisions, and historical changes in geopolitical entities.

Figure 1: Simple world maps displaying ISO-3166 Alpha-2 and Alpha-3 codes for each country, sized by the geographical area of each country.

How I made this graphic?

Loading required libraries, data import & creating custom functions.

Code
# Data Import and Wrangling Tools
library(tidyverse)            # All things tidy

# Final plot tools
library(scales)               # Nice Scales for ggplot2
library(fontawesome)          # Icons display in ggplot2
library(ggtext)               # Markdown text support for ggplot2
library(showtext)             # Display fonts in ggplot2
library(colorspace)           # Lighten and Darken colours
library(patchwork)            # Compiling Plots

# Geocomputation
library(sf)                   # Simple Features in R

# Load the data

countries <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2024/2024-11-12/countries.csv')
# country_subdivisions <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2024/2024-11-12/country_subdivisions.csv')
# former_countries <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2024/2024-11-12/former_countries.csv')

Visualization Parameters

Code
# Font for titles
font_add_google("Bebas Neue",
  family = "title_font"
) 

# Font for the caption
font_add_google("Saira Extra Condensed",
  family = "caption_font"
) 

# Font for plot text
font_add_google("Bowlby One",
  family = "body_font"
) 

font_add_google(
  "Bree Serif",
  "slab_font"
)

showtext_auto()

# A base Colour
base_col <- "black"
bg_col <- lighten(base_col, 0.2)
seecolor::print_color(bg_col)

# Colour for the text
text_col <- lighten(bg_col, 0.8)
seecolor::print_color(text_col)

# Colour for highlighted text
text_hil <- lighten(bg_col, 0.9)
seecolor::print_color(text_hil)

# Define Base Text Size
bts <- 90 

# Caption stuff for the plot
sysfonts::font_add(
  family = "Font Awesome 6 Brands",
  regular = here::here("docs", "Font Awesome 6 Brands-Regular-400.otf")
)
github <- "&#xf09b"
github_username <- "aditya-dahiya"
xtwitter <- "&#xe61b"
xtwitter_username <- "@adityadahiyaias"
social_caption_1 <- glue::glue("<span style='font-family:\"Font Awesome 6 Brands\";'>{github};</span> <span style='color: {text_hil}'>{github_username}  </span>")
social_caption_2 <- glue::glue("<span style='font-family:\"Font Awesome 6 Brands\";'>{xtwitter};</span> <span style='color: {text_hil}'>{xtwitter_username}</span>")

# Add text to plot--------------------------------------------------------------
plot_title <- "ISO-3166 Country Codes"

plot_caption <- paste0(
  "**Data:** {ISOcodes} by Christian Buchta & Kurt Hornik", 
  " |  **Code:** ", 
  social_caption_1, 
  " |  **Graphics:** ", 
  social_caption_2
  )

rm(github, github_username, xtwitter, 
   xtwitter_username, social_caption_1, 
   social_caption_2)

Exploratory Data Analysis and Wrangling

Code
sf_use_s2(FALSE)

df <- rnaturalearth::ne_countries(
  returnclass = "sf",
  scale = "small"
  ) |>
  select(admin, iso_a2, iso_a3, geometry) |> 
  left_join(
    countries |> 
      rename(
        iso_a2 = alpha_2,
        iso_a3 = alpha_3
      ) |> 
      select(iso_a2, iso_a3)
  ) |> 
  mutate(area = as.numeric(st_area(geometry))) |> 
  mutate(
    iso_a2 = case_when(
      admin == "France" ~ "FR",
      admin == "Norway" ~ "NO",
      .default = iso_a2
    )
  ) |> 
  mutate(
    iso_a3 = case_when(
      admin == "France" ~ "FRA",
      admin == "Norway" ~ "NOR",
      .default = iso_a3
    )
  )

# Another easy method to fetch area 
# (though not in this week's TidyTuesday)
# spData::world |> 
#   select(iso_a2, area_km2, geom)

The Base Plot

Code
g1 <- df |> 
  ggplot() +
  geom_sf(
    colour = alpha(text_hil, 0.05),
    fill = bg_col,
    linewidth = 1
  ) +
  geom_sf_text(
    mapping = aes(
      size = area,
      label = iso_a2,
      colour = iso_a2
    ),
    family = "slab_font"
  ) +
  scale_size_continuous(
    range = c(10, 50)
  ) +
  coord_sf(
    crs = "ESRI:54030"
  ) +
  labs(
    subtitle = "Alpha-2 Codes"
  ) +
  theme_void(
    base_family = "body_font",
    base_size = bts
  ) +
  theme(
    legend.position = "none",
    plot.margin = margin(0,0,0,0, "mm"),
    panel.border = element_blank(),
    plot.subtitle = element_text(
        margin = margin(0,0,-10,0, "mm"),
        hjust = 0.1,
        colour = text_col,
        family = "title_font",
        size = 1.5 * bts
      )
  )

g2 <- df |> 
  ggplot() +
  geom_sf(
    colour = alpha(text_hil, 0.05),
    fill = bg_col,
    linewidth = 1
  ) +
  geom_sf_text(
    mapping = aes(
      size = area,
      label = iso_a3,
      colour = iso_a3
    ),
    family = "slab_font"
  ) +
  scale_size_continuous(
    range = c(7, 40)
  ) +
  coord_sf(
    crs = "ESRI:54030"
  ) +
  labs(
    subtitle = "Alpha-3 Codes"
  ) +
  theme_void(
    base_family = "body_font",
    base_size = bts
  ) +
  theme(
    legend.position = "none",
    plot.margin = margin(0,0,-5,0, "mm"),
    panel.border = element_blank(),
    plot.subtitle = element_text(
        margin = margin(0,0,-10,0, "mm"),
        hjust = 0.1,
        colour = text_col,
        family = "title_font",
        size = 1.5 * bts
      )
  )

library(patchwork)

g <- patchwork::wrap_plots(
  g1, g2,
  ncol = 1, nrow = 2
  ) +
  plot_annotation(
    title = plot_title,
    caption = plot_caption,
    theme = theme(
      plot.margin = margin(0,-15,0,-40, "mm"),
      plot.title = element_text(
        colour = text_col,
        size = bts * 3.5,
        hjust = 0.6,
        margin = margin(10,0,10,0, "mm"),
        family = "title_font",
        face = "bold"
      ),
      plot.caption = element_textbox(
        family = "caption_font",
        hjust = 0.9,
        margin = margin(0,0,10,0, "mm"),
        colour = text_hil,
        size = bts * 0.5
      ),
      plot.background = element_rect(
        fill = bg_col,
        colour = bg_col
      ),
      panel.background = element_rect(
        fill = bg_col,
        colour = bg_col
      )
    )
  )

ggsave(
  filename = here::here(
    "data_vizs",
    "tidy_iso_country_codes.png"
  ),
  plot = g,
  width = 400,
  height = 500,
  units = "mm",
  bg = bg_col
)

Savings the thumbnail for the webpage

Code
# Saving a thumbnail

library(magick)
# Saving a thumbnail for the webpage
image_read(here::here("data_vizs", 
                      "tidy_iso_country_codes.png")) |> 
  image_resize(geometry = "400") |> 
  image_write(
    here::here(
      "data_vizs", 
      "thumbnails", 
      "tidy_iso_country_codes.png"
    )
  )

Session Info

Code
# Data Import and Wrangling Tools
library(tidyverse)            # All things tidy

# Final plot tools
library(scales)               # Nice Scales for ggplot2
library(fontawesome)          # Icons display in ggplot2
library(ggtext)               # Markdown text support for ggplot2
library(showtext)             # Display fonts in ggplot2
library(colorspace)           # Lighten and Darken colours
library(patchwork)            # Compiling Plots

# Geocomputation
library(sf)                   # Simple Features in R



sessioninfo::session_info()$packages |> 
  as_tibble() |> 
  select(package, 
         version = loadedversion, 
         date, source) |> 
  arrange(package) |> 
  janitor::clean_names(
    case = "title"
  ) |> 
  gt::gt() |> 
  gt::opt_interactive(
    use_search = TRUE
  ) |> 
  gtExtras::gt_theme_espn()
Table 1: R Packages and their versions used in the creation of this page and graphics