Exploratory analyses

Author

Bioshifts

Code
rm(list=ls())
gc()
          used (Mb) gc trigger (Mb) max used (Mb)
Ncells  587057 31.4    1346851   72   660908 35.3
Vcells 1066839  8.2    8388608   64  1800310 13.8
Code
library(dplyr)
library(tidyverse)
library(tidyr)
library(ggplot2)
library(knitr)
library(pbapply)
library(data.table)

1 Source code

Code
try(source(here::here("R/settings.R")))
Error in eval(ei, envir) : object 'realm' not found

2 Load data

Code
realm <- "Mar"

# Bioshifts data
sp_bioshifts <- read.csv(here::here("Output",paste(realm,"sps_bioshifts.csv")))
# SDM outputs data
sp_SDM_out <- read.csv(here::here("Output",paste(realm,"sp_SDM_out.csv")))
# range position
sp_range_pos <- read.csv(here::here("Output",paste(realm,"sp_range_pos.csv")))
sp_range_pos <- sp_range_pos[,-which(names(sp_range_pos) %in% c("START","END", "BG"))]
# Exposure
sp_exposure <- read.csv(here::here("Output",paste(realm,"sp_exposure.csv")))
sp_exposure_SA <- sp_exposure[,-which(names(sp_exposure) == "exp_BG")]
sp_exposure_BG <- sp_exposure[,-which(names(sp_exposure) == "exp_SA")]
sp_exposure_BG <- sp_exposure_BG %>% spread(bio, exp_BG)
names(sp_exposure_BG)[6:9] <- paste(names(sp_exposure_BG)[6:9],
                                    "BG", sep = "_")
sp_exposure_SA <- sp_exposure_SA %>% spread(bio, exp_SA)
names(sp_exposure_SA)[6:9] <- paste(names(sp_exposure_SA)[6:9],
                                    "SA", sep = "_")
sp_exposure <- cbind(sp_exposure_BG, sp_exposure_SA[,6:9])
sp_exposure <- sp_exposure[,-which(names(sp_exposure) %in% c("START","END"))]

# SDM shifts
sp_SDM_shift <- read.csv(here::here("Output",paste(realm,"sp_SDM_shift.csv")))
sp_SDM_shift <- sp_SDM_shift %>% filter(!quant %in% c("0%","100%","mean"))
sp_SDM_shift$time_period <- paste(round(sp_SDM_shift$START),
                                  round(sp_SDM_shift$END),
                                  sep = "-")

3 Merge SDM results with bioshifts

3.1 Leading edge

Code
LE <- sp_bioshifts %>% filter(Param == "LE")

# add SDM output data
sp_SDM_out_CV <- sp_SDM_out %>% filter(Mod == "CV")
LE <- left_join(LE, sp_SDM_out_CV, by = join_by(New_name == Species))
LE$time_period <- paste(round(LE$START), round(LE$END),sep="-")

# add range position
LE <- left_join(LE, sp_range_pos, 
                by = join_by(New_name == Species,
                             ID == ID, 
                             time_period == time_period,
                             Type == type))

# add Exposure
LE <- left_join(LE, sp_exposure, 
                by = join_by(New_name == Species,
                             ID == ID, 
                             time_period == Time_periods))

# add SDM shift
tmp <- sp_SDM_shift %>% 
    filter(quant %in% c("5%","25%","50%","75%","95%","99%")) %>%
    select(c("quant","shift_BG","shift_SA","ID","Species", "Type", "time_period"))
tmp$quant <- gsub("%","",tmp$quant)
tmp_BG <- tmp[,-3]
tmp_BG <- tmp_BG %>% spread(quant, value = shift_BG)
names(tmp_BG)[5:9] <- paste0("BG_", names(tmp_BG)[5:9])
tmp_SA <- tmp[,-2]
tmp_SA <- tmp_SA %>% spread(quant, value = shift_SA)
names(tmp_SA)[5:9] <- paste0("SA_", names(tmp_SA)[5:9])
tmp <- cbind(tmp_BG,tmp_SA[,5:9])

LE <- left_join(LE, tmp, 
                by = join_by(New_name == Species,
                             ID == ID, 
                             time_period == time_period,
                             Type == Type))

3.2 Trailing edge

Code
TE <- sp_bioshifts %>% filter(Param == "TE")

# add SDM output data
sp_SDM_out_CV <- sp_SDM_out %>% filter(Mod == "CV")
TE <- left_join(TE, sp_SDM_out_CV, by = join_by(New_name == Species))
TE$time_period <- paste(round(TE$START), round(TE$END),sep="-")

# add range position
TE <- left_join(TE, sp_range_pos, 
                by = join_by(New_name == Species,
                             ID == ID, 
                             time_period == time_period,
                             Type == type))

# add Exposure
TE <- left_join(TE, sp_exposure, 
                by = join_by(New_name == Species,
                             ID == ID, 
                             time_period == Time_periods))

# add SDM shift
tmp <- sp_SDM_shift %>% 
    filter(quant %in% c("5%","25%","50%","75%","95%","99%")) %>%
    select(c("quant","shift_BG","shift_SA","ID","Species", "Type", "time_period"))
tmp$quant <- gsub("%","",tmp$quant)
tmp_BG <- tmp[,-3]
tmp_BG <- tmp_BG %>% spread(quant, value = shift_BG)
names(tmp_BG)[5:9] <- paste0("BG_", names(tmp_BG)[5:9])
tmp_SA <- tmp[,-2]
tmp_SA <- tmp_SA %>% spread(quant, value = shift_SA)
names(tmp_SA)[5:9] <- paste0("SA_", names(tmp_SA)[5:9])
tmp <- cbind(tmp_BG,tmp_SA[,5:9])

TE <- left_join(TE, tmp, 
                by = join_by(New_name == Species,
                             ID == ID, 
                             time_period == time_period,
                             Type == Type))

3.3 Optimum

Code
O <- sp_bioshifts %>% filter(Param == "O")

# add SDM output data
sp_SDM_out_CV <- sp_SDM_out %>% filter(Mod == "CV")
O <- left_join(O, sp_SDM_out_CV, by = join_by(New_name == Species))
O$time_period <- paste(round(O$START), round(O$END),sep="-")

# add range position
O <- left_join(O, sp_range_pos, 
               by = join_by(New_name == Species,
                            ID == ID, 
                            time_period == time_period,
                            Type == type))

# add Exposure
O <- left_join(O, sp_exposure, 
               by = join_by(New_name == Species,
                            ID == ID, 
                            time_period == Time_periods))

# add SDM shift
tmp <- sp_SDM_shift %>% 
    filter(quant %in% c("5%","25%","50%","75%","95%","99%")) %>%
    select(c("quant","shift_BG","shift_SA","ID","Species", "Type", "time_period"))
tmp$quant <- gsub("%","",tmp$quant)
tmp_BG <- tmp[,-3]
tmp_BG <- tmp_BG %>% spread(quant, value = shift_BG)
names(tmp_BG)[5:9] <- paste0("BG_", names(tmp_BG)[5:9])
tmp_SA <- tmp[,-2]
tmp_SA <- tmp_SA %>% spread(quant, value = shift_SA)
names(tmp_SA)[5:9] <- paste0("SA_", names(tmp_SA)[5:9])
tmp <- cbind(tmp_BG,tmp_SA[,5:9])

O <- left_join(O, tmp, 
               by = join_by(New_name == Species,
                            ID == ID, 
                            time_period == time_period,
                            Type == Type))

There are 173 shifts at the leading edge, 66 shifts at the trailing edge, and 286 at the optimum (center) of the species range.

3.4 Group

Code
SDM_shifts <- rbind(LE,TE,O)

4 Explore relationships

4.1 N shifts per parameter

Code
tmp <- data.frame(table(SDM_shifts$Param))

ggplot(tmp, aes(x = Var1, y = Freq))+
    geom_col()+
    theme_classic()+
    geom_text(aes(label = Freq), vjust = -0.5)+
    xlab("N shifts")

6 Is range shift sensitive to climate exposure?

6.1 Exposure at the study area

Code
tmp_e_SA <-  SDM_shifts %>% 
    select(SHIFT, Param, New_name, Type, ID, time_period,
           SST_max_SA, SST_mean_SA, SST_min_SA, SST_sd_SA) 

tmp_e_SA <- tmp_e_SA %>% 
    gather(type, Exposure, -c(SHIFT,Param,New_name,Type,ID,time_period))  
tmp_e_SA$type <- factor(tmp_e_SA$type, 
                        levels = c("SST_max_SA", "SST_mean_SA", "SST_min_SA", "SST_sd_SA"))

ggplot(tmp_e_SA, aes(x = Exposure, y = SHIFT))+
    geom_point()+
    xlab("Exposure")+
    ylab("Observed shift (km/yr)")+
    theme_classic() +
    geom_smooth(method = "gam")+
    facet_wrap(.~type, nrow = 1)

6.1.1 Per parameter

Code
ggplot(tmp_e_SA, aes(x = Exposure, y = SHIFT))+
    geom_point()+
    xlab("Exposure")+
    ylab("Observed shift (km/yr)")+
    theme_classic() +
    geom_smooth(method = "gam")+
    facet_wrap(Param~type, nrow = 3)

6.2 Exposure at the background area

Code
tmp_e_BG <-  SDM_shifts %>% 
    select(SHIFT, Param, New_name, Type, ID, time_period,
           SST_max_BG, SST_mean_BG, SST_min_BG, SST_sd_BG) 

tmp_e_BG <- tmp_e_BG %>% 
    gather(type, Exposure, -c(SHIFT,Param,New_name,Type,ID,time_period))  
tmp_e_BG$type <- factor(tmp_e_BG$type, 
                        levels = c("SST_max_BG", "SST_mean_BG", "SST_min_BG", "SST_sd_BG"))

ggplot(tmp_e_BG, aes(x = Exposure, y = SHIFT))+
    geom_point()+
    xlab("Exposure")+
    ylab("Observed shift (km/yr)")+
    theme_classic() +
    geom_smooth(method = "gam")+
    facet_wrap(.~type, nrow = 1)

6.2.1 Per parameter

Code
ggplot(tmp_e_BG, aes(x = Exposure, y = SHIFT))+
    geom_point()+
    xlab("Exposure")+
    ylab("Observed shift (km/yr)")+
    theme_classic() +
    geom_smooth(method = "gam")+
    facet_wrap(Param~type, nrow = 3)
`geom_smooth()` using formula 'y ~ s(x, bs = "cs")'
Warning: Removed 558 rows containing non-finite values (stat_smooth).
Warning: Removed 558 rows containing missing values (geom_point).