# 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 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 filter(name == "Longitude of natural origin") |> pull(value) # should be 150 # For the map background ocean st_as_sfc() |> st_break_antimeridian(lon_0 = origin) |> st_segmentize(units::set_units(100, km)) Solving the problem Actually this is the easiest part, once the data is clean! results filter(str_detect(name, regex(code, ignore_case = TRUE))) 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 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 " />

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.

A photo of many flags

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:

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…

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)

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))

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)))

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

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))
A map of showing countries whose ISO 3166-1 alpha-2 code is contained as a substring in their name
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.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)