forked from eqmh/lme-extractions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lme_extractions.Rmd
198 lines (166 loc) · 7.83 KB
/
lme_extractions.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
---
title: "OBIS extractions from Large Marine Ecosystem regions"
output: html_notebook
---
# Marine Biodiversity Observation Network Pole to Pole of the Americas ([MBON Pole to Pole](https://marinebon.org/p2p/))
Written by E. Montes (emontesh@usf.edu) on Auguts 28, 2020.
This code creates a map showing the boundaries of a selected Large Marine Ecosystems ([LME](http://lme.edc.uri.edu/index.php/lme-introduction)) and extracts records for selected taxa from the [Ocean Biodiversity Information System (OBIS)](https://obis.org/) using [robis](https://obis.org/manual/accessr/) tools.
# Step 1
First, let's load required libraries
```{r}
library(robis)
library(rgdal) # for `ogrInfo()` and `readOGR()`
library(tools) # for `file_path_sans_ext()`
library(dplyr) # for `inner_join()`, `filter()`, `summarise()`, and the pipe operator (%>%)
library(ggplot2) # for `fortify()` and for plotting
library(sp) # for `point.in.polygon()` and `spDists()`
library(tidyr) # for `gather()`
library(readr) # for `write_tsv()`
library(leaflet)
library(lubridate)
```
# Step 2
Now let's provide the function fortify.shape(), which puts the shapefile data in the object class data.frame, so that it can be used by ggplot2, and extract portions of the data (from the fortified data.frame object) for a smaller domain
```{r}
fortify.shape <- function(x){
x@data$id <- rownames(x@data)
x.f <- fortify(x, region = "id")
x.join <- inner_join(x.f, x@data, by = "id")
}
subset.shape <- function(x, domain){
x.subset <- filter(x, long > domain[1] &
long < domain[2] &
lat > domain[3] &
lat < domain[4])
x.subset
}
```
# Step 3
Let's read the shapefile "LMEs66.shp" containing all polygons, fortify the global data and then extract domain. See numbers [here](http://lme.edc.uri.edu/index.php/lme-introduction)
```{r}
path.lme.coast <- ("~/lme-extractions/data")
fnam.lme.coast <- "LMEs66.shp"
dat.coast <- readOGR(dsn = path.lme.coast,
layer = file_path_sans_ext(fnam.lme.coast))
# fortify the global data and then extract domain
dat.coast <- fortify.shape(dat.coast) # a 410951x8 dataframe
# Specify the desired LME:
dat.sel_1 <- subset(dat.coast, LME_NUMBER == 15) # S. Brazil.
dat.sel_2 <- subset(dat.coast, LME_NUMBER == 16) # E. Brazil
dat.sel_3 <- subset(dat.coast, LME_NUMBER == 17) # N. Brazil
dat.sel_4 <- subset(dat.coast, LME_NUMBER == 14) # Patagonia
dat.sel_5 <- subset(dat.coast, LME_NUMBER == 13) # Humboldt C.
dat.sel_6 <- subset(dat.coast, LME_NUMBER == 12) # Caribbean
dat.sel_7 <- subset(dat.coast, LME_NUMBER == 11) # P. Ctral A.
dat.sel_8 <- subset(dat.coast, LME_NUMBER == 5) # GoM
dat.sel_9 <- subset(dat.coast, LME_NUMBER == 4) # Gulf of California
dat.sel_10 <- subset(dat.coast, LME_NUMBER == 3) # CCS
dat.sel_11 <- subset(dat.coast, LME_NUMBER == 2) # G. Alaska
dat.sel_12 <- subset(dat.coast, LME_NUMBER == 1) # E. Bearing S.
dat.sel_13 <- subset(dat.coast, LME_NUMBER == 54) # Chukchi S.
dat.sel_14 <- subset(dat.coast, LME_NUMBER == 55) # Beauford S.
dat.sel_15 <- subset(dat.coast, LME_NUMBER == 66) # Canadian Arctic
dat.sel_16 <- subset(dat.coast, LME_NUMBER == 18) # Canadian E. Arctic
dat.sel_17 <- subset(dat.coast, LME_NUMBER == 9) # Labrador S.
dat.sel_18 <- subset(dat.coast, LME_NUMBER == 8) # Scotian Shelf
dat.sel_19 <- subset(dat.coast, LME_NUMBER == 7) # NE US
dat.sel_20 <- subset(dat.coast, LME_NUMBER == 6) # SE US
dat.sel_21 <- subset(dat.coast, LME_NUMBER == 10) # Hawaii
dat.sel_22 <- subset(dat.coast, LME_NUMBER == 63) # Hudson Bay Complex
dat.sel_23 <- subset(dat.coast, LME_NUMBER == 19) # Greenland Sea
dat.sel_24 <- subset(dat.coast, LME_NUMBER == 21) # Norwegian Sea
dat.sel_25 <- subset(dat.coast, LME_NUMBER == 20) # Barents Sea
dat.sel_26 <- subset(dat.coast, LME_NUMBER == 58) # Kara Sea
dat.sel_27 <- subset(dat.coast, LME_NUMBER == 57) # Laptev Sea
dat.sel_28 <- subset(dat.coast, LME_NUMBER == 56) # E. Sibarian Sea
```
# Step 4
Plotting the coastline and selected LME boundaries
```{r}
# Define lat/lon limits here
xlims <- c(-150, -25)
ylims <- c(-60, 60)
# Generate a base map with the coastline:
p0 <- ggplot() + theme(text = element_text(size=18)) +
geom_path(data = dat.coast, aes(x = long, y = lat, group = group),
color = "black", size = 0.25) +
coord_map(projection = "mercator") +
scale_x_continuous(limits = xlims, expand = c(0, 0)) +
scale_y_continuous(limits = ylims, expand = c(0, 0)) +
labs(list(title = "", x = "Longitude", y = "Latitude"))
p0
# highlight LME of interest
p.sel <- p0 +
geom_path(data = dat.sel_6,
aes(x = long, y = lat, group = group),
colour = "chartreuse4", size = 1) +
geom_path(data = dat.sel_8,
aes(x = long, y = lat, group = group),
colour = "coral", size = 0.75)
p.sel
```
# Step 5
This section extracts OBIS records of using the "occurrence" function or downloaded data, and plots time series or pie charts with distributions of large groups (annelids, molluscs, plants and echinoderms) in the upper 100m.
```{r}
# Set extraction params
# LME codes (from OBIS URL)
# N Brazil=40017; E Brazil=40016; S Brazil=40015; Patagonia=40014; Humboldt=40013; Caribbean=40012;
# P Ctral A=40011; CCS=40003; GoA=40002; NE USA=40007; E Bearing=40001; Canada E Arctic=40018; GoM=40005;
# Chukchi=40054; SE USA=40006; Labrador=40009; Scotian S=40008
area = 40005
depth = 100
mol_code = 51
echi_code = 1806
anne_code = 882
plan_code = 3
## read data from OBIS
## Molluscs
mollusc.100_2 = occurrence(areaid = area, taxonid = mol_code, enddepth = depth)
## Ehinoderms
echino.100_2 = occurrence(areaid = area, taxonid = echi_code, enddepth = depth)
## Annelida
anne.100_2 = occurrence(areaid = area, taxonid = anne_code, enddepth = depth)
## Platae
plant.100_2 = occurrence(areaid = area, taxonid = plan_code, enddepth = depth)
## Merge the data frames
#subset data (year and phylum)
sub_mollusc <- data.frame(mollusc.100_2$date_year, mollusc.100_2$phylum)
# rename column headers
names(sub_mollusc)[names(sub_mollusc) == "mollusc.100_2.date_year"] <- "year"
names(sub_mollusc)[names(sub_mollusc) == "mollusc.100_2.phylum"] <- "phylum"
sub_echino <- data.frame(echino.100_2$date_year, echino.100_2$phylum)
names(sub_echino)[names(sub_echino) == "echino.100_2.date_year"] <- "year"
names(sub_echino)[names(sub_echino) == "echino.100_2.phylum"] <- "phylum"
sub_anne <- data.frame(anne.100_2$date_year, anne.100_2$phylum)
names(sub_anne)[names(sub_anne) == "anne.100_2.date_year"] <- "year"
names(sub_anne)[names(sub_anne) == "anne.100_2.phylum"] <- "phylum"
sub_plant <- data.frame(plant.100_2$date_year, plant.100_2$phylum)
names(sub_plant)[names(sub_plant) == "plant.100_2.date_year"] <- "year"
names(sub_plant)[names(sub_plant) == "plant.100_2.phylum"] <- "phylum"
total.100 <- bind_rows(sub_mollusc, sub_echino, sub_anne, sub_plant)
## Plot the data
ts_plot <- ggplot() +
geom_histogram(data = total.100, aes(x = year, fill = phylum), binwidth = 2) +
scale_fill_brewer(palette = "Spectral") +
xlim(c(1960, 2017)) +
theme(axis.text=element_text(size=12),
axis.title=element_text(size=14,face="bold")) +
theme(axis.text.x = element_text(size=14, angle=0),
axis.text.y = element_text(size=14, angle=0))
ts_plot
# ggsave(ts_plot, filename = "test.png", device = "png", width = 20, height = 10, dpi=300)
## Plot pie chart
# Group plants in a single group
all_tbl <- table(total.100$phylum)
all_df <- as.data.frame(all_tbl)
p_list = c("Chlorophyta", "Rhodophyta", "Tracheophyta")
rest_list = c("Annelida", "Echinodermata", "Mollusca")
p_idx = match(p_list, rownames(all_tbl))
rest_idx = match(rest_list, rownames(all_tbl))
p_sum = sum(all_df$Freq[p_idx], na.rm = TRUE)
freq_val <- c(all_df$Freq[rest_idx], p_sum)
group_id <- c(c(rest_list), "Plants")
f_tbl <- data.frame(group = group_id, freq = freq_val)
cols <- rainbow(nrow(f_tbl))
groups_pie <- pie(f_tbl$freq, labels = f_tbl$group, col = cols)
```