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:
countries (ISO 3166-1),
country_subdivisions (ISO 3166-2), and
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.
How I made this graphic?
Loading required libraries, data import & creating custom functions.
Code
# Data Import and Wrangling Toolslibrary(tidyverse) # All things tidy# Final plot toolslibrary(scales) # Nice Scales for ggplot2library(fontawesome) # Icons display in ggplot2library(ggtext) # Markdown text support for ggplot2library(showtext) # Display fonts in ggplot2library(colorspace) # Lighten and Darken colourslibrary(patchwork) # Compiling Plots# Geocomputationlibrary(sf) # Simple Features in R# Load the datacountries <- 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 titlesfont_add_google("Bebas Neue",family ="title_font") # Font for the captionfont_add_google("Saira Extra Condensed",family ="caption_font") # Font for plot textfont_add_google("Bowlby One",family ="body_font") font_add_google("Bree Serif","slab_font")showtext_auto()# A base Colourbase_col <-"black"bg_col <-lighten(base_col, 0.2)seecolor::print_color(bg_col)# Colour for the texttext_col <-lighten(bg_col, 0.8)seecolor::print_color(text_col)# Colour for highlighted texttext_hil <-lighten(bg_col, 0.9)seecolor::print_color(text_hil)# Define Base Text Sizebts <-90# Caption stuff for the plotsysfonts::font_add(family ="Font Awesome 6 Brands",regular = here::here("docs", "Font Awesome 6 Brands-Regular-400.otf"))github <-""github_username <-"aditya-dahiya"xtwitter <-""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)
# Saving a thumbnaillibrary(magick)# Saving a thumbnail for the webpageimage_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 Toolslibrary(tidyverse) # All things tidy# Final plot toolslibrary(scales) # Nice Scales for ggplot2library(fontawesome) # Icons display in ggplot2library(ggtext) # Markdown text support for ggplot2library(showtext) # Display fonts in ggplot2library(colorspace) # Lighten and Darken colourslibrary(patchwork) # Compiling Plots# Geocomputationlibrary(sf) # Simple Features in Rsessioninfo::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()