Skip to content

Instantly share code, notes, and snippets.

@juanchiem
Last active April 10, 2022 13:13
Show Gist options
  • Save juanchiem/3690306a2f7d7b36ac251fd55cf05e09 to your computer and use it in GitHub Desktop.
Save juanchiem/3690306a2f7d7b36ac251fd55cf05e09 to your computer and use it in GitHub Desktop.
# remotes::install_github("ropensci/gistr")
pacman::p_load(tidyverse, gistr)
options(github.username = 'juanchiem')
gists('mineall')
gist("3690306a2f7d7b36ac251fd55cf05e09") %>% gist_save() %>% gist_open()
# Get country centroids---
pacman::p_load(rgeos, rworldmap)
# get world map
wmap <- getMap(resolution="low")
# get centroids
centroids <- gCentroid(wmap, byid=TRUE)
# get a data.frame with centroids
df_centr <- as.data.frame(centroids)
head(df_centr)
# Get people info ----
pacman::p_load(googlesheets4)
gs4_auth(email = "[email protected]")
sheet <-gs4_get("https://docs.google.com/spreadsheets/d/1n3OvqpgfQThSh8I3nVNmih4WmOT4mN6TGFhTKMhUj_M/edit?usp=sharing")
alumnos <- read_sheet(sheet) %>%
mutate(country= recode(pais,
BRA= "Brazil",
URU="Uruguay",
ARG="Argentina",
COL="Colombia",
VEN="Venezuela",
BOL="Bolivia",
ECU="Ecuador",
CHI="Chile",
PAR="Paraguay",
PER="Peru",
MEX="Mexico",
CUB="Cuba",
USA= "United States of America"))
# Summarize info and append coords ----
puntos_geo <-
alumnos %>%
count(country) %>%
left_join(df_centr %>% rownames_to_column("country")) %>%
mutate(x = replace(x, country == "United States of America", -107),
y = replace(y, country == "United States of America", 40))
# Plot map ----
pacman::p_load(sf, ggspatial)
puntos_geo %>%
ggplot(aes(x = x, y = y)) +
annotation_map_tile(type = "cartolight", zoom=3, zoomin = 0) +
geom_spatial_point(aes(size = n)) +
geom_spatial_label_repel(aes(label = n), box.padding = .2, size = 3, alpha=.5) +
ylim(c(-52, 40)) +
xlim(c(-115, -40)) +
coord_sf(crs = 4326) +
theme_void() +
guides(size="none")
@juanchiem
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment