Google Docs

Publish date: Nov 14, 2021
Tags: 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"))
countrylatlng
Argentina-34.996496-64.967282
Austria47.59397014.124560
Belgium50.6402814.666715
Bulgaria42.60739725.485662
Canada61.066692-107.991707
China35.000074104.999927
Cyprus34.98230233.145128
Czechia49.81670015.474954
Denmark55.67024910.333328
France46.6033541.888334
Germany51.08342010.423447
Greece38.99536821.987713
Italy42.63842612.674297
Liechtenstein47.1416319.553153
Mexico23.658512-102.007710
Monaco43.7323497.427683
Nigeria9.6000367.999972
Portugal40.033263-7.889626
Spain39.326068-4.837979
Sweden59.67497114.520858
Switzerland46.7985628.231974
Tunisia33.8439419.400138
Turkey38.95975934.924965
United Arab Emirates24.00024953.999483
United Kingdom54.702354-3.276575
United States of America39.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