Linking species occurrences and evolution to environmental variables and quantifying species โnicheโ and its evolution is essential for many biogeographic questions. We will use the temperature range of species to reconstruct the evolution of temperature mean and variance using BITE tomorrow.
After this exercise, you will be able to extract environmental data from for geographic coordinates.
raster
, read_csv
, extract
)Once you downloaded the environmental data, it is straightforward to extract values for geographic coordinates. The raster
package will help you to handle environmental data in R and extract the values for your coordinates.
library(raster)
library(tidyverse)
occ <- read_csv("example_data/input/occurrence_records_clean.csv")
env <- raster("example_data/input/wc2.0_bio_5m_01.tif")
# Generate a spatial object with the coordinates
pts <- occ %>% select(decimalLongitude, decimalLatitude) %>% as.matrix()
# Extract the environmental values
ext <- raster::extract(env, pts) %>% enframe(name = NULL, value = "MAT")
# Bind coordinates and environmental variables
out <- bind_cols(occ, ext)
write_csv(out, "output/records+environment.csv")