Home > database >  Choropleth map and ISO-3 codes
Choropleth map and ISO-3 codes

Time:01-05

I am trying to use ggplot2 for plotting the Choropleth map. My original data are with ISO-3 code which means that are includes code with three letters (e.g For Afghanistan code is AFG etc.).

For plotting the Choropleth map, ggplot2 instead of ISO-3 code uses the names of countries that do not correspond with the full names of the countries ( e.g United states are USA, United Kingdom is UK, etc.) As you can see some of the name abbreviations of the names of the countries.

Below you can see data from ggplot2 and how it works.

library(ggplot2)
mapdata <- map_data("world") 

Take into account that for one country you can find different names, these names are not precise for me, and for that reason, I must merge this data with ISO-3 codes.

So can anybody help me how to solve this problem and the names from ggplot2, mapdata join for each country with ISO-3 code?

CodePudding user response:

You can use the package countrycode to convert country names to a lot of different codes, including ISO codes, and vice versa. Note that the conversion fails for a few countries/islands (see warning message below):

library(ggplot2)
library(countrycode)

mapdata <- map_data("world") 

converted <- countrycode(mapdata$region, "country.name", "iso3c")
#> Warning in countrycode_convert(sourcevar = sourcevar, origin = origin, destination = dest, : Some values were not matched unambiguously: Ascension Island, Azores, Barbuda, Bonaire, Canary Islands, Chagos Archipelago, Grenadines, Heard Island, Kosovo, Madeira Islands, Micronesia, Saba, Saint Martin, Siachen Glacier, Sint Eustatius, Virgin Islands

unique(converted)
#>   [1] "ABW" "AFG" "AGO" "AIA" "ALB" "FIN" "AND" "ARE" "ARG" "ARM" "ASM" "ATA"
#>  [13] "AUS" "ATF" "ATG" NA    "AUT" "AZE" "BDI" "BEL" "BEN" "BFA" "BGD" "BGR"
#>  [25] "BHR" "BHS" "BIH" "BLM" "BLR" "BLZ" "BMU" "BOL" "BRA" "BRB" "BRN" "BTN"
#>  [37] "BWA" "CAF" "CAN" "CHE" "CHL" "CHN" "CIV" "CMR" "COD" "COG" "COK" "COL"
#>  [49] "COM" "CPV" "CRI" "CUB" "CUW" "CYM" "CYP" "CZE" "DEU" "DJI" "DMA" "DNK"
#>  [61] "DOM" "DZA" "ECU" "EGY" "ERI" "ESP" "EST" "ETH" "FJI" "FLK" "REU" "MYT"
#>  [73] "GUF" "MTQ" "GLP" "FRA" "FRO" "GAB" "GBR" "GEO" "GGY" "GHA" "GIN" "GMB"
#>  [85] "GNB" "GNQ" "GRC" "GRD" "GRL" "GTM" "GUM" "GUY" "HND" "HRV" "HTI" "HUN"
#>  [97] "IDN" "IMN" "IND" "CCK" "CXR" "IRL" "IRN" "IRQ" "ISL" "ISR" "ITA" "SMR"
#> [109] "JAM" "JEY" "JOR" "JPN" "KAZ" "KEN" "KGZ" "KHM" "KIR" "KNA" "KOR" "KWT"
#> [121] "LAO" "LBN" "LBR" "LBY" "LCA" "LIE" "LKA" "LSO" "LTU" "LUX" "LVA" "MAR"
#> [133] "MCO" "MDA" "MDG" "MDV" "MEX" "MHL" "MKD" "MLI" "MLT" "MMR" "MNE" "MNG"
#> [145] "MNP" "MOZ" "MRT" "MSR" "MUS" "MWI" "MYS" "NAM" "NCL" "NER" "NFK" "NGA"
#> [157] "NIC" "NIU" "NLD" "NOR" "NPL" "NRU" "NZL" "OMN" "PAK" "PAN" "PCN" "PER"
#> [169] "PHL" "PLW" "PNG" "POL" "PRI" "PRK" "PRT" "PRY" "PSE" "PYF" "QAT" "ROU"
#> [181] "RUS" "RWA" "ESH" "SAU" "SDN" "SSD" "SEN" "SGP" "SGS" "SHN" "SLB" "SLE"
#> [193] "SLV" "SOM" "SPM" "SRB" "STP" "SUR" "SVK" "SVN" "SWE" "SWZ" "SXM" "SYC"
#> [205] "SYR" "TCA" "TCD" "TGO" "THA" "TJK" "TKM" "TLS" "TON" "TTO" "TUN" "TUR"
#> [217] "TWN" "TZA" "UGA" "UKR" "URY" "USA" "UZB" "VAT" "VCT" "VEN" "VNM" "VUT"
#> [229] "WLF" "WSM" "YEM" "ZAF" "ZMB" "ZWE"

Created on 2023-01-04 with reprex v2.0.2

CodePudding user response:

another approach:

  • grab some online ISO-3 ressource and convert it to a dataframe, e. g.:
library(rvest)

html_source <- read_html('https://www.iban.com/country-codes')

iso3 <- html_source |> 
  html_element('#myTable') |>
  html_table() |>
  rename(region = Country)
  • fuzzy-join it by, e. g. region:
library(fuzzyjoin)

mapdata_iso3 <- 
  mapdata |>
  stringdist_join(iso3, max_dist = 1) ## adapt max_dist as fit
## > mapdata_iso3 |> 
##     select(region = region.x, `Alpha-3 code`) |> head()
##     region Alpha-3 code
## 1  Aruba          ABW
## 2  Aruba          ABW
## 3  Aruba          ABW
## 4  Aruba          ABW
## 5  Aruba          ABW
## 6  Aruba          ABW
  • Related