library(tidyverse)
library(sf)
library(terra)
library(dismo)
library(tmap)
library(patchwork)
<- read_csv(here::here("data", "magpie_obvs.csv"))
magpie <- read_csv(here::here("data", "tule_elk_obvs.csv"))
tule_elk
<- here::here("data", "climate", "wc2.1_2.5m")
bioclim_dir <- list.files(bioclim_dir, pattern = glob2rx("*.tif$"), full.names = TRUE)
bioclim <- bioclim[
bioclim_sort # Sort filepaths based on numeric suffix
order(
# Extract numeric suffix of filenames and convert to numeric
as.numeric(gsub(".*_(\\d+)\\.tif$", "\\1", bioclim)))]
<- rast(bioclim_sort) bioclim_rast
Name Raster Layers
<- c("annualMeanTemp", "meanDiurnalRange", "isothermality", "tempSeasonality", "maxTempWarmMonth", "maxTempColdMonth", "tempAnnualRange", "meanTempWetQ", "meanTempDryQ", "meanTempWarmQ", "meanTempColdQ", "annualPrecip", "precipWetMonth", "precipDryMonth", "precipSeasonality", "precipWetQ", "precipDryQ", "precipWarmQ", "precipColdQ")
variables names(bioclim_rast) <- variables
Find Geographic Extent of Species Occurences
<- magpie %>%
magpie_sf rename(long = longitude,
lat = latitude) %>%
drop_na(long, lat) %>%
st_as_sf(coords = c("long", "lat"), crs = 4326)
# Obtain geographic extent/bounding box of the species occurrences
<- st_bbox(magpie_sf) magpie_bbox
Crop Raster and Extract Points for Species Occurences
# Crop raster to match the geographic extent of the species occurrences
<- crop(bioclim_rast, magpie_bbox) bioclim_crop
# Extract points from raster for all species occurrences
<- as_tibble(extract(bioclim_crop, magpie_sf)) bioClim_pts
Crop Raster and Extract Random Points
set.seed(42) # for reproducibility
# Generate random sample points from raster
<- dismo::randomPoints(mask = raster(bioclim_crop[["annualMeanTemp"]]),
random_pts n = nrow(magpie) * 2,
ext = magpie_bbox)
# Extract points from raster for random sample points
<- as_tibble(extract(bioclim_crop, random_pts)) bioClim_random_pts
View Map
<- tm_shape(raster(bioclim_crop[["annualPrecip"]])) +
map_1 tm_raster(palette = "Blues", title = "Annual Precipitation") +
tm_shape(magpie_sf) +
tm_dots(col = "#3a5a40", size = 0.15) +
tm_layout(legend.position = c("left", "bottom"),
legend.bg.color = "white")
<- tm_shape(raster(bioclim_crop[["annualMeanTemp"]])) +
map_2 tm_raster(palette = "-RdYlBu", title = "Annual Mean Temp") +
tm_shape(magpie_sf) +
tm_dots(col = "#3a5a40", size = 0.15) +
tm_layout(legend.position = c("left", "bottom"),
legend.bg.color = "white")
tmap_arrange(map_1, map_2)
View Plot
<- ggplot(data = bioClim_pts, aes(x = annualPrecip, y = annualMeanTemp)) +
plot_1 geom_point(shape = 16, color = "#3a5a40") +
labs(x = "Annual Precipitation",
y = "Annual Mean Temperature",
title = "Species Climate Niche") +
theme_bw()
<- ggplot(data = bioClim_random_pts, aes(x = annualPrecip, y = annualMeanTemp)) +
plot_2 geom_point(shape = 16) +
labs(x = "Annual Precipitation",
y = element_blank(),
title = "Background Climate") +
theme_bw()
+ plot_2 plot_1
Create Generalizable Workflow
<- function(clim_rast, clim_var1, clim_var2, occurences, species_name){
climate_envelope
<- species_name %>%
species_name str_to_lower() %>%
str_replace_all(" ", "_")
<- occurences %>%
occurences_sf rename(long = longitude,
lat = latitude) %>%
drop_na(long) %>%
st_as_sf(coords = c("long", "lat"), crs = 4326)
<- st_bbox(occurences_sf)
occurences_bbox
<- crop(clim_rast, occurences_bbox)
clim_crop
<- as_tibble(extract(clim_crop, occurences_sf))
clim_pts
<- randomPoints(mask = raster(clim_rast[[clim_var1]]),
random_pts n = nrow(occurences) * 2,
ext = occurences_bbox)
<- as_tibble(extract(clim_crop, random_pts))
clim_random_pts
<- tm_shape(raster(clim_crop[[clim_var1]])) +
map_1 tm_raster(palette = "Blues") +
tm_shape(occurences_sf) +
tm_dots(col = "#3a5a40", size = 0.15) +
tm_layout(legend.position = c("left", "bottom"),
legend.bg.color = "white")
<- tm_shape(raster(clim_crop[[clim_var2]])) +
map_2 tm_raster(palette = "-RdYlBu") +
tm_shape(occurences_sf) +
tm_dots(col = "#3a5a40", size = 0.15) +
tm_layout(legend.position = c("left", "bottom"),
legend.bg.color = "white")
<- ggplot(data = clim_pts, aes_string(x = clim_var1, y = clim_var2)) +
plot_1 geom_point(shape = 16, color = "#3a5a40") +
labs(title = "Species Climate Niche") +
theme_bw()
<- ggplot(data = clim_random_pts, aes_string(x = clim_var1, y = clim_var2)) +
plot_2 geom_point(shape = 16) +
labs(y = element_blank(),
title = "Background Climate") +
theme_bw()
assign(paste0(species_name, "_map_1"), map_1, envir = .GlobalEnv)
assign(paste0(species_name, "_map_2"), map_2, envir = .GlobalEnv)
assign(paste0(species_name, "_plot_1"), plot_1, envir = .GlobalEnv)
assign(paste0(species_name, "_plot_2"), plot_2, envir = .GlobalEnv)
}
climate_envelope(clim_rast = bioclim_rast, clim_var1 = "annualPrecip", clim_var2 = "annualMeanTemp", occurences = tule_elk, species_name = "Tule Elk")
tule_elk_map_1
tule_elk_map_2
tule_elk_plot_1
tule_elk_plot_2