-
Notifications
You must be signed in to change notification settings - Fork 7
/
README.Rmd
executable file
·430 lines (329 loc) · 19.2 KB
/
README.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
---
title: "rasterSp: R Package to rasterize and summarise IUCN range maps"
output: github_document
editor_options:
chunk_output_type: console
---
## R Setup
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = T, fig.width=8, fig.height=8, warning=F, comment=NA, message=F, fig.path="figures/")
```
### Install rasterSp package
To *use* the package, you first have to install it from GitHub using the `remotes` package.
```{r, install_package, eval=FALSE}
# Install remotes if not previously installed
if(!"remotes" %in% installed.packages()[,"Package"]) install.packages("remotes")
# Install rasterSp from Github if not previously installed
if(!"rasterSp" %in% installed.packages()[,"Package"]) remotes::install_github("RS-eco/rasterSp", build_vignettes = T)
```
Load the rasterSp package
```{r}
library(rasterSp)
```
**If you encounter a bug or if you have any problems, please file an [issue](https://github.com/RS-eco/rasterSp/issues) on Github.**
## Species Data
The rasterSp package includes various rasterized species data. For example, the bird distribution data can be loaded by:
```{r}
data(ter_birds_dist)
```
**Note:** The code for how this data was created data can be found in the data-raw folder.
### Specify path of file directory
```{r global_options}
filedir <- "/home/matt/Documents/"
```
**Note:** You need to adapt filedir according to the path on your computer, where the IUCN Data is stored.
### Rasterize IUCN species shapefiles
IUCN data was downloaded from: https://www.iucnredlist.org/resources/spatial-data-download, by running the following code:
```{r, eval=F}
if(!dir.exists(paste0(filedir, "/IUCN/TERRESTRIAL_MAMMALS"))){
download.file("http://spatial-data.s3.amazonaws.com/groups/TERRESTRIAL_MAMMALS.zip",
destfile = paste0(filedir, "/IUCN/TERRESTRIAL_MAMMALS.zip")) # <-- 594.3 MB
unzip(paste0(filedir, "/IUCN/TERRESTRIAL_MAMMALS.zip"), exdir = paste0(filedir, "/IUCN"))
unlink(paste0(filedir, "/IUCN/TERRESTRIAL_MAMMALS.zip"))
}
```
You have one shapefile for a group of animals, consisting of individual polygons for each species.
Using the `rasterizeRange()` function, the spatial distribution of each species is turned into a grid with a specific resolution. You can specify the resolution in degrees, here we use 0.5°. If save = TRUE, the grid for each species is saved to a GTiff file at the specified location (see filepath argument).
**Please note:** If touches = TRUE, all cells touched by lines or polygons are affected, not just those on the line render path, or whose center point is within the polygon.
```{r rasterizeRange, eval=FALSE}
# Convert shape files into rasters and save to file
rasterizeRange(dsn=paste0(filedir, "/IUCN/AMPHIBIANS.shp"),
resolution=0.5, save=TRUE, touches=T,
seasonal=c(1,2), origin=1, presence=c(1,2),
path=paste0(filedir, "/SpeciesData/"))
rasterizeRange(dsn=paste0(filedir, "/IUCN/FW_ODONATA_v6.2/FW_ODONATA.shp"),
resolution=0.5, save=TRUE, touches=T,
seasonal=c(1,2), origin=1, presence=c(1,2),
path=paste0(filedir, "/SpeciesData/"))
rasterizeRange(dsn=paste0(filedir, "/IUCN/TERRESTRIAL_MAMMALS.shp"),
resolution=0.5, save=TRUE, touches=T,
seasonal=c(1,2), origin=1, presence=c(1,2),
path=paste0(filedir, "/SpeciesData/"))
rasterizeRange(dsn=paste0(filedir, "/IUCN/REPTILES.shp"),
resolution=0.5, save=TRUE, touches=T,
seasonal=c(1,2), origin=1, presence=c(1,2),
path=paste0(filedir, "/SpeciesData/"))
rasterizeRange(dsn=paste0(filedir, "/IUCN/REPTILES_v6.3/REPTILES_PART1.shp"),
id="sci_name", resolution=0.5, save=TRUE, touches=T,
seasonal=c(1,2), origin=1, presence=c(1,2),
path=paste0(filedir, "/ReptileData_v6.3/"))
rasterizeRange(dsn=paste0(filedir, "/IUCN/REPTILES_v6.3/REPTILES_PART2.shp"),
id="sci_name", resolution=0.5, save=TRUE, touches=T,
seasonal=c(1,2), origin=1, presence=c(1,2),
path=paste0(filedir, "/ReptileData_v6.3/"))
```
### Rasterize BirdLife species shapefiles
Bird data was obtained from BirdLife. Currently we use the 2015 version, which comes in form of individual shapefiles for each species. **Note:** The `rasterizeRange` function can also handle a list of shapefiles. <!-- 2015 BirdLife International data came from Christian (All.7z). -->
```{r rasterizeBirdlife, eval=FALSE}
rasterizeRange(dsn=list.files(paste0(filedir, "/BirdLife/"), pattern=".shp", full.names=TRUE),
id="SCINAME", resolution=0.5, save=TRUE, touches=T,
seasonal=c(1,2), origin=1, presence=c(1,2),
path=paste0(filedir, "/SpeciesData/"))
```
BirdLife as well as IUCN shapefiles provide information on a couple of parameters (e.g. seasonal, origin and presence). These three parameters are implemented in the `rasterizeRange` function, which then selects only a specific subset of Polygons for each species. Infos on the different parameters, can be found here: http://datazone.birdlife.org/species/spcdistPOS.
### Birdlife 2018 data
The Birdlife 2018 data comes as gdb file, but can be converted in a shapfile by:
```{r, eval=FALSE}
library(rgdal)
# The input file geodatabase
gdb_file = paste0(filedir, "/BOTW.gdb")
# List all feature classes in a file geodatabase
subset(ogrDrivers(), grepl("GDB", name))
fc_list = ogrListLayers(gdb_file)
print(fc_list)
# Read the feature class
botw <- readOGR(dsn=gdb_file, layer="All_Species")
# Save as shapefile
writeOGR(botw, dsn=paste0(filedir, "/BirdLife_2018/"), layer="All_Species", driver="ESRI Shapefile")
```
In our case this was done in ArcMap, as it is much faster, although using the `sf` package might increase the performance in R considerably. The shapefile can then be loaded directly from file, by:
```{r, eval=FALSE}
botw <- sf::st_read(dsn=paste0(filedir, "/BirdLife_2018/"), layer="All_Species")
```
## Rasterize GARD Reptile Data
GARD reptile data was downloaded from: https://datadryad.org/resource/doi:10.5061/dryad.83s7k
When using this data, please cite the original publication:
Roll U, Feldman A, Novosolov M, Allison A, Bauer AM, Bernard R, Böhm M, Castro-Herrera F, Chirio L, Collen B, Colli GR, Dabool L, Das I, Doan TM, Grismer LL, Hoogmoed M, Itescu Y, Kraus F, LeBreton M, Lewin A, Martins M, Maza E, Meirte D, Nagy ZT, de C. Nogueira C, Pauwels OSG, Pincheira-Donoso D, Powney GD, Sindaco R, Tallowin OJS, Torres-Carvajal O, Trape J, Vidan E, Uetz P, Wagner P, Wang Y, Orme CDL, Grenyer R, Meiri S (2017) The global distribution of tetrapods reveals a need for targeted reptile conservation. Nature Ecology & Evolution 1(11): 1677–1682. https://doi.org/10.1038/s41559-017-0332-2
Additionally, please cite the Dryad data package:
Meiri S, Roll U, Grenyer R, Feldman A, Novosolov M, Bauer AM (2017) Data from: The global distribution of tetrapods reveals a need for targeted reptile conservation. Dryad Digital Repository. https://doi.org/10.5061/dryad.83s7k
```{r, eval=F}
rasterizeRange(dsn=paste0(filedir, "/GARD1.1_dissolved_ranges/modeled_reptiles.shp"),
id="Binomial", resolution=0.5, save=TRUE, touches=T,
path=paste0(filedir, "/GARD_SpeciesData/"))
```
## Calculate global species richness
The calcSR function uses a stepwise procedure to calculate the sum of species for each grid cell. This means only two files are loaded into memory at the same time to avoid memory shortage.
```{r calculate_sr, fig.width=10, fig.height=6, eval=F}
#Calculate amphibian richness
data(amphibians)
sr_amphibians <- calcSR(species_names=amphibians$binomial, path=paste0(filedir, "/SpeciesData/"))
raster::plot(sr_amphibians)
```
However, this approach takes quite a while and means we have to recalculate the species richness everytime we change the species names. A much faster way is to save the presence of each species as a data frame and then only extract the species we are interested in. This is shown further below.
## Calculate SR per Group
```{r}
# Calulate amphibian SR
library(dplyr)
sr_amphibians <- amphibians_dist %>% mutate(group = "Amphibians", presence = 1) %>%
group_by(x, y, group) %>% summarise(sum = sum(presence))
# Calulate odonata SR
sr_odonata <- odonata_dist %>% mutate(group = "Odonata", presence = 1) %>%
group_by(x, y, group) %>% summarise(sum = sum(presence))
# Calulate terrestrial mammal SR
sr_ter_mammals <- ter_mammals_dist %>% mutate(group = "Mammals", presence = 1) %>%
group_by(x, y, group) %>% summarise(sum = sum(presence))
# Calulate terrestrial bird SR
sr_ter_birds <- ter_birds_dist %>% mutate(group = "Birds", presence = 1) %>%
group_by(x, y, group) %>% summarise(sum = sum(presence))
# Calulate reptile SR
sr_reptiles <- reptiles_dist %>% mutate(group = "Reptiles", presence = 1) %>%
group_by(x, y, group) %>% summarise(sum = sum(presence))
sr_gard_reptiles <- gard_reptiles_dist %>% mutate(group = "GARD_Reptiles", presence = 1) %>%
group_by(x, y, group) %>% summarise(sum = sum(presence))
```
## Plot global map of SR per taxa
```{r iucn_sr_alltaxa, fig.width=7, fig.height=8, dpi=600}
library(ggmap2)
sr_alltaxa <- do.call(rbind, list(sr_amphibians, sr_ter_birds, sr_ter_mammals))
sr_alltaxa <- tidyr::spread(sr_alltaxa, group, sum)
ggmap2(sr_alltaxa, name=c("Amphibians", "Birds", "Mammals"), split=TRUE, ncol=1, country=T)
```
## Plot global map of Reptile SR
```{r sr_reptiles, fig.width=7, fig.height=7, dpi=600}
library(ggmap2)
sr_rept <- do.call(rbind, list(sr_reptiles, sr_gard_reptiles))
sr_rept <- tidyr::spread(sr_rept, group, sum)
ggmap2(sr_rept, name=c("Reptiles", "GARD Reptiles"), split=TRUE, ncol=1, country=T)
```
## Plot global map of Odonata SR
```{r sr_odonata, fig.width=9.5, fig.height=4, dpi=600}
library(ggmap2)
sr_odonata <- tidyr::spread(sr_odonata, group, sum)
ggmap2(sr_odonata, name=c("Odonata"), ncol=1, country=T)
```
## Calculate and plot SR by order or family
**Note:** Amphibian plot by order looks really weird and has a strange z-axis value compared to the overall amphibian map
```{r sr_amphibian_order, eval=T}
# Calculate the Amphibian SR per order
amphi_all <- left_join(amphibians_dist, amphibians[,c("binomial", "order_name")],
by = c("species" = "binomial"))
amphi_sum <- amphi_all %>% mutate(presence = 1) %>% group_by(x, y, order_name) %>%
summarise(sum = sum(presence)) %>% as.data.frame()
ggmap2(data=amphi_sum, name="SR", split=FALSE, long=TRUE,
subnames=unique(amphi_sum$order_name), ncol=1)
```
## Calculate number of presences per species
Show distribution of number of presences per species
```{r}
# Number of records per species
count_sp_amphibian <- amphibians_dist %>% mutate(group = "Amphibians", presence = 1) %>%
group_by(species, group) %>% summarise(sum = sum(presence))
count_sp_odonata <- odonata_dist %>% mutate(group = "Odonata", presence = 1) %>%
group_by(species, group) %>% summarise(sum = sum(presence))
count_sp_ter_mammal <- ter_mammals_dist %>% mutate(group = "Mammals", presence = 1) %>%
group_by(species, group) %>% summarise(sum = sum(presence))
count_sp_ter_bird <- ter_birds_dist %>% mutate(group = "Birds", presence = 1) %>%
group_by(species, group) %>% summarise(sum = sum(presence))
count_sp_reptiles <- reptiles_dist %>% mutate(group = "Reptiles", presence = 1) %>%
group_by(species, group) %>% summarise(sum = sum(presence))
count_sp_gard_reptiles <- gard_reptiles_dist %>% mutate(group = "GARD_Reptiles", presence = 1) %>%
group_by(species, group) %>% summarise(sum = sum(presence))
# Combine counts per species into one dataframe
species_presences_alltaxa <- rbind(count_sp_amphibian, count_sp_odonata, count_sp_ter_bird,
count_sp_ter_mammal, count_sp_reptiles, count_sp_gard_reptiles)
rm(count_sp_amphibian, count_sp_odonata, count_sp_ter_mammal, count_sp_ter_bird,
count_sp_reptiles, count_sp_gard_reptiles)
```
Create Table with number of records per class and group
```{r, results="asis"}
# Calculate number of records for each class (<10, 10-50, >50 records)
library(dplyr)
class1 <- species_presences_alltaxa %>% group_by(group) %>% filter(sum < 10) %>% summarise(n())
class2 <- species_presences_alltaxa %>% group_by(group) %>% filter(sum >= 10, sum <= 50) %>% summarise(n())
class3 <- species_presences_alltaxa %>% group_by(group) %>% filter(sum > 50) %>% summarise(n())
sum_records <- Reduce(function(x,y) merge(x,y, by="group"), list(class1, class2, class3))
colnames(sum_records) <- c("Group", "n < 10", "10 <= n <= 50", "n > 50")
# Create table
knitr::kable(sum_records, style="markdown")
```
## Create plot of small ranging species (n < 10)
```{r iucn_sr_smallrange, fig.width=9.5, fig.height=4, dpi=300}
#Calculate SR of for non-modelled species
library(dplyr)
sr_amphibians_smallrange <- amphibians_dist_smallrange %>%
group_by(x, y, group) %>% summarise(sum = sum(presence))
sr_odonata_smallrange <- odonata_dist_smallrange %>%
group_by(x, y, group) %>% summarise(sum = sum(presence))
sr_ter_birds_smallrange <- ter_birds_dist_smallrange %>%
group_by(x, y, group) %>% summarise(sum = sum(presence))
sr_ter_mammals_smallrange <- ter_mammals_dist_smallrange %>%
group_by(x, y, group) %>% summarise(sum = sum(presence))
sr_reptiles_smallrange <- reptiles_dist_smallrange %>%
group_by(x, y, group) %>% summarise(sum = sum(presence))
#Create plot of global richness per group
sr_alltaxa_smallrange <- do.call(rbind, list(sr_amphibians_smallrange, sr_odonata_smallrange,
sr_ter_birds_smallrange, sr_ter_mammals_smallrange,
sr_reptiles_smallrange))
sr_alltaxa_smallrange <- tidyr::spread(sr_alltaxa_smallrange, group, sum)
data(outline, package="ggmap2")
ggmap2::ggmap2(sr_alltaxa_smallrange, name=c("Amphibians", "Odonata", "Birds", "Mammals", "Reptiles"),
split=TRUE, ncol=2, country=T)
```
## Threatened species (according to IUCN Red List)
Plot SR of threatened species
```{r iucn_sr_threatened, fig.width=8, fig.height=9, dpi=300}
#Calculate SR of for threatened species
sr_amphibians_threatened <- amphibians_threatened %>% mutate(group = "Amphibians", presence=1) %>%
group_by(x, y, group) %>% summarise(sum = sum(presence))
sr_ter_mammals_threatened <- ter_mammals_threatened %>% mutate(group = "Mammals", presence=1) %>%
group_by(x, y, group) %>% summarise(sum = sum(presence))
sr_ter_birds_threatened <- ter_birds_threatened %>% mutate(group = "Birds", presence=1) %>%
group_by(x, y, group) %>% summarise(sum = sum(presence))
sr_alltaxa_threatened <- do.call(rbind, list(sr_amphibians_threatened,
sr_ter_birds_threatened,
sr_ter_mammals_threatened))
sr_alltaxa_threatened <- tidyr::spread(sr_alltaxa_threatened, group, sum)
library(ggmap2)
ggmap2(sr_alltaxa_threatened, name=c("Amphibians", "Birds", "Mammals"),
split=TRUE, ncol=1, country=T)
```
## Endemic species (by country)
Create dataframe of species data, which only occur in one country. And add country to species data.
```{r iucn_sr_endemic, fig.width=9.5, fig.height=4, dpi=300}
#Rasterize country shapefile
data(countriesHigh, package="rworldxtra")
data(landseamask_generic, package="rISIMIP")
countries <- raster::rasterize(countriesHigh, landseamask_generic,
field="SOV_A3")
countries <- data.frame(raster::rasterToPoints(countries))
colnames(countries) <- c("x", "y", "country")
# Identify species that only occur in one country
amphibians_endemic <- amphibians_dist %>% left_join(countries) %>%
group_by(species) %>% summarise(n = n_distinct(country)) %>%
filter(n == 1)
ter_mammals_endemic <- ter_mammals_dist %>% left_join(countries) %>%
group_by(species) %>% summarise(n = n_distinct(country)) %>%
filter(n == 1)
ter_birds_endemic <- ter_birds_dist %>% left_join(countries) %>%
group_by(species) %>% summarise(n = n_distinct(country)) %>%
filter(n == 1)
reptiles_endemic <- reptiles_dist %>% left_join(countries) %>%
group_by(species) %>% summarise(n = n_distinct(country)) %>%
filter(n == 1)
# Subset species by endemism
amphibians_dist_endemic <- amphibians_dist %>%
filter(species %in% amphibians_endemic$species)
ter_mammals_dist_endemic <- ter_mammals_dist %>%
filter(species %in% ter_mammals_endemic$species)
ter_birds_dist_endemic <- ter_birds_dist %>%
filter(species %in% ter_birds_endemic$species)
reptiles_dist_endemic <- reptiles_dist %>%
filter(species %in% reptiles_endemic$species)
# Calculate SR of endemic species
library(dplyr)
sr_amphibians_endemic <- amphibians_dist_endemic %>% mutate(group = "Amphibians", presence = 1) %>%
group_by(x, y, group) %>% summarise(sum = sum(presence))
sr_ter_mammals_endemic <- ter_mammals_dist_endemic %>% mutate(group = "Mammals", presence = 1) %>%
group_by(x, y, group) %>% summarise(sum = sum(presence))
sr_ter_birds_endemic <- ter_birds_dist_endemic %>% mutate(group = "Birds", presence = 1) %>%
group_by(x, y, group) %>% summarise(sum = sum(presence))
sr_reptiles_endemic <- reptiles_dist_endemic %>% mutate(group = "Reptiles", presence = 1) %>%
group_by(x, y, group) %>% summarise(sum = sum(presence))
# Plot SR of endemics
sr_alltaxa_endemic <- do.call(rbind, list(sr_amphibians_endemic,
sr_ter_birds_endemic,
sr_ter_mammals_endemic,
sr_reptiles_endemic))
sr_alltaxa_endemic <- tidyr::spread(sr_alltaxa_endemic, group, sum)
library(ggmap2)
ggmap2(sr_alltaxa_endemic, name=c("Amphibians", "Birds", "Mammals", "Reptiles"),
split=TRUE, ncol=2, country=T)
```
## Endemic species (by range size)
```{r iucn_sr_endemic_rangesize, fig.width=9.5, fig.height=4, dpi=300}
# Load endemic species names
data(amphibians_endemic)
data(ter_mammals_endemic)
data(ter_birds_endemic)
data(reptiles_endemic)
# Calculate SR of endemic species
sr_amphibians_endemic <- amphibians_dist %>% filter(species %in% amphibians_endemic$species_name) %>%
mutate(group = "Amphibians", presence = 1) %>% group_by(x, y, group) %>% summarise(sum = sum(presence))
sr_ter_mammals_endemic <- ter_mammals_dist %>% filter(species %in% ter_mammals_endemic$species_name) %>%
mutate(group = "Mammals", presence = 1) %>% group_by(x, y, group) %>% summarise(sum = sum(presence))
sr_ter_birds_endemic <- ter_birds_dist %>% filter(species %in% ter_birds_endemic$species_name) %>%
mutate(group = "Birds", presence = 1) %>% group_by(x, y, group) %>% summarise(sum = sum(presence))
sr_reptiles_endemic <- reptiles_dist %>% filter(species %in% reptiles_endemic$species_name) %>%
mutate(group = "Reptiles", presence = 1) %>%
group_by(x, y, group) %>% summarise(sum = sum(presence))
# Plot SR of endemics
sr_alltaxa_endemic <- do.call(rbind, list(sr_amphibians_endemic,
sr_ter_birds_endemic,
sr_ter_mammals_endemic,
sr_reptiles_endemic))
sr_alltaxa_endemic <- tidyr::spread(sr_alltaxa_endemic, group, sum)
library(ggmap2)
ggmap2(sr_alltaxa_endemic, name=c("Amphibians", "Birds", "Mammals", "Reptiles"), split=TRUE, ncol=2, country=T)
```
## Invasive species
To assess the invasiveness of species ranges, see the corresponding `vignette("iucn-invasions")`.