library(patchwork)
library(knitr)
library(lubridate)
library(terra)
library(sf)
::sf_use_s2(FALSE)
sflibrary(tidyverse)
Herpailurus yagouaroundi Data Generation
Data generation for Herpailurus yagouaroundi (blobs and rasters).
- R Libraries
- Basemaps
<- '+proj=laea +lon_0=-73.125 +lat_0=0 +datum=WGS84 +units=m +no_defs'
equalareaCRS <- st_read('data/latam.gpkg', layer = 'latam', quiet = T)
latam <- st_read('data/latam.gpkg', layer = 'countries', quiet = T)
countries <- st_read('data/latam.gpkg', layer = 'latam_land', quiet = T)
latam_land <- rast('data/latam_raster.tif', lyrs='latam')
latam_raster <- rast('data/latam_raster.tif', lyrs='countries')
latam_countries
<- sf::st_read('big_data/hyagouaroundi_IUCN.shp', quiet = T) %>% sf::st_transform(crs=equalareaCRS) hyagouaroundi_IUCN
- Dataset
<- read_csv('data/data_PO.csv')
data_carnivores_PO <- read_csv('data/data_PA.csv') data_carnivores_PA
Check data for each years on example species
Code
# Presence-only
%>% filter(species == 'Herpailurus yagouaroundi') %>% filter(year>=2000) %>%
data_carnivores_PO mutate(period=ifelse(year<=2013, 'first period: 2000-2013', 'second period: 2014-2021')) %>%
group_by(species, period) %>% count() %>% pivot_wider(names_from = 'period', values_from = n) %>% kable()
species | first period: 2000-2013 | second period: 2014-2021 |
---|---|---|
Herpailurus yagouaroundi | 216 | 588 |
Code
# Presence-absence
%>% filter(species == 'Herpailurus yagouaroundi' & presence ==1) %>%
data_carnivores_PA mutate(period=ifelse(year(date_start)<=2013, 'first period: 2000-2013', 'second period: 2014-2021')) %>%
group_by(species, period) %>% count() %>% pivot_wider(names_from = 'period', values_from = n) %>% kable()
species | first period: 2000-2013 | second period: 2014-2021 |
---|---|---|
Herpailurus yagouaroundi | 290 | 312 |
Presence-absence data
Create blobs
Blobs contain data on total effort, total temporal span, a list of the records IDs that are included for the blob and an area size of the combined areas.
Code
<- data_carnivores_PA %>%
data_PA_GIS filter(species == 'Herpailurus yagouaroundi') %>%
mutate(year=lubridate::year(date_end)) %>%
filter(year>=2000, year<=2021) %>%
as.data.frame() %>%
::st_as_sf(coords=c('decimalLongitude', 'decimalLatitude')) %>%
sfst_set_crs(4326)
<- st_transform(data_PA_GIS, crs=equalareaCRS)
PA
# create a blob for all sites
# blobs are definded as areas where some sampling was done over the entire period
<- ymd('2000-01-01')
periodStart <- ymd('2014-01-01')
periodEnd <- ymd('2020-12-31')
periodMax
<- PA %>%
PA_allsites mutate(start_date=as_date(ifelse(date_end-date_start<0, date_end, date_start)),
end_date=as_date(ifelse(date_end-date_start<0, date_start, date_end))) %>%
mutate(span=interval(date_start, date_end),
target= interval(periodEnd, today()),
period=ifelse(span %within% target, 'time2', 'time1')) %>%
distinct(independentLocation, period, .keep_all = T)
<- st_buffer(PA_allsites, sqrt(PA_allsites$area_m2/pi))
PA_allsites_buff
<- PA_allsites_buff %>% filter(period=='time1') %>%
blobs_time1 st_union() %>% st_cast('POLYGON') %>% st_sf('ID'= 1:length(.))
<- PA_allsites_buff %>% filter(period=='time2') %>%
blobs_time2 st_union() %>% st_cast('POLYGON') %>% st_sf('ID'= 1:length(.))
# to calculate the effort in days for each blob, we need to differenciate the two periods
<- PA %>%
PA_allsites_effort_and_time mutate(start_date=as_date(ifelse(date_end-date_start<0, date_end, date_start)),
end_date=as_date(ifelse(date_end-date_start<0, date_start, date_end))) %>%
mutate(span=interval(date_start, date_end),
target= interval(periodEnd, today()),
period=ifelse(span %within% target, 'time2', 'time1')) %>%
group_by(independentLocation, independentYearSpan, area_m2) %>%
summarise(effort=max(effort),
period=paste(unique(period)),
recordIDs=paste(unique(recordID), collapse = ';'),
maxEndDate=max(end_date),
minStartDate=min(start_date)) %>%
mutate(maxTemporalSpan= maxEndDate-minStartDate)
# to calculate the effort in days for each blob, we need to differenciate the two periods
<- st_join(blobs_time1,
blobs_efforttime_time1 %>% filter(period=='time1'),
PA_allsites_effort_and_time left=TRUE) %>%
group_by(ID) %>%
summarise(effort=sum(effort),
temporalSpan=sum(maxTemporalSpan),
recordIDs=paste(unique(recordIDs), collapse = ';')) %>%
mutate(blobArea=st_area(.))
<- st_join(blobs_time2,
blobs_efforttime_time2 %>% filter(period=='time2'),
PA_allsites_effort_and_time left=TRUE) %>%
group_by(ID) %>%
summarise(effort=sum(effort),
temporalSpan=sum(maxTemporalSpan),
recordIDs=paste(unique(recordIDs), collapse = ';')) %>%
mutate(blobArea=st_area(.))
Calculate PA blobs
Code
# function to create blobs for time1 and time2 for each species
<- function(PA, blobs, sp, time){
calculate_PA_sp_blobs <- ymd('2000-01-01')
periodStart <- ymd('2014-01-01')
periodEnd <- PA %>% filter(species==sp & presence==1) %>%
df_period filter(date_start>=periodStart) %>%
mutate(span=interval(date_start, date_end),
target=interval(periodEnd, today()),
period=ifelse(span %within% target, 'time2', 'time1')) %>%
filter(period==time) %>% select(species, presence)
<- st_join(blobs, df_period,
df_period_blobs left=TRUE, join = st_contains) %>%
group_by(ID) %>%
summarise(presence=max(presence),
temporalSpan=max(temporalSpan),
effort=max(effort),
blobArea=max(blobArea)) %>%
mutate(presence=ifelse(is.na(presence), 0, presence))
return(df_period_blobs)
}
# SPECIES
<- calculate_PA_sp_blobs(PA, blobs_efforttime_time1, 'Herpailurus yagouaroundi', 'time1')
PA_hyagouaroundi_time1_blobs <- calculate_PA_sp_blobs(PA, blobs_efforttime_time2, 'Herpailurus yagouaroundi', 'time2') PA_hyagouaroundi_time2_blobs
Plots
Code
<- rbind(PA_hyagouaroundi_time1_blobs %>% mutate(period='time1'),
data_PA_time1_time2 %>% mutate(period='time2'))
PA_hyagouaroundi_time2_blobs
<- ggplot() +
gg_PA_time1 geom_sf(data=countries, fill='#fafaf8', col='#bfc5c7', size=0.2) +
geom_sf(data=hyagouaroundi_IUCN, fill='grey60', alpha=0.5, col='grey90', size=0.1) +
geom_sf(data=data_PA_time1_time2 %>% filter(period=='time1') %>% st_buffer(20000),
aes(fill=factor(presence)), col=NA, show.legend = F) +
coord_sf(xlim = c(-3500000, 4100000), ylim = c(-4400000, 3000000)) + # st_bbox(hyagouaroundi_IUCN)
scale_fill_manual(values = c("#474545","#E31A1C"))+
scale_color_manual(values = c("#474545","#E31A1C")) +
labs(title='PA', subtitle='time1: 2000-2013', col='') +
theme_bw() +
theme(text=element_text(size = 14))
<- ggplot() +
gg_PA_time2 geom_sf(data=countries, fill='#fafaf8', col='#bfc5c7', size=0.2) +
geom_sf(data=hyagouaroundi_IUCN, fill='grey60', alpha=0.5, col='grey90', size=0.1) +
geom_sf(data=data_PA_time1_time2 %>% filter(period=='time2') %>% st_buffer(20000),
aes(fill=factor(presence), col=factor(presence)), show.legend = F) +
coord_sf(xlim = c(-3500000, 4100000), ylim = c(-4400000, 3000000)) + # st_bbox(hyagouaroundi_IUCN)
scale_fill_manual(values = c("#434445","#1F78B4"))+
scale_color_manual(values = c("#434445","#1F78B4")) +
labs(title='', subtitle='time2: 2014 to 2021', col='') +
theme_bw() +
theme(text=element_text(size = 14))
+ gg_PA_time2 gg_PA_time1
Presence-only data
Calculate PO rasters
Code
# rasterisation of the presence-only data
<- data_carnivores_PO %>%
data_PO_GIS filter(species == 'Herpailurus yagouaroundi') %>%
mutate(year=lubridate::year(eventDate)) %>%
filter(year>=2000, year<=2021) %>%
as.data.frame() %>%
::st_as_sf(coords=c('decimalLongitude', 'decimalLatitude')) %>%
sfst_set_crs(4326)
<- st_transform(data_PO_GIS, crs=equalareaCRS)
PO
# functions to create presence-only rasters for time1 and time2 for each species
<- function(PO, sp, raster, time){
calculate_PO_sp_period_raster <- 2000
periodStart <- 2014
periodEnd <- PO %>% filter(species==sp & !is.na(year)) %>%
df_period filter(year>=periodStart) %>%
mutate(period=ifelse(year>=periodEnd , 'time2', 'time1'),
occurrence=1) %>% filter(period==time)
$countryCode <- as.factor(df_period$countryCode)
df_period
<- terra::rasterize(x = vect(df_period),
df_period_raster y = raster,
field = 'occurrence',
fun = 'count', na.rm=T,
sum = T, background=0) %>% mask(., raster)
<- terra::rasterize(x = vect(df_period),
df_country_raster y = raster,
field = 'countryCode',
fun = first) %>% mask(., raster)
<- as.factor(df_country_raster)
df_country_raster
levels(df_country_raster) <- data.frame(
ID = 0:(length(levels(df_period$countryCode))-1),
countryCode = levels(df_period$countryCode)
)
names(df_period_raster) <- 'count'
names(df_country_raster) <- 'country'
<- c(df_period_raster, df_country_raster)
df_raster return(df_raster)
}
# Herpailurus yagouaroundi
<- calculate_PO_sp_period_raster(PO, 'Herpailurus yagouaroundi', latam_raster, 'time1')
PO_hyagouaroundi_time1_raster <- calculate_PO_sp_period_raster(PO, 'Herpailurus yagouaroundi', latam_raster, 'time2') PO_hyagouaroundi_time2_raster
Plots
Code
<- data_PO_GIS %>%
data_PO_time1_time2 mutate(period=ifelse(year>=2014 , 'time2', 'time1'), occurrence=1)
<- ggplot() +
gg_PO_time1 geom_sf(data=countries, fill='#fafaf8', col='#bfc5c7', size=0.2) +
geom_sf(data=hyagouaroundi_IUCN, fill='grey60', alpha=0.5, col='grey90', size=0.1) +
geom_sf(data=data_PO_time1_time2 %>% filter(period=='time1'), col="#E31A1C", size=0.5, show.legend = F) +
coord_sf(xlim = c(-3500000, 4100000), ylim = c(-4400000, 3000000)) + # st_bbox(hyagouaroundi_IUCN)
labs(title='PO time1: 2000-2013', col='') +
theme_bw() +
theme(text=element_text(size = 14))
<- ggplot() +
gg_PO_time2 geom_sf(data=countries, fill='#fafaf8', col='#bfc5c7', size=0.2) +
geom_sf(data=hyagouaroundi_IUCN, fill='grey60', alpha=0.5, col='grey90', size=0.1) +
geom_sf(data=data_PO_time1_time2 %>% filter(period=='time2'), col="#1F78B4", size=0.5, show.legend = F) +
coord_sf(xlim = c(-3500000, 4100000), ylim = c(-4400000, 3000000)) + # st_bbox(hyagouaroundi_IUCN)
labs(title='PO time2: 2014 to 2021', col='') +
theme_bw() +
theme(text=element_text(size = 14))
+ gg_PO_time2 gg_PO_time1
Save data
# blobs
saveRDS(PA_hyagouaroundi_time1_blobs, 'data/species_POPA_data/PA_hyagouaroundi_time1_blobs.rds')
saveRDS(PA_hyagouaroundi_time2_blobs, 'data/species_POPA_data/PA_hyagouaroundi_time2_blobs.rds')
# presence-only rasters
::writeRaster(PO_hyagouaroundi_time1_raster, 'data/species_POPA_data/PO_hyagouaroundi_time1_raster.tif', overwrite=TRUE)
terra::writeRaster(PO_hyagouaroundi_time2_raster, 'data/species_POPA_data/PO_hyagouaroundi_time2_raster.tif', overwrite=TRUE)
terra
saveRDS(data_PA_time1_time2, 'data/species_POPA_data/data_hyagouaroundi_PA.rds')
saveRDS(data_PO_time1_time2, 'data/species_POPA_data/data_hyagouaroundi_PO.rds')