Reputation: 61
I have been looking now for a while and I fail to find a package which returns latitudes and longitudes of ISO country codes.
There are datasets which contain many countries and their respective latitudes / longitudes, but the lists I found do not include "countries" like the Cayman Islands, etc.. My plan would be to get longitudes and latitudes for almost every "country" with an ISO 3 code, here is a list: https://en.wikipedia.org/wiki/ISO_3166-1_alpha-3
or the countries which I use specifically:
country_url <- "https://raw.githubusercontent.com/lukes/ISO-3166-Countries-with-Regional-Codes/master/all/all.csv"
country_all_codes <- readr::read_csv(url(country_url))
The latitudes and longitudes should then be used in a leaflet
map!
Any comment is appreciated!
PS:
Principally it should work with the ggmap package
, but since I am writing the code for someone else, I don't want to be registered with an Google account..
Upvotes: 1
Views: 865
Reputation: 8699
You can source the list of countries (with spatial information) via giscoR::gisco_get_counties()
.
It will return countries as polygons, to reduce the data to single points consider sf::st_centroid()
. Just be aware that there can be some confusion on what constitutes a country - does "France" as a country mean Metropolitan France full stop, or are the overseas parts in? These will have an impact on the location of the centroid. Some tweaking may be necessary.
But the dataset does contain Cayman Islands, has a column for ISO code and is compatible with both ggmap
and leaflet
packages.
library(sf)
library(dplyr)
library(leaflet)
countries <- giscoR::gisco_get_countries() %>%
st_centroid()
leaflet(data = countries) %>%
addProviderTiles("Stamen.Watercolor") %>%
addMarkers(label = ~ ISO3_CODE)
Also note that with the choice of basemap is highly personal. I am mostly showing off with the watercolor, Stamen.Toner or CartoDB.Positron might be more sensible choices...
Upvotes: 1