Skip to content

Instantly share code, notes, and snippets.

@jsta
Last active May 31, 2021 13:03
Show Gist options
  • Save jsta/2dc9f7eb3057195a1db3eb9d4383e8f7 to your computer and use it in GitHub Desktop.
Save jsta/2dc9f7eb3057195a1db3eb9d4383e8f7 to your computer and use it in GitHub Desktop.
Mapping some lake data
# Mapping some lake data
# Reference: https://r-spatial.org/r/2018/10/25/ggplot2-sf.html
library(sf) # create geospatial layers
library(rnaturalearth) # basemap
library(ggplot2) # plotting
library(tidyr)
library(scales)
dt <- read.csv("lake_data_file.csv", stringsAsFactors = FALSE)
dt_sf <- st_as_sf(dt, coords = c("lon", "latitude"), crs = 4326)
bbox <- st_as_sfc(st_bbox(dt_sf))
states_all <- ne_states(
country = c(
"united states of america", "canada"
), returnclass = "sf")
states <- states_all[
unlist(lapply(st_intersects(states_all, bbox), function(x) length(x) > 0)), ]
ggplot() +
geom_sf(data = states, fill = "antiquewhite") +
geom_sf(
data = tidyr::drop_na(dt_sf, tp_mean), aes(color = tp_mean), size = 1) +
scale_color_viridis_c(trans = "log", name = "TP",
labels = scales::number_format(accuracy = 1)) +
coord_sf(
xlim = as.numeric(st_bbox(dt_sf))[c(1, 3)],
ylim = as.numeric(st_bbox(dt_sf))[c(2, 4)], expand = FALSE) +
theme_bw() +
theme(panel.background = element_rect(fill = "aliceblue"))
ggsave("test.png")
@jsta
Copy link
Author

jsta commented May 31, 2021

test

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