World map of visited countries in R

Antoine Soetewey 2020-01-09 11 minute read

Like me, if you like traveling as much as R you might want to draw a world map of the countries you have visited in R. Below an example with the countries I have visited as of January 2020:

To draw this map in R, you will need the following packages:

library(highcharter)
library(dplyr)
library(maps)

As usual, you need the packages to be installed on your machine before loading them with library(). You can install a package with the command install.packages("name_of_package").

After having loaded the packages, we are going to use the dataset called iso3166 from the {maps} package and rename it dat. Here are the first 6 rows of the dataset:

dat <- iso3166
head(dat)
##   a2  a3       ISOname               mapname sovereignty
## 1 AW ABW         Aruba                 Aruba Netherlands
## 2 AF AFG   Afghanistan           Afghanistan Afghanistan
## 3 AO AGO        Angola                Angola      Angola
## 4 AI AIA      Anguilla              Anguilla    Anguilla
## 5 AX ALA Aland Islands Finland:Aland Islands     Finland
## 6 AL ALB       Albania               Albania     Albania

We rename the variable a3 by iso-a3:

dat <- rename(dat, "iso-a3" = a3)
head(dat)
##   a2 iso-a3       ISOname               mapname sovereignty
## 1 AW    ABW         Aruba                 Aruba Netherlands
## 2 AF    AFG   Afghanistan           Afghanistan Afghanistan
## 3 AO    AGO        Angola                Angola      Angola
## 4 AI    AIA      Anguilla              Anguilla    Anguilla
## 5 AX    ALA Aland Islands Finland:Aland Islands     Finland
## 6 AL    ALB       Albania               Albania     Albania

We save the visited countries in a vector called countries_visited. To know the ISO codes of the countries you have visited, check the column ISOname in the dataset and extract the ISO codes from the column iso-a3:

countries_visited <- c("AUS", "BEL", "CAN", "CZE", "DNK", "FIN", "FRA", "DEU", "GRC", "HUN", "IRL", "ITA", "LVA", "LUX", "MCO", "MMR", "NLD", "NZL", "NOR", "PRT", "ROU", "SGP", "ESP", "SWE", "CHE", "TWN", "THA", "GBR", "USA")

We now create a new variable called visited which equals to 1 if you have visited the country and 0 otherwise:

dat$visited <- ifelse(dat$`iso-a3` %in% countries_visited, 1, 0)
head(dat)
##   a2 iso-a3       ISOname               mapname sovereignty visited
## 1 AW    ABW         Aruba                 Aruba Netherlands       0
## 2 AF    AFG   Afghanistan           Afghanistan Afghanistan       0
## 3 AO    AGO        Angola                Angola      Angola       0
## 4 AI    AIA      Anguilla              Anguilla    Anguilla       0
## 5 AX    ALA Aland Islands Finland:Aland Islands     Finland       0
## 6 AL    ALB       Albania               Albania     Albania       0

Finally, we are ready to draw the world map thanks to the hcmap() command from the {highcharter} package:

hcmap(
  map = "custom/world-highres3", # high resolution world map
  data = dat, # name of dataset
  joinBy = "iso-a3",
  value = "visited",
  showInLegend = FALSE, # hide legend
  nullColor = "#DADADA",
  download_map_data = TRUE
) %>%
  hc_mapNavigation(enabled = FALSE) %>%
  hc_legend("none") %>%
  hc_title(text = "World map") # title

Change the arguments to your needs and you are good to go. To go even further, you can also add a list including all your visited countries thanks to this code:

dat <- subset(dat, dat$visited == 1)
sort(dat$ISOname) # sort is to have the visited countries in alphabetical order
##  [1] "Australia"                                           
##  [2] "Belgium"                                             
##  [3] "Canada"                                              
##  [4] "Clipperton Island"                                   
##  [5] "Czech Republic"                                      
##  [6] "Denmark"                                             
##  [7] "Finland"                                             
##  [8] "France"                                              
##  [9] "Germany"                                             
## [10] "Greece"                                              
## [11] "Hungary"                                             
## [12] "Ireland"                                             
## [13] "Italy"                                               
## [14] "Latvia"                                              
## [15] "Luxembourg"                                          
## [16] "Monaco"                                              
## [17] "Myanmar"                                             
## [18] "Netherlands"                                         
## [19] "New Zealand"                                         
## [20] "Norway"                                              
## [21] "Portugal"                                            
## [22] "Portugal"                                            
## [23] "Portugal"                                            
## [24] "Romania"                                             
## [25] "Singapore"                                           
## [26] "Spain"                                               
## [27] "Spain"                                               
## [28] "Sweden"                                              
## [29] "Switzerland"                                         
## [30] "Taiwan"                                              
## [31] "Thailand"                                            
## [32] "United Kingdom of Great Britain and Northern Ireland"
## [33] "United States"

And count the number of countries visited:

paste(
  "Total: ",
  sum(dat$visited),
  " countries."
)
## [1] "Total:  33  countries."

In conclusion, here is the entire code to draw a world map with visited countries highlighted, a list of all countries in alphabetical order and the number of countries visited:

library(highcharter)
library(dplyr)
library(maps)

dat <- iso3166
dat <- rename(dat, "iso-a3" = a3)
countries_visited <- c("AUS", "BEL", "CAN", "CZE", "DNK", "FIN", "FRA", "DEU", "GRC", "HUN", "IRL", "ITA", "LVA", "LUX", "MCO", "MMR", "NLD", "NZL", "NOR", "PRT", "ROU", "SGP", "ESP", "SWE", "CHE", "TWN", "THA", "GBR", "USA")
dat$visited <- ifelse(dat$`iso-a3` %in% countries_visited, 1, 0)

hcmap(
  map = "custom/world-highres3", # high resolution world map
  data = dat, # name of dataset
  joinBy = "iso-a3",
  value = "visited",
  showInLegend = FALSE, # hide legend
  nullColor = "#DADADA",
  download_map_data = TRUE
) %>%
  hc_mapNavigation(enabled = FALSE) %>%
  hc_legend("none") %>%
  hc_title(text = "World map") # title

dat <- subset(dat, dat$visited == 1)
sort(dat$ISOname) # sort is to have the visited countries in alphabetical order

paste(
  "Total: ",
  sum(dat$visited),
  " countries."
)

Thanks for reading. I hope this article helped you to draw a world map with visited countries highlighted in R.

As always, if you have a question or a suggestion related to the topic covered in this article, please add it as a comment so other readers can benefit from the discussion.



Liked this post?

Get updates every time a new article is published.
No spam and unsubscribe anytime.


↑ Back to top