[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:
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 codesiso_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 cleaningcountries <- 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 originepsg <- "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 backgroundocean <- 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 wranglingresults |> 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")
#otiawootke table { font-family: system-ui, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji'; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale;}#otiawootke thead, #otiawootke tbody, #otiawootke tfoot, #otiawootke tr, #otiawootke td, #otiawootke th { border-style: none;}#otiawootke p { margin: 0; padding: 0;}#otiawootke .gt_table { display: table; border-collapse: collapse; line-height: normal; margin-left: auto; margin-right: auto; color: #333333; font-size: 16px; font-weight: normal; font-style: normal; background-color: #FFFFFF; width: auto; border-top-style: solid; border-top-width: 2px; border-top-color: #A8A8A8; border-right-style: none; border-right-width: 2px; border-right-color: #D3D3D3; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #A8A8A8; border-left-style: none; border-left-width: 2px; border-left-color: #D3D3D3;}#otiawootke .gt_caption { padding-top: 4px; padding-bottom: 4px;}#otiawootke .gt_title { color: #333333; font-size: 125%; font-weight: initial; padding-top: 4px; padding-bottom: 4px; padding-left: 5px; padding-right: 5px; border-bottom-color: #FFFFFF; border-bottom-width: 0;}#otiawootke .gt_subtitle { color: #333333; font-size: 85%; font-weight: initial; padding-top: 3px; padding-bottom: 5px; padding-left: 5px; padding-right: 5px; border-top-color: #FFFFFF; border-top-width: 0;}#otiawootke .gt_heading { background-color: #FFFFFF; text-align: center; border-bottom-color: #FFFFFF; border-left-style: none; border-left-width: 1px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 1px; border-right-color: #D3D3D3;}#otiawootke .gt_bottom_border { border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #D3D3D3;}#otiawootke .gt_col_headings { border-top-style: solid; border-top-width: 2px; border-top-color: #D3D3D3; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #D3D3D3; border-left-style: none; border-left-width: 1px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 1px; border-right-color: #D3D3D3;}#otiawootke .gt_col_heading { color: #333333; background-color: #FFFFFF; font-size: 100%; font-weight: normal; text-transform: inherit; border-left-style: none; border-left-width: 1px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 1px; border-right-color: #D3D3D3; vertical-align: bottom; padding-top: 5px; padding-bottom: 6px; padding-left: 5px; padding-right: 5px; overflow-x: hidden;}#otiawootke .gt_column_spanner_outer { color: #333333; background-color: #FFFFFF; font-size: 100%; font-weight: normal; text-transform: inherit; padding-top: 0; padding-bottom: 0; padding-left: 4px; padding-right: 4px;}#otiawootke .gt_column_spanner_outer:first-child { padding-left: 0;}#otiawootke .gt_column_spanner_outer:last-child { padding-right: 0;}#otiawootke .gt_column_spanner { border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #D3D3D3; vertical-align: bottom; padding-top: 5px; padding-bottom: 5px; overflow-x: hidden; display: inline-block; width: 100%;}#otiawootke .gt_spanner_row { border-bottom-style: hidden;}#otiawootke .gt_group_heading { padding-top: 8px; padding-bottom: 8px; padding-left: 5px; padding-right: 5px; color: #333333; background-color: #FFFFFF; font-size: 100%; font-weight: initial; text-transform: inherit; border-top-style: solid; border-top-width: 2px; border-top-color: #D3D3D3; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #D3D3D3; border-left-style: none; border-left-width: 1px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 1px; border-right-color: #D3D3D3; vertical-align: middle; text-align: left;}#otiawootke .gt_empty_group_heading { padding: 0.5px; color: #333333; background-color: #FFFFFF; font-size: 100%; font-weight: initial; border-top-style: solid; border-top-width: 2px; border-top-color: #D3D3D3; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #D3D3D3; vertical-align: middle;}#otiawootke .gt_from_md > :first-child { margin-top: 0;}#otiawootke .gt_from_md > :last-child { margin-bottom: 0;}#otiawootke .gt_row { padding-top: 8px; padding-bottom: 8px; padding-left: 5px; padding-right: 5px; margin: 10px; border-top-style: solid; border-top-width: 1px; border-top-color: #D3D3D3; border-left-style: none; border-left-width: 1px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 1px; border-right-color: #D3D3D3; vertical-align: middle; overflow-x: hidden;}#otiawootke .gt_stub { color: #333333; background-color: #FFFFFF; font-size: 100%; font-weight: initial; text-transform: inherit; border-right-style: solid; border-right-width: 2px; border-right-color: #D3D3D3; padding-left: 5px; padding-right: 5px;}#otiawootke .gt_stub_row_group { color: #333333; background-color: #FFFFFF; font-size: 100%; font-weight: initial; text-transform: inherit; border-right-style: solid; border-right-width: 2px; border-right-color: #D3D3D3; padding-left: 5px; padding-right: 5px; vertical-align: top;}#otiawootke .gt_row_group_first td { border-top-width: 2px;}#otiawootke .gt_row_group_first th { border-top-width: 2px;}#otiawootke .gt_summary_row { color: #333333; background-color: #FFFFFF; text-transform: inherit; padding-top: 8px; padding-bottom: 8px; padding-left: 5px; padding-right: 5px;}#otiawootke .gt_first_summary_row { border-top-style: solid; border-top-color: #D3D3D3;}#otiawootke .gt_first_summary_row.thick { border-top-width: 2px;}#otiawootke .gt_last_summary_row { padding-top: 8px; padding-bottom: 8px; padding-left: 5px; padding-right: 5px; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #D3D3D3;}#otiawootke .gt_grand_summary_row { color: #333333; background-color: #FFFFFF; text-transform: inherit; padding-top: 8px; padding-bottom: 8px; padding-left: 5px; padding-right: 5px;}#otiawootke .gt_first_grand_summary_row { padding-top: 8px; padding-bottom: 8px; padding-left: 5px; padding-right: 5px; border-top-style: double; border-top-width: 6px; border-top-color: #D3D3D3;}#otiawootke .gt_last_grand_summary_row_top { padding-top: 8px; padding-bottom: 8px; padding-left: 5px; padding-right: 5px; border-bottom-style: double; border-bottom-width: 6px; border-bottom-color: #D3D3D3;}#otiawootke .gt_striped { background-color: rgba(128, 128, 128, 0.05);}#otiawootke .gt_table_body { border-top-style: solid; border-top-width: 2px; border-top-color: #D3D3D3; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #D3D3D3;}#otiawootke .gt_footnotes { color: #333333; background-color: #FFFFFF; border-bottom-style: none; border-bottom-width: 2px; border-bottom-color: #D3D3D3; border-left-style: none; border-left-width: 2px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 2px; border-right-color: #D3D3D3;}#otiawootke .gt_footnote { margin: 0px; font-size: 90%; padding-top: 4px; padding-bottom: 4px; padding-left: 5px; padding-right: 5px;}#otiawootke .gt_sourcenotes { color: #333333; background-color: #FFFFFF; border-bottom-style: none; border-bottom-width: 2px; border-bottom-color: #D3D3D3; border-left-style: none; border-left-width: 2px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 2px; border-right-color: #D3D3D3;}#otiawootke .gt_sourcenote { font-size: 90%; padding-top: 4px; padding-bottom: 4px; padding-left: 5px; padding-right: 5px;}#otiawootke .gt_left { text-align: left;}#otiawootke .gt_center { text-align: center;}#otiawootke .gt_right { text-align: right; font-variant-numeric: tabular-nums;}#otiawootke .gt_font_normal { font-weight: normal;}#otiawootke .gt_font_bold { font-weight: bold;}#otiawootke .gt_font_italic { font-style: italic;}#otiawootke .gt_super { font-size: 65%;}#otiawootke .gt_footnote_marks { font-size: 75%; vertical-align: 0.4em; position: initial;}#otiawootke .gt_asterisk { font-size: 100%; vertical-align: 0;}#otiawootke .gt_indent_1 { text-indent: 5px;}#otiawootke .gt_indent_2 { text-indent: 10px;}#otiawootke .gt_indent_3 { text-indent: 15px;}#otiawootke .gt_indent_4 { text-indent: 20px;}#otiawootke .gt_indent_5 { text-indent: 25px;}#otiawootke .katex-display { display: inline-flex !important; margin-bottom: 0.75em !important;}#otiawootke div.Reactable > div.rt-table > div.rt-thead > div.rt-tr.rt-tr-group-header > div.rt-th-group:after { height: 0px !important;}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
Continue reading:
Country codes