Skip to content

Instantly share code, notes, and snippets.

@jsta
Created September 24, 2021 22:01
Show Gist options
  • Save jsta/dae282046b8ec1231d970948924f715d to your computer and use it in GitHub Desktop.
Save jsta/dae282046b8ec1231d970948924f715d to your computer and use it in GitHub Desktop.
GLEON 2021 R Spatial Workshop
library(sf)
library(mapview)
erie_outline <- st_read("data/erie_outline.shp")
st_geometry_type(erie_outline)
st_crs(erie_outline)
st_bbox(erie_outline)
library(ggplot2)
# install.packages("ggplot2")
plot(erie_outline$geometry)
ggplot() +
geom_sf(data = erie_outline, fill="cyan", size=1.5) +
ggtitle("Lake Erie Outline")
mapview(erie_outline)
st_write(erie_outline, "data/erie_outline.gpkg")
erie_outline
erie_zones <- st_read("data/erie_zones.shp")
erie_zones
View(erie_zones)
names(erie_zones)
head(erie_zones)
zone_5 <- dplyr::filter(erie_zones, MGMTUNIT == "MU5")
mapview(erie_zones) + mapview(zone_5)
ggplot() +
geom_sf(data = zone_5)
mapview(erie_zones, zcol = "MGMTUNIT")
ggplot() +
geom_sf(data = erie_zones, aes(color = MGMTUNIT)) +
ggtitle("Managment Units", subtitle = "colored by zone") +
labs(color = "Unit ID")
ggplot() +
geom_sf(data = erie_zones, aes(color = MGMTUNIT, size = MGMTUNIT)) +
scale_size_manual(values = c(1, 2, 3, 4, 1))
fish_tracks <- read.csv("data/fish_tracks.csv")
View(fish_tracks)
st_crs(erie_outline)
fish_locations <- st_as_sf(fish_tracks, coords = c("X", "Y"), crs = st_crs(4326))
st_crs(fish_locations)
mapview(fish_locations)
table(fish_locations$animal_id)
ggplot() +
geom_sf(data = fish_locations)
mapview(erie_outline) + mapview(fish_locations, zcol = "animal_id")
library(raster)
library(rgdal)
GDALinfo("data/erie_bathy.tif")
erie_bathy <- raster("data/erie_bathy.tif")
summary(erie_bathy)
mapview(erie_bathy)
plot(erie_bathy)
minValue(erie_bathy)
maxValue(erie_bathy)
erie_bathy_df <- as.data.frame(erie_bathy, xy = TRUE)
View(erie_bathy)
ggplot() +
geom_raster(data = erie_bathy_df, aes(x = x, y = y, fill = erie_bathy)) +
scale_fill_viridis_c(na.value = "deeppink")
ggplot() +
geom_histogram(data = erie_bathy_df, aes(erie_bathy))
fish_tracks_bathy <- extract(x = erie_bathy,
y = as(fish_locations, "Spatial"), df = TRUE)
View(fish_tracks_bathy)
summary(fish_tracks_bathy$erie_bathy)
ggplot() +
geom_histogram(data = fish_tracks_bathy, aes(x = erie_bathy))
erie_bathy_cropped <- crop(x = erie_bathy, y = as(erie_outline, "Spatial"))
mapview(erie_bathy) + mapview(erie_bathy_cropped)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment