Google Docs
Publish date: Nov 14, 2021Tags: Programming
library(googlesheets4)
library(sf)
library(opencage) # for geocoding addresses
library(usethis)
library(hrbrthemes) # hrbrmstr/hrbrthemes
library(tidyverse)
library(kableExtra)
library(rnaturalearth)
library(tmap)
library(ggthemes)
# usethis::edit_r_environ() # add Opencage API to your .Renviron file
# Add a line OPENCAGE_KEY="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
# Before you exit, make sure your .Renviron ends with a blank line, then save and close it.
# Restart RStudio after modifying .Renviron in order to load the API key into memory.
# To check everything worked, go to console and type
# Sys.getenv("OPENCAGE_KEY")
googlesheets4::gs4_auth() # google sheets authorisation
#load countries_visited googlesheets
countries_visited <- read_sheet("https://docs.google.com/spreadsheets/d/14k4xrwrMRfabnyqQ2y_mTNdf-gT5KBDMAAS42H44V7E/edit?usp=sharing
")
geocoded <- countries_visited %>%
mutate(
address_geo = purrr::map(country, opencage_forward, limit=1) # the beauty of purrr:map()
) %>%
unnest_wider(address_geo) %>% # opencage returns a list, hence we unnest it...
unnest(results) %>% # look inside the results that opencage returns
rename(lat = geometry.lat, # rename latitude/longitude to lat/lng
lng = geometry.lng) %>%
select(country, lat, lng) # just select country, latitude, longitude
geocoded %>%
kable()%>% # print a table with geocoded addresses
kable_styling(bootstrap_options = c("striped", "hover", "condensed", "responsive"))
country | lat | lng |
---|---|---|
Argentina | -34.996496 | -64.967282 |
Austria | 47.593970 | 14.124560 |
Belgium | 50.640281 | 4.666715 |
Bulgaria | 42.607397 | 25.485662 |
Canada | 61.066692 | -107.991707 |
China | 35.000074 | 104.999927 |
Cyprus | 34.982302 | 33.145128 |
Czechia | 49.816700 | 15.474954 |
Denmark | 55.670249 | 10.333328 |
France | 46.603354 | 1.888334 |
Germany | 51.083420 | 10.423447 |
Greece | 38.995368 | 21.987713 |
Italy | 42.638426 | 12.674297 |
Liechtenstein | 47.141631 | 9.553153 |
Mexico | 23.658512 | -102.007710 |
Monaco | 43.732349 | 7.427683 |
Nigeria | 9.600036 | 7.999972 |
Portugal | 40.033263 | -7.889626 |
Spain | 39.326068 | -4.837979 |
Sweden | 59.674971 | 14.520858 |
Switzerland | 46.798562 | 8.231974 |
Tunisia | 33.843941 | 9.400138 |
Turkey | 38.959759 | 34.924965 |
United Arab Emirates | 24.000249 | 53.999483 |
United Kingdom | 54.702354 | -3.276575 |
United States of America | 39.783730 | -100.445882 |
# we will use the rnatural earth package to get a medium resolution
# vector map of world countries excl. Antarctica
world <- ne_countries(scale = "medium", returnclass = "sf") %>%
filter(name != "Antarctica")
st_geometry(world) # what is the geometry?
## Geometry set for 240 features
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: -180 ymin: -58.49229 xmax: 180 ymax: 83.59961
## CRS: +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
## First 5 geometries:
# CRS: +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
ggplot(data = world) +
geom_sf() + # the first two lines just plot the world shapefile
geom_point(data = geocoded, # then we add points
aes(x = lng, y = lat),
size = 2,
colour = "#001e62") +
theme_void()
world_visited <- left_join(world, geocoded, by=c("admin" = "country" )) %>%
mutate(visited = if_else (!is.na(lat), "visited", "not visited")
)
ggplot(world_visited)+
geom_sf(aes(fill=visited),
show.legend = FALSE)+ # no legend
scale_fill_manual(values=c('#f0f0f0', '#3182bd'))+
coord_sf(datum = NA) +
theme_void()+
labs(title="Which countries have I travelled to?")+
theme_ipsum_rc(grid="", strip_text_face = "bold") +
NULL