-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path05_climate-change-analysis.Rmd
448 lines (382 loc) · 10.6 KB
/
05_climate-change-analysis.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
---
editor_options:
chunk_output_type: console
---
# Analyzing temperature and precipitation change over time
In this script, we will process the reconstructed climate data and analyze trends in climate over time
## Load necessary libraries
```{r}
library(terra)
library(sf)
library(stringi)
library(glue)
library(purrr)
library(dplyr)
library(tidyr)
library(tibble)
# for plotting
library(viridis)
library(colorspace)
library(tmap)
library(scales)
library(ggplot2)
library(patchwork)
# for GLMMS
library(mgcv)
```
```{r}
# load nilgiris
hills <- st_read("data/landcover/Nil_Ana_Pal.shp")
# get reconstructions
climate_reconstructed <- list.files(
"results/climate",
pattern = "raster_reconstructed",
full.names = T
) |>
lapply(rast)
# get differences with respect to first reconstruction
climate_change <- lapply(climate_reconstructed, function(le) {
le <- le / le[[1]]
le <- le[[-1]]
le <- le - 1
terra::crop(
le, vect(hills)
)
})
```
## Plot change in climate as percentages.
```{r}
ppt_dry_change <- tm_shape(climate_change[[1]]) +
tm_raster(
palette = colorspace::diverging_hcl(
31,
palette = "Blue-Yellow",
rev = T
),
style = "cont", midpoint = 0,
title = "% difference, dry season ppt.",
legend.is.portrait = F
) +
tm_shape(hills) +
tm_borders() +
tm_legend(
legend.outside = T,
legend.outside.position = "bottom",
legend.outside.size = .1
)
tmap_save(
ppt_dry_change,
filename = "figs/fig_ppt_dry_change.png"
)
```

```{r}
ppt_wet_change <- tm_shape(climate_change[[2]]) +
tm_raster(
palette = colorspace::diverging_hcl(
31,
palette = "Blue-Yellow",
rev = T
),
style = "cont", midpoint = 0,
title = "% difference, wet season ppt.",
legend.is.portrait = F
) +
tm_shape(hills) +
tm_borders() +
tm_legend(
legend.outside = T,
legend.outside.position = "bottom",
legend.outside.size = .1
)
tmap_save(
ppt_wet_change,
filename = "figs/fig_ppt_wet_change.png"
)
```

```{r}
temp_dry_change <- tm_shape(climate_change[[3]]) +
tm_raster(
palette = colorspace::diverging_hcl(
31,
rev = F
),
style = "cont", midpoint = 0,
title = "% difference, dry season temp.",
legend.is.portrait = F
) +
tm_shape(hills) +
tm_borders() +
tm_legend(
legend.outside = T,
legend.outside.position = "bottom",
legend.outside.size = .1
)
tmap_save(
temp_dry_change,
filename = "figs/fig_temp_dry_change.png"
)
```

```{r}
temp_wet_change <- tm_shape(climate_change[[4]]) +
tm_raster(
palette = colorspace::diverging_hcl(
31,
rev = F
),
style = "cont", midpoint = 0,
title = "% difference, wet season temp.",
legend.is.portrait = F
) +
tm_shape(hills) +
tm_borders() +
tm_legend(
legend.outside = T,
legend.outside.position = "bottom",
legend.outside.size = .1
)
tmap_save(
temp_wet_change,
filename = "figs/fig_temp_wet_change.png"
)
```

## Load list of resurvey locations/historical sites of occurrence
```{r}
# load list of resurvey locations and add elevation as a variable
# remove the modern resurvey locations only (ie. ASFQ)
sites <- read.csv("data/list-of-resurvey-locations.csv")
# set missing historical site code to NA
sites <- mutate(sites,
historical_site_code = if_else(
historical_site_code == "", NA_character_, historical_site_code
),
site_name = if_else(
site_name == "", NA_character_, site_name
)
)
# remove sites with historical site code missing
sites <- filter(sites, !is.na(historical_site_code))
# convert to sf object and transform
sites <- st_as_sf(sites, coords = c("longitude", "latitude")) %>%
`st_crs<-`(4326) %>%
st_transform(32643)
# add an outline of the nilgiris shapefile (contains areas below 1400 m)
nil <- st_read("data/landcover/Nil_Ana_Pal.shp")
nil <- nil[2, ]
nil <- st_transform(nil, 32643)
buffer <- st_buffer(nil, 3e4) %>%
st_transform(4326)
# add the elevation raster
alt <- rast("data/elevation/alt/")
# this layer is not added to github as a result of its large size and can be downloaded from SRTM (Farr et al. (2007))
alt.hills <- terra::crop(alt, as(buffer, "Spatial"))
rm(alt)
gc()
# get slope and aspect
slopeData <- terrain(x = alt.hills, v = c("slope", "aspect"))
# use c() on terra object to combine as a stack
elevData <- c(alt.hills, slopeData)
rm(alt.hills)
gc()
```
## Load the climate rasters
Note that there are four rasters: temperature and precipitation for both dry season and rainy season respectively. The dry season refers to the months of December to May while the rainy season refers to the months of June to November. Each raster corresponds to mean monthly value across a single decade (1870-1890 and so on). In other words, ppt_dry_1870 would refer to the mean monthly precipitation between December to May for the time-period 1870 to 1880.
```{r}
# use previously loaded climate reconstruction data
climate_reconstructed
# crop each reconstruction to the previously prepared buffer
# and transform/assign the crs of the site data
climate_reconst_nilgiris <- map(
climate_reconstructed, function(cl) {
cl <- terra::project(cl, crs(sites))
}
)
# extract the above data for each of the resurvye locations
# use explicit namespacing of extract from terra, i.e., terra::extract
climate_reconst_sites <- lapply(climate_reconst_nilgiris, function(cl) {
terra::extract(cl, as(sites, "SpatVector"))
})
# extract elevation, aspect, slope at sites
# first transform elevation data to UTM 43N
terrain_nilgiris <- terra::project(elevData, crs(sites))
terrain_sites <- terra::extract(terrain_nilgiris, as(sites, "SpatVector"))
```
## Combine site climate and terrain data
```{r}
# merge all extracted climate data at sites
climate_reconst_sites <- reduce(climate_reconst_sites, left_join)
# now convert to long format
climate_reconst_sites <- pivot_longer(
climate_reconst_sites,
cols = !ID,
names_to = "climate_measure"
)
# split season and year column
climate_reconst_sites <- separate_wider_regex(
climate_reconst_sites,
cols = climate_measure,
patterns = c(
climate_measure = ".*", "_",
season = "dry|rainy", "_",
year = "\\d{4}"
)
)
# join with terrain data
clim_terrain_sites <- left_join(
climate_reconst_sites, terrain_sites
)
# add site data - first drop
sites <- st_drop_geometry(sites) %>%
mutate(ID = seq(nrow(sites))) %>%
as_tibble()
# join with site data
clim_terrain_sites <- left_join(
clim_terrain_sites,
sites
)
# make factors numerics
clim_terrain_sites <- mutate(
clim_terrain_sites,
year = as.numeric(year),
alt = as.numeric(as.character(alt))
)
# save climate at sites
readr::write_csv(
clim_terrain_sites,
"data/results/clim_terrain_sites.csv"
)
```
## Plot all reconstructed climate over time colouring by elevation and grouping by site. Each line represents one modern site code.
```{r}
ggplot(
clim_terrain_sites,
aes(
x = year, y = value,
col = alt, group = modern_site_code
)
) +
geom_line(
position = position_jitter(width = 0.2)
) +
scale_colour_binned_sequential(
palette = "Batlow"
) +
facet_grid(
rows = vars(climate_measure),
cols = vars(season),
scales = "free_y",
labeller = label_both
) +
labs(
colour = "Elev. (m)"
)
ggsave(
filename = "figs/fig_clim_reconst_site_elev.png",
dpi = 150
)
```

## Alternative figure for temperature change over time
```{r}
# get the mean proportional change in climate variables at sites
# binned by elevation (200m bins), separated by season
clim_prop_change <- clim_terrain_sites %>%
mutate(
elev_bin = plyr::round_any(alt, 200)
) %>%
group_by(
climate_measure, season, modern_site_code, elev_bin
) %>%
mutate(
prop_change = (value - first(value)) / first(value)
)
# nest by climate variable for more visible colour scales
clim_prop_change <- ungroup(clim_prop_change) %>%
nest(.by = "climate_measure")
# function for plotting
fn_plot <- function(df) {
ggplot(df) +
stat_summary_2d(
aes(as.factor(year), as.factor(elev_bin), z = prop_change),
col = "grey", linewidth = 0.1
) +
facet_grid(
cols = vars(season),
labeller = labeller(
season = c(
dry = "Dry", rainy = "Rainy"
)
)
) +
scale_fill_continuous_diverging(
palette = "Blue-Red 2",
labels = function(x) {
scales::percent(x)
}
) +
theme_bw() +
theme(
legend.position = "top",
legend.key.height = unit(3, "mm"),
legend.key.width = unit(20, "mm"),
axis.text.x = element_text(
angle = 45, hjust = 0.75
)
) +
labs(
x = "Decade", y = "Elevation bin (m)",
fill = "Difference (%)"
)
}
clim_prop_change <- mutate(
clim_prop_change,
plots = map(data, fn_plot)
)
# save plots
walk2(
clim_prop_change$plots,
clim_prop_change$climate_measure,
function(x, y) {
ggsave(
x,
filename = glue("figs/fig_elev_bin_{y}_change.png"),
height = 4, width = 7,
dpi = 300
)
}
)
```

## Test for climate change over time
Run a GLMM for the value over time, with measure, season, historical site code, and modern land cover type as random effects.
All terrain covariates are fixed effects, and time is the main fixed effect of interest.
Save/report this model fit.
```{r}
# set site code and landcover as factors
clim_terrain_sites$historical_site_code <- as.factor(clim_terrain_sites$historical_site_code)
clim_terrain_sites$modern_landCover_type <- as.factor(clim_terrain_sites$modern_landCover_type)
# split by measure
clim_terrain_sites <- nest(
clim_terrain_sites,
.by = climate_measure
)
# run model
clim_terrain_sites <- mutate(
clim_terrain_sites,
model_fit = map(
data,
function(df) {
gam(
value ~ year + alt + slope + aspect + season +
s(historical_site_code, bs = "re") +
s(modern_landCover_type, bs = "re"),
data = df
)
}
)
)
map(clim_terrain_sites$model_fit, summary)
```