
Photo by Hari Nandakumar on Unsplash
Source Materials
The following materials are modified from Leander Anderegg’s niche modeling exercise.
1. Background
This week, you will practice function making with rasters and create a generalizable workflow to determine the climate envelope (or the climate where a species currently lives) of two endemic species to California:
- Tule elk (Cervus canadensis nannodes)
- Yellow-billed magpie (Pica nutalli)
2. Get Started
- Create an
.Rprojas your version controlled project for Week 8 - Create a Quarto document inside your project
- Download this data folder from Google Drive and move it inside your project
- Load all necessary packages and read spatial objects
library(tidyverse)
library(sf)
library(terra)
library(dismo)
library(tmap)magpie <- read_csv(here::here("data", "magpie_obvs.csv"))
tule_elk <- read_csv(here::here("data", "tule_elk_obvs.csv"))
bioclim_dir <- here::here("data", "climate", "wc2.1_2.5m")
bioclim <- list.files(bioclim_dir, pattern = glob2rx("*.tif$"), full.names = TRUE)
bioclim_sort <- bioclim[
# Sort filepaths based on numeric suffix
order(
# Extract numeric suffix of filenames and convert to numeric
as.numeric(gsub(".*_(\\d+)\\.tif$", "\\1", bioclim)))]
bioclim_rast <- rast(bioclim_sort)3. Your Task
Now, to meet this week’s learning objectives, your task:
- Update the
bioclim_rastlayer names to match the Bioclimatic variables usingnames() - Use
st_as_sfto convert and find the geographic extent of species occurrence datamagpieusingst_bbox() - Crop
bioclim_rastto match the extent of species occurrence datamagpie - Extract points from the cropped
bioclim_rastfor each occurrence inmagpie - Create “background values” by using
dismo::randomPoints()to generate random sample points from the croppedbioclim_rast - Extract points from
bioclim_rastfor each random sample point generated in Step #5 - Plot species climate niche and background climate (temperature vs. precipitation)
- Modify steps 2-7 into a generalizable workflow for other species occurrence data
- Try your new function with species occurrence data
tule_elk