-
Notifications
You must be signed in to change notification settings - Fork 6
/
fig_4_biodiversity_by_hab_and_mpa.Rmd
331 lines (264 loc) · 11.9 KB
/
fig_4_biodiversity_by_hab_and_mpa.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
---
title: 'habitats and biodiversity risk'
author: "*Compiled on `r date()` by `r Sys.info()['user']`*"
output:
html_document:
code_folding: hide
toc: true
toc_depth: 3
toc_float: yes
number_sections: true
theme: cerulean
highlight: haddock
includes:
in_header: '~/github/src/templates/ohara_hdr.html'
pdf_document:
toc: true
---
``` {r setup, echo = TRUE, message = FALSE, warning = FALSE}
knitr::opts_chunk$set(fig.width = 6, fig.height = 4, fig.path = 'figs/',
echo = TRUE, message = FALSE, warning = FALSE)
library(raster)
source('https://raw.githubusercontent.com/oharac/src/master/R/common.R')
### includes library(tidyverse); library(stringr);
### dir_M points to ohi directory on Mazu; dir_O points to home dir on Mazu
dir_git <- here()
source(file.path(dir_git, '_setup/common_fxns.R'))
```
# Summary
Compare biodiversity risk within various marine habitats
# Data sources
* IUCN species API: IUCN. (2019). The IUCN Red List of Threatened Species. Version 2019-2.
* IUCN species shapefiles: IUCN. (2019). The IUCN Red List of Threatened Species. Version 2019-2. Retrieved August 2019, from http://www.iucnredlist.org
* BirdLife International shapefiles: BirdLife International and Handbook of the Birds of the World. (2018). Bird species distribution maps of the world. Version 7.0. Available at http://datazone.birdlife.org/species/requestdis
* World Database on Protected Areas: IUCN, & UNEP-WCMC. (2018, June). The World Database on Protected Areas (WDPA). Retrieved June 9, 2018, from Cambridge, UK: UNEP-WCMC website: www.protectedplanet.net
* Marine Ecoregions of the World: Spalding, M. D., Fox, H. E., Allen, G. R., Davidson, N., Ferdaña, Z. A., Finlayson, M. A. X., … others. (2007). Marine ecoregions of the world: A bioregionalization of coastal and shelf areas. BioScience, 57(7), 573–583.
# Methods
## Compare biodiversity risk to habitats
Using CHI habs, compare biodiversity risk by habitat type.
``` {r set up hab raster to 1 km g-p}
habs_files <- list.files(file.path(dir_M, "git-annex/impact_acceleration/habitats"),
pattern = '.tif$', full.names = TRUE)
dir_habs_reproj <- file.path(dir_o_anx, 'habs')
hab_stack <- stack(habs_files)
# plot(hab_stack)
ocean_500m_file <- file.path(dir_o_anx, 'spatial', 'ocean_500m.tif')
ocean_500m_rast <- raster(ocean_500m_file)
tmp <- parallel::mclapply(habs_files, mc.cores = 12, FUN = function(x) {
### x <- habs_files[1]
reproj_file <- file.path(dir_habs_reproj, basename(x)) %>%
str_replace('.tif$', '_500m.tif')
if(!file.exists(reproj_file)) {
cat_msg('Processing ', reproj_file)
y <- raster(x)
z <- projectRaster(y, ocean_500m_rast, method = 'ngb',
# progress = 'text',
filename = reproj_file)
} else {
cat_msg(reproj_file, ' exists; skipping it')
}
})
```
10 km x 10 km habitat raster cells will contain values approximating the number of km^2^ (out of 100) occupied by that habitat.
``` {r aggregate to 10 km g-p}
habs_500m_files <- list.files(dir_habs_reproj,
pattern = '_500m.tif$',
full.names = TRUE)
habs_10km_files <- str_replace(habs_500m_files, '_500m.tif$', '_10km.tif') %>%
setNames(habs_500m_files)
rast_base <- raster(file.path(dir_spatial, 'cell_id_rast.tif'))
for(hab_500m in habs_500m_files) {
### hab_500m <- habs_500m_files[1]
out_file <- habs_10km_files[hab_500m]
if(!file.exists(out_file)) {
cat_msg('Processing ', basename(hab_500m), ' to: ', out_file)
rast <- raster(hab_500m)
rast_10km <- raster::aggregate(rast, fact = 20, fun = sum, na.rm = TRUE)
rast_10km <- rast_10km / 4
writeRaster(rast_10km, out_file)
}
}
# z <- stack(habs_10km_files)
# plot(z)
```
``` {r set up habs and values dataframe}
cell_id_rast <- raster(file.path(dir_spatial, 'cell_id_rast.tif'))
mean_rast <- raster(file.path(dir_output,
'mean_risk_raster_comp.tif'))
mean_rr_rast <- raster(file.path(dir_output,
'mean_rr_risk_raster_comp.tif'))
pct_th_rast <- raster(file.path(dir_output,
'pct_threat_raster_comp.tif'))
pct_th_rr_rast <- raster(file.path(dir_output,
'sr_rr_pct_threat_raster_comp.tif'))
ocean_area_rast <- raster(file.path(dir_spatial, 'ocean_area_rast.tif'))
habs_10km_files <- list.files(file.path(dir_o_anx, 'habs'),
pattern = '_10km.tif$',
full.names = TRUE)
habs_rasts <- stack(habs_10km_files)
### initialize df with cell ID rast, then add columns for habs
habs_df <- data.frame(cell_id = values(cell_id_rast))
for(i in 1:nlayers(habs_rasts)) {
habs_df[ , i + 1] <- values(habs_rasts[[i]])
} ### note: data.frame with cols cell_id and V1-V22
habs_df <- habs_df %>%
setNames(c('cell_id', basename(habs_10km_files)))
mpa_df <- read_csv(file.path(dir_spatial, 'wdpa_mpa_area.csv'),
col_types = 'dddd') %>%
filter(wdpa_category <= 2) %>%
select(-wdpa_category) %>%
group_by(cell_id) %>%
summarize(mpa = sum(prot_area_km2, na.rm = TRUE))
risk_df <- habs_df %>%
mutate(risk = values(mean_rast),
rr_risk = values(mean_rr_rast),
pct_th = values(pct_th_rast),
rr_pct_th = values(pct_th_rr_rast),
ocean_area = values(ocean_area_rast)) %>%
gather(hab, cells, -cell_id, -(risk:ocean_area)) %>%
filter(!is.na(cells) & cells != 0) %>%
filter(!is.na(risk)) %>%
filter(!is.na(ocean_area))
risk_mpa_df <- risk_df %>%
left_join(mpa_df, by = 'cell_id') %>%
mutate(mpa = ifelse(is.na(mpa), 0, mpa),
mpa = ifelse(mpa > ocean_area, ocean_area, mpa),
non_mpa = ocean_area - mpa) %>%
gather(mpa_status, area, mpa, non_mpa) %>%
mutate(mpa_status = (mpa_status == 'mpa'))
# x <- risk_df %>% select(hab) %>% distinct()
# write_csv(x, file.path(dir_setup, 'raw/hab_names_raw.csv'))
habs_lookup <- read_csv(file.path(dir_setup, 'raw',
'hab_names_lookup.csv'))
risk_mpa_df <- risk_mpa_df %>%
left_join(habs_lookup, by = 'hab') %>%
filter(!is.na(order)) %>%
arrange(order) %>%
mutate(hab_desc = fct_inorder(hab_desc))
risk_mpa_df <- risk_mpa_df %>%
arrange(hab_desc) %>%
select(cell_id, risk, rr_risk, mpa_status, area, hab_desc) %>%
group_by(hab_desc, mpa_status) %>%
mutate(prop_area = area / sum(area)) %>%
ungroup()
prot_pct <- risk_mpa_df %>%
group_by(hab_desc) %>%
summarize(pct_prot = sum(area * mpa_status) / sum(area) * 100,
tot_area = sum(area))
### this is independent of range-rarity weighting
no_take_means <- risk_mpa_df %>%
group_by(mpa_status, hab_desc) %>%
summarize(mu = sum(risk * prop_area),
mu_rr = sum(rr_risk * prop_area)) %>%
ungroup()
write_csv(no_take_means, file.path(dir_setup, 'int/mpa_mean_risks_by_hab.csv'))
rm('habs_df', 'risk_df')
```
## Facet plot
``` {r plot_function}
plot_dist <- function(df, means_df, x_lims = c(0, 0.6)) {
x <- ggplot(df, aes(x = risk)) +
ggtheme_plot(base_size = 7) +
theme(strip.text.y = element_text(angle = 0, hjust = 0),
axis.text.y = element_blank(),
axis.title = element_blank(),
panel.grid.major.y = element_blank(),
plot.margin = unit(c(.1, .25, .1, .35), 'cm')) +
geom_density(aes(weight = area, x = risk, ..scaled..,
fill = mpa_status, color = mpa_status),
alpha = .5, size = .25) +
geom_vline(data = means_df,
aes(xintercept = mu, linetype = mpa_status),
color = 'grey20', alpha = .8) +
scale_x_continuous(expand = c(0, 0),
limits = x_lims,
labels = c('LC', 'NT', 'VU', 'EN', 'CR', 'EX'),
breaks = c( 0.0, 0.2, 0.4, 0.6, 0.8, 1.0)) +
scale_y_continuous(expand = c(0, 0)) +
scale_fill_manual(values = c('red4', 'cadetblue2')) +
scale_color_manual(values = c('red3', 'cadetblue4')) +
facet_grid( hab_desc ~ ., scales = 'free_y') +
labs(fill = 'Marine reserve', color = 'Marine reserve', linetype = 'Marine reserve')
return(x)
}
```
``` {r generate figure}
prot_pct_lbls <- prot_pct %>%
mutate(area_lbl = sprintf('%.1f x 10⁶ km²', tot_area/1e6),
prot_lbl = sprintf('%.1f%% no take', pct_prot))
hab_plot_unweighted <- plot_dist(risk_mpa_df, no_take_means) +
theme(strip.text.y = element_blank())
hab_plot_rrweighted <- plot_dist(risk_mpa_df %>% select(-risk, risk = rr_risk),
no_take_means %>%
select(-mu, mu = mu_rr)) +
geom_text(data = prot_pct_lbls, aes(x = .59, y = .95, label = area_lbl),
hjust = 1, size = 2.1, vjust = 1, color = 'grey20') +
geom_text(data = prot_pct_lbls, aes(x = .59, y = .45, label = prot_lbl),
hjust = 1, size = 2.1, vjust = 1, color = 'grey20')
plot_combined <- cowplot::plot_grid(hab_plot_unweighted +
theme(strip.text.y = element_blank(),
legend.justification = c(1, 1),
legend.position = c(1, 1)),
hab_plot_rrweighted +
theme(legend.position = 'none'),
labels = c('A', 'B'), hjust = 0,
label_size = 9,
rel_widths = c(2, 3))
# ggsave(file.path(dir_git, 'ms_figures/fig4_bd_risk_vs_habitat.tiff'),
# height = 2.8, width = 4.75, dpi = 300, units = 'in')
ggsave(file.path(dir_git, 'ms_figures/fig4_bd_risk_vs_habitat.png'),
height = 2.8, width = 4.75, dpi = 300, units = 'in')
```
![](ms_figures/fig4_bd_risk_vs_habitat.png)
## Wherefore the big difference between oceanic waters and high seas in terms of protected coverage?
Examine where MPAs in open ocean and MPAs in EEZs overlap.
``` {r, eval = FALSE}
cell_id_rast <- raster(file.path(dir_spatial, 'cell_id_rast.tif'))
mean_rast <- raster(file.path(dir_output,
'mean_risk_raster_comp.tif'))
surf_10km_file <- list.files(file.path(dir_o_anx, 'habs'),
pattern = 'surface_waters_10km.tif$',
full.names = TRUE)
surf_rast <- raster(surf_10km_file)
eez_rast <- raster(file.path(dir_spatial, 'eez_rast.tif'))
mpa_df <- read_csv(file.path(dir_spatial, 'wdpa_mpa_area.csv'),
col_types = 'dddd') %>%
filter(wdpa_category <= 2) %>%
select(-wdpa_category) %>%
group_by(cell_id) %>%
summarize(mpa = sum(prot_area_km2, na.rm = TRUE))
df <- data.frame(cell_id = values(cell_id_rast),
risk = values(mean_rast),
ocean = values(surf_rast),
eez = values(eez_rast)) %>%
filter(!is.na(risk)) %>%
left_join(mpa_df, by = 'cell_id')
ocean_prot_sum <- df %>%
filter(!is.na(ocean)) %>%
group_by(eez) %>%
summarize(prot = sum(mpa, na.rm = TRUE),
total = n()) %>%
filter(prot > 0)
ocean_prot <- df %>%
filter(!is.na(ocean)) %>%
filter(!is.na(mpa)) %>%
mutate(abnj = ifelse((eez == 213 | eez > 255), 1, -1))
ocean_prot_rast <- subs(cell_id_rast, ocean_prot, by = 'cell_id', which = 'abnj')
### print map
library(sf)
land_poly <- read_sf(file.path(dir_spatial, 'ne_10m_land/ne_10m_land.shp')) %>%
st_transform(gp_proj4)
map_df <- rasterToPoints(ocean_prot_rast) %>%
as.data.frame() %>%
setNames(c('long', 'lat', 'abnj'))
map1 <- ggplot(map_df) +
geom_raster(aes(long, lat, fill = abnj), show.legend = TRUE) +
geom_sf(data = land_poly, aes(geometry = geometry),
fill = 'grey96', color = 'grey40', size = .10) +
ggtheme_map(base_size = 7) +
theme(plot.margin = unit(c(.2, 0, .1, .5), units = 'cm')) +
coord_sf(datum = NA) + ### ditch graticules
scale_x_continuous(expand = c(0, 0)) +
scale_y_continuous(expand = c(0, 0))
print(map1)
```