Skip to content

Instantly share code, notes, and snippets.

@benmarwick
Created October 16, 2021 06:33
Show Gist options
  • Save benmarwick/90fb6bc1284c4bc99089bc568a47f80e to your computer and use it in GitHub Desktop.
Save benmarwick/90fb6bc1284c4bc99089bc568a47f80e to your computer and use it in GitHub Desktop.
Tektite maps for southeast Asia & Vietnam
library(tidyverse)
library(sf)
library(googlesheets4)
# get our data from google sheets, we need to:
# - 'publish to web'
# - adjust sharing settings to share with anyone
# I've done these, now, so it should just work:
my_key <- "1xUqRGnb9kwBi128cERiHkV5w4uREwl-mqAVf0jmQhhg"
# Authorize user on Google
googlesheets4::gs4_auth()
# download the spreadsheet into R
tektite_locations <-
my_key %>%
read_sheet(sheet = "Geological tektites")
# clean and tidy the coodinates column
# devtools::install_github("ropenscilabs/parzer")
library(parzer)
tektite_locations_clean <-
tektite_locations %>%
separate(`Location Coordinates (DDM)`,
into = c("lat", "lon"),
sep = "E|W") %>%
mutate(lon = ifelse(str_detect(lon, "S"), paste0("-", lon), lon)) %>%
mutate(lon = str_replace_all(lon, regex("N|S|°|'"), " ")) %>%
mutate(lat = str_replace_all(lat, regex("°|'"), " ")) %>%
mutate(lon = parse_lon(lon)) %>%
mutate(lat = parse_lat(lat)) %>%
filter_at(vars(lon, lat), ~!is.na(.))
# plot the locations
library(rnaturalearth)
library(rnaturalearthdata)
library(ggspatial)
library(ggrepel)
library(shadowtext)
library(rgeos)
# get a base map
world <- ne_countries(scale = "medium", returnclass = "sf")
vietnam <- ne_countries(scale = "medium",
country = 'vietnam',
returnclass = "sf")
# convert to sf object so it has geometry
tektite_locations_clean_sf <-
st_as_sf(tektite_locations_clean,
coords = c("lon", "lat"),
crs = st_crs(world))
# prepare label text
label_data <-
cbind(tektite_locations_clean_sf,
st_coordinates(tektite_locations_clean_sf$geometry))
# draw the map
tektite_locations_map <-
ggplot() +
geom_sf(data = world,
fill= "#faebd7") +
geom_sf(data = tektite_locations_clean_sf,
colour = "red",
size = 1) +
coord_sf(xlim = c(160, 85),
ylim = c(-50, 35),
expand = FALSE) +
geom_label_repel(data = label_data,
aes(x = X,
y = Y,
label=Map.Label),
label.size = NA,
size = 3,
alpha = 0.8,
segment.alpha = 0.4,
label.padding=0.2,
na.rm=TRUE,
force = 40,
seed = 1234,
max.overlaps = 100) +
annotation_scale(location = "bl",
width_hint = 0.5) +
annotation_north_arrow(location = "bl",
which_north = "true",
pad_x = unit(0.25, "in"),
pad_y = unit(0.25, "in"),
style = north_arrow_fancy_orienteering) +
xlab("Longitude") +
ylab("Latitude") +
ggtitle("") +
theme(panel.background = element_rect(fill = "aliceblue"),
axis.text.y = element_text(size = 6),
axis.text.x = element_text(size = 6))
tektite_locations_map
ggsave("tektite-locations-map.png",
height = 8,
width = 7)
# draw the map of vietnam only
tektite_locations_clean_sf$vietnam <-
tektite_locations_clean_sf %>%
st_within(vietnam) %>%
lengths > 0
tektite_locations_clean_sf$vietnam <-
ifelse(tektite_locations_clean_sf$`Map Label` == "Cong Cai",
TRUE,
tektite_locations_clean_sf$vietnam)
tektite_locations_clean_sf_vietnam <-
tektite_locations_clean_sf %>%
filter(vietnam)
# prepare label text
label_data_vietnam <-
cbind(tektite_locations_clean_sf_vietnam,
st_coordinates(tektite_locations_clean_sf_vietnam$geometry))
tektite_locations_map_vietnam <-
ggplot() +
geom_sf(data = vietnam,
fill= "#faebd7") +
geom_sf(data = tektite_locations_clean_sf_vietnam,
colour = "red",
size = 1) +
geom_label_repel(data = label_data_vietnam,
aes(x = X,
y = Y,
label=Map.Label),
label.size = NA,
alpha = 0.8,
segment.alpha = 0.4,
label.padding=0.2,
na.rm=TRUE,
force = 40,
seed = 1234,
max.overlaps = 100) +
annotation_scale(location = "bl",
width_hint = 0.5) +
annotation_north_arrow(location = "bl",
which_north = "true",
pad_x = unit(0.25, "in"),
pad_y = unit(0.25, "in"),
style = north_arrow_fancy_orienteering) +
xlab("Longitude") +
ylab("Latitude") +
ggtitle("") +
theme(panel.background = element_rect(fill = "aliceblue"),
axis.text.y = element_text(size = 6),
axis.text.x = element_text(size = 6))
tektite_locations_map_vietnam
ggsave("tektite-locations-vietnam-map.png",
height = 8,
width = 4)
library(cowplot)
plot_grid(tektite_locations_map,
tektite_locations_map_vietnam,
ncol = 2,
align = "hv",
axis = "tb",
rel_widths = c(2, 1)
) +
theme(panel.background = element_rect(fill='white',
colour = "white"),
plot.background = element_rect(fill='white',
colour = "white"))
ggsave("tektite-locations-panel-map.png",
height = 6,
width = 10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment