Site icon R-bloggers

Country codes

[This article was first published on r.iresmi.net, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Physicists of many nations – CC BY-NC-ND by Ann Fisher

Day 3 of 30DayMapChallenge: « Polygons » (previously).

An interesting challenge a few weeks ago on https://en.osm.town/@opencage/115271196316302891:

< svg xmlns="http://www.w3.org/2000/svg" xlink="http://www.w3.org/1999/xlink" width="32" height="32" viewbox="0 0 79 75">< path d="M63 45.3v-20c0-4.1-1-7.3-3.2-9.7-2.1-2.4-5-3.7-8.5-3.7-4.1 0-7.2 1.6-9.3 4.7l-2 3.3-2-3.3c-2-3.1-5.1-4.7-9.2-4.7-3.5 0-6.4 1.3-8.6 3.7-2.1 2.4-3.1 5.6-3.1 9.7v20h8V25.9c0-4.1 1.7-6.2 5.2-6.2 3.8 0 5.8 2.5 5.8 7.4V37.7H44V27.1c0-4.9 1.9-7.4 5.8-7.4 3.5 0 5.2 2.1 5.2 6.2V45.3h8ZM74.7 16.6c.6 6 .1 15.7.1 17.3 0 .5-.1 4.8-.1 5.3-.7 11.5-8 16-15.6 17.5-.1 0-.2 0-.3 0-4.9 1-10 1.2-14.9 1.4-1.2 0-2.4 0-3.6 0-4.8 0-9.7-.6-14.4-1.7-.1 0-.1 0-.1 0s-.1 0-.1 0 0 .1 0 .1 0 0 0 0c.1 1.6.4 3.1 1 4.5.6 1.7 2.9 5.7 11.4 5.7 5 0 9.9-.6 14.8-1.7 0 0 0 0 0 0 .1 0 .1 0 .1 0 0 .1 0 .1 0 .1.1 0 .1 0 .1.1v5.6s0 .1-.1.1c0 0 0 0 0 .1-1.6 1.1-3.7 1.7-5.6 2.3-.8.3-1.6.5-2.4.7-7.5 1.7-15.4 1.3-22.7-1.2-6.8-2.4-13.8-8.2-15.5-15.2-.9-3.8-1.6-7.6-1.9-11.5-.6-5.8-.6-11.7-.8-17.5C3.9 24.5 4 20 4.9 16 6.7 7.9 14.1 2.2 22.3 1c1.4-.2 4.1-1 16.5-1h.1C51.4 0 56.7.8 58.1 1c8.4 1.2 15.5 7.5 16.6 15.6Z" fill="currentColor">
Post by @opencage@en.osm.town
View on Mastodon

Name countries whose ISO 3166-1 alpha-2 code is contained as a substring in the common English version of the country’s name.

For example: Italy has its ISO code “IT” in its name; “SE” is the ISO code for Sweden, but is not found in its name.

Let’s map that…

< section id="setup" class="level2">

Setup

library(dplyr)
library(stringr)
library(purrr)
library(emoji)
library(rvest)
library(glue)
library(gt)
library(ggplot2)
library(giscoR)
library(janitor)
library(sf)
library(jsonlite)
< section id="data" class="level2">

Data

# ISO_3166-1_alpha-2 codes
iso_3166_a2 <- read_html("https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2") |> 
  html_table(na.strings = "") |> # take care not to interpret Namibia as NA!
  pluck(4) |> 
  rename(code = Code,
         name = `Country name (using title case)`) 

# Using data from {giscoR}, more adapted than {rnaturalearth} for alpha-2 codes
# but we still need some manual cleaning
countries <- gisco_countries |> 
  clean_names() |> 
  mutate(cntr_id = case_match(cntr_id,
                              "EL" ~ "GR",
                              "GB" ~ "UK",
                              .default = cntr_id))

# I want an Equal Earth projection centered on the Pacific (EPSG:8859)
# we must correct the geometry at the anti meridian, so we must get the 
# projection origin
epsg <- "8859"
origin <- fromJSON(glue("https://epsg.io/{epsg}.json")) |> 
  pluck("conversion", "parameters") |>
  filter(name == "Longitude of natural origin") |> 
  pull(value) # should be 150

# For the map background
ocean <- st_bbox(countries) |> 
  st_as_sfc() |> 
  st_break_antimeridian(lon_0 = origin) |> 
  st_segmentize(units::set_units(100, km))
< section id="solving-the-problem" class="level2">

Solving the problem

Actually this is the easiest part, once the data is clean!

results <- iso_3166_a2 |> 
  filter(str_detect(name, regex(code, ignore_case = TRUE)))
< section id="results" class="level1">

Results

From the 249 countries having an ISO code, 59 match our query (Table 1). For all of them the code appears in the two first characters.

There is at least one missing! New Caledonia code is NC; this code is not in its name but this territory is part of FraNCe. According to lenient rules it counts…

# using emoji flags for display, we need some more data wrangling
results |> 
  mutate(name_flag = name |> 
           str_split_i(",", 1)|> 
           str_replace_all(
             c("Lao People's Democratic Republic" = "Laos",
               "Russian Federation" = "Russia",
               "Syrian Arab Republic" = "Syria",
               "Virgin Islands \\(U\\.S\\.\\)" = "U.S. Virgin Islands"))) |> 
  mutate(flag = map(name_flag, possibly(\(x) flag(x), otherwise = "")),
         display = glue("{flag} {name} ({code})")) |> 
  arrange(display) |> 
  select(display) |> 
  gt() |> 
  cols_label(display = "Country") |> 
  cols_align(align = "left")
Country
🇦🇫 Afghanistan (AF)
🇦🇱 Albania (AL)
🇦🇷 Argentina (AR)
🇦🇺 Australia (AU)
🇦🇿 Azerbaijan (AZ)
🇧🇪 Belgium (BE)
🇧🇴 Bolivia, Plurinational State of (BO)
🇧🇷 Brazil (BR)
🇨🇦 Canada (CA)
🇨🇴 Colombia (CO)
🇨🇺 Cuba (CU)
🇨🇾 Cyprus (CY)
🇨🇿 Czechia (CZ)
🇩🇯 Djibouti (DJ)
🇩🇴 Dominican Republic (DO)
🇪🇨 Ecuador (EC)
🇪🇬 Egypt (EG)
🇪🇷 Eritrea (ER)
🇪🇹 Ethiopia (ET)
🇫🇮 Finland (FI)
🇫🇷 France (FR)
🇬🇦 Gabon (GA)
🇬🇪 Georgia (GE)
🇬🇭 Ghana (GH)
🇬🇮 Gibraltar (GI)
🇬🇷 Greece (GR)
🇬🇺 Guam (GU)
🇭🇺 Hungary (HU)
🇮🇳 India (IN)
🇮🇷 Iran, Islamic Republic of (IR)
🇮🇹 Italy (IT)
🇯🇪 Jersey (JE)
🇯🇴 Jordan (JO)
🇰🇪 Kenya (KE)
🇰🇮 Kiribati (KI)
🇱🇦 Lao People’s Democratic Republic (LA)
🇱🇮 Liechtenstein (LI)
🇱🇺 Luxembourg (LU)
🇳🇦 Namibia (NA)
🇳🇮 Nicaragua (NI)
🇳🇴 Norway (NO)
🇴🇲 Oman (OM)
🇵🇦 Panama (PA)
🇵🇪 Peru (PE)
🇵🇭 Philippines (PH)
🇶🇦 Qatar (QA)
🇷🇴 Romania (RO)
🇷🇺 Russian Federation (RU)
🇷🇼 Rwanda (RW)
🇸🇦 Saudi Arabia (SA)
🇸🇴 Somalia (SO)
🇸🇾 Syrian Arab Republic (SY)
🇹🇭 Thailand (TH)
🇹🇴 Tonga (TO)
🇺🇬 Uganda (UG)
🇺🇿 Uzbekistan (UZ)
🇻🇪 Venezuela, Bolivarian Republic of (VE)
🇻🇮 Virgin Islands (U.S.) (VI)
🇾🇪 Yemen (YE)
Table 1: Countries whose ISO 3166-1 alpha-2 code is contained as a substring in their name
< section id="map" class="level2">

Map

Now, to fulfill the 30DayMapChallenge, a classic choropleth map.

countries |> 
  st_break_antimeridian(lon_0 = origin) |> 
  left_join(results,
            join_by(cntr_id == code)) |> 
  ggplot() +
  geom_sf(data = ocean, fill = "paleturquoise", color = NA, alpha = .4) +
  geom_sf(aes(fill = !is.na(name), color = !is.na(name))) +
  scale_fill_manual(values =  c("TRUE" = "darkolivegreen3",
                                "FALSE" = "snow2"),
                    labels = c("TRUE" = "yes",
                               "FALSE" = "no")) +
  scale_color_manual(values =  c("TRUE" = "darkolivegreen4",
                                 "FALSE" = "snow3"),
                     labels = c("TRUE" = "yes",
                                "FALSE" = "no")) +
  coord_sf(crs = glue("EPSG:{epsg}")) +
  guides(fill = guide_legend(reverse = TRUE),
         color = guide_legend(reverse = TRUE)) +
  labs(title = glue("Countries whose ISO 3166-1 alpha-2 code is contained as a \\
                    substring in their name"),
       fill = "name has ISO alpha-2 ?",
       color = "name has ISO alpha-2 ?",
       caption = glue("data : Gisco, Wikipedia
                      https://r.iresmi.net/ {Sys.Date()}")) +
  theme_minimal() +
  theme(plot.caption = element_text(size = 6),
        legend.position = "bottom",
        plot.background = element_rect(fill = "white", color = NA))
Figure 1: Countries whose ISO 3166-1 alpha-2 code is contained as a substring in their name
< !-- -->
To leave a comment for the author, please follow the link and comment on their blog: r.iresmi.net.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Exit mobile version