-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBATS_TrophicAmplification.R
246 lines (178 loc) · 8.8 KB
/
BATS_TrophicAmplification.R
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
# Jack Conroy
# 3 Nov 2023
# BATS zoop biomass time series
# BATS surface chl time series
## ------------------------------------------ ##
# Housekeeping -----
## ------------------------------------------ ##
# Load necessary libraries
# If you don't have the "librarian" package, uncomment the next line and run it to install the package
# install.packages("librarian")
librarian::shelf(tidyverse, googledrive, zoo)
# Set site
site <- "PAL"
# Create necessary sub-folder(s)
dir.create(path = file.path("raw_data"), showWarnings = F)
dir.create(path = file.path("raw_data", site), showWarnings = F)
# Identify raw data files
# For example, here I'm pulling all the PAL csv files from Google Drive
# A new window will pop up asking you to select the appropriate Google Drive account
# For more help, see: https://nceas.github.io/scicomp.github.io/tutorials.html#using-the-googledrive-r-package
raw_PAL_ids <- googledrive::drive_ls(googledrive::as_id("https://drive.google.com/drive/u/1/folders/14k1iUJv7a7ZXj34Y35Iy9pEKJnO53ONH"),
type = "csv")
# For each raw data file, download it into its own site folder
for(k in 1:nrow(raw_PAL_ids)){
# Download file (but silence how chatty this function is)
googledrive::with_drive_quiet(
googledrive::drive_download(file = raw_PAL_ids[k, ]$id, overwrite = T,
path = file.path("raw_data", site, raw_PAL_ids[k, ]$name)) )
# Print success message
message("Downloaded file ", k, " of ", nrow(raw_PAL_ids))
}
# total zoop dry weight
zoopDW <- read.csv(file.path("raw_data", site, "BATSZoopBiomassMonthly.csv"))
min(zoopDW$avgDryWtMgM3TotYM)
min(na.omit(zoopDW$avgDryWtMgM3TotYM))
zoopDW$adjTotalDW <- log10(zoopDW$avgDryWtMgM3TotYM)
hist(zoopDW$adjTotalDW)
zoopDW$Date <- as.yearmon(paste(zoopDW$Year, zoopDW$Month), "%Y %m")
plot(adjTotalDW ~ Date, data = zoopDW, type = "b",
main = "Total zooplankton dry weight - BATS")
# I'm not quite sure how this rolling mean is being aligned, because it is even number
# Can't use rollmean b/c there are NAs
run.mean.total <- rollapply(zoopDW$adjTotalDW, 12, function(x) mean(x, na.rm = T))
sd(run.mean.total)
plot(adjTotalDW ~ Date, data = zoopDW, type = "b",
main = "Total zooplankton dry weight - BATS, std. dev. = 0.095", ylab = "logZoopTotal",
ylim = c(-0.2, 1), xlim = c(zoopDW$Date[1], zoopDW$Date[length(zoopDW$Date)]))
par(new = T)
# manually coding the dates for the rolling mean b/c uncertain about above alignment
plot(run.mean.total ~ zoopDW$Date[6:345], type = "l",
col = "red", lwd = 3,
ylim = c(-0.2, 1), xlim = c(zoopDW$Date[1], zoopDW$Date[length(zoopDW$Date)]),
ylab = "", xlab = "")
# 0.2-0.5 mm zoop dry weight
min(zoopDW$avgDryWtMgM30200YM)
min(na.omit(zoopDW$avgDryWtMgM30200YM))
zoopDW$adj200DW <- log10(zoopDW$avgDryWtMgM30200YM)
hist(zoopDW$adj200DW)
plot(adj200DW ~ Date, data = zoopDW, type = "b",
main = "0.2-0.5 mm zooplankton dry weight - BATS")
# I'm not quite sure how this rolling mean is being aligned, because it is even number
# Can't use rollmean b/c there are NAs
run.mean.200 <- rollapply(zoopDW$adj200DW, 12, function(x) mean(x, na.rm = T))
sd(run.mean.200)
plot(adj200DW ~ Date, data = zoopDW, type = "b",
main = "0.2-0.5 mm zooplankton dry weight - BATS, std. dev. = 0.116", ylab = "logZoop0.2mm",
ylim = c(-1, 0.5), xlim = c(zoopDW$Date[1], zoopDW$Date[length(zoopDW$Date)]))
par(new = T)
# manually coding the dates for the rolling mean b/c uncertain about above alignment
plot(run.mean.200 ~ zoopDW$Date[6:345], type = "l",
col = "red", lwd = 3,
ylim = c(-1, 0.5), xlim = c(zoopDW$Date[1], zoopDW$Date[length(zoopDW$Date)]),
ylab = "", xlab = "")
# 0.5-1 mm zoop dry weight
min(zoopDW$avgDryWtMgM30500YM)
min(na.omit(zoopDW$avgDryWtMgM30500YM))
zoopDW$adj500DW <- log10(zoopDW$avgDryWtMgM30500YM)
hist(zoopDW$adj500DW)
plot(adj500DW ~ Date, data = zoopDW, type = "b",
main = "0.5-1 mm zooplankton dry weight - BATS")
# I'm not quite sure how this rolling mean is being aligned, because it is even number
# Can't use rollmean b/c there are NAs
run.mean.500 <- rollapply(zoopDW$adj500DW, 12, function(x) mean(x, na.rm = T))
sd(run.mean.500)
plot(adj500DW ~ Date, data = zoopDW, type = "b",
main = "0.5-1 mm zooplankton dry weight - BATS, std. dev. = 0.119", ylab = "logZoop0.5mm",
ylim = c(-1, 0.5), xlim = c(zoopDW$Date[1], zoopDW$Date[length(zoopDW$Date)]))
par(new = T)
# manually coding the dates for the rolling mean b/c uncertain about above alignment
plot(run.mean.500 ~ zoopDW$Date[6:345], type = "l",
col = "red", lwd = 3,
ylim = c(-1, 0.5), xlim = c(zoopDW$Date[1], zoopDW$Date[length(zoopDW$Date)]),
ylab = "", xlab = "")
# 1-2 mm zoop dry weight
min(zoopDW$avgDryWtMgM31000YM)
min(na.omit(zoopDW$avgDryWtMgM31000YM))
zoopDW$adj1000DW <- log10(zoopDW$avgDryWtMgM31000YM)
hist(zoopDW$adj1000DW)
plot(adj1000DW ~ Date, data = zoopDW, type = "b",
main = "1-2 mm zooplankton dry weight - BATS")
# I'm not quite sure how this rolling mean is being aligned, because it is even number
# Can't use rollmean b/c there are NAs
run.mean.1000 <- rollapply(zoopDW$adj1000DW, 12, function(x) mean(x, na.rm = T))
sd(run.mean.1000)
plot(adj1000DW ~ Date, data = zoopDW, type = "b",
main = "1-2 mm zooplankton dry weight - BATS, std. dev. = 0.108", ylab = "logZoop1mm",
ylim = c(-1, 0.5), xlim = c(zoopDW$Date[1], zoopDW$Date[length(zoopDW$Date)]))
par(new = T)
# manually coding the dates for the rolling mean b/c uncertain about above alignment
plot(run.mean.1000 ~ zoopDW$Date[6:345], type = "l",
col = "red", lwd = 3,
ylim = c(-1, 0.5), xlim = c(zoopDW$Date[1], zoopDW$Date[length(zoopDW$Date)]),
ylab = "", xlab = "")
# 2-5 mm zoop dry weight
min(zoopDW$avgDryWtMgM32000YM)
min(na.omit(zoopDW$avgDryWtMgM32000YM))
zoopDW$adj2000DW <- log10(zoopDW$avgDryWtMgM32000YM)
hist(zoopDW$adj2000DW)
plot(adj2000DW ~ Date, data = zoopDW, type = "b",
main = "2-5 mm zooplankton dry weight - BATS")
# I'm not quite sure how this rolling mean is being aligned, because it is even number
# Can't use rollmean b/c there are NAs
run.mean.2000 <- rollapply(zoopDW$adj2000DW, 12, function(x) mean(x, na.rm = T))
sd(run.mean.2000)
plot(adj2000DW ~ Date, data = zoopDW, type = "b",
main = "2-5 mm zooplankton dry weight - BATS, std. dev. = 0.137", ylab = "logZoop2mm",
ylim = c(-1.5, 0.5), xlim = c(zoopDW$Date[1], zoopDW$Date[length(zoopDW$Date)]))
par(new = T)
# manually coding the dates for the rolling mean b/c uncertain about above alignment
plot(run.mean.2000 ~ zoopDW$Date[6:345], type = "l",
col = "red", lwd = 3,
ylim = c(-1.5, 0.5), xlim = c(zoopDW$Date[1], zoopDW$Date[length(zoopDW$Date)]),
ylab = "", xlab = "")
# >5 mm zoop dry weight
min(zoopDW$avgDryWtMgM35000YM)
min(na.omit(zoopDW$avgDryWtMgM35000YM))
zoopDW$adj5000DW <- log10(zoopDW$avgDryWtMgM35000YM)
hist(zoopDW$adj5000DW)
plot(adj5000DW ~ Date, data = zoopDW, type = "b",
main = ">5 mm zooplankton dry weight - BATS")
# I'm not quite sure how this rolling mean is being aligned, because it is even number
# Can't use rollmean b/c there are NAs
run.mean.5000 <- rollapply(zoopDW$adj5000DW, 12, function(x) mean(x, na.rm = T))
sd(run.mean.5000)
plot(adj5000DW ~ Date, data = zoopDW, type = "b",
main = ">5 mm zooplankton dry weight - BATS, std. dev. = 0.141", ylab = "logZoop5mm",
ylim = c(-2, 1), xlim = c(zoopDW$Date[1], zoopDW$Date[length(zoopDW$Date)]))
par(new = T)
# manually coding the dates for the rolling mean b/c uncertain about above alignment
plot(run.mean.5000 ~ zoopDW$Date[6:345], type = "l",
col = "red", lwd = 3,
ylim = c(-2, 1), xlim = c(zoopDW$Date[1], zoopDW$Date[length(zoopDW$Date)]),
ylab = "", xlab = "")
# Surface chlorophyll a
chl <- read.csv(file.path("raw_data", site, "BATSChlAMonthly.csv"))
min(chl$avgChlA)
min(na.omit(chl$avgChlA))
# sometimes the chl concentration is zero. that must be wrong. so replacing with NAs
chl$avgChlA[chl$avgChlA == 0] <- NA
min(na.omit(chl$avgChlA))
chl$adjChl <- log10(chl$avgChlA)
hist(chl$adjChl)
chl$Date <- as.yearmon(paste(chl$Year, chl$Month), "%Y %m")
plot(adjChl ~ Date, data = chl, type = "b",
main = "Chlorophyll a - BATS")
# I'm not quite sure how this rolling mean is being aligned, because it is even number
# Can't use rollmean b/c there are NAs
run.mean.chl <- rollapply(chl$adjChl, 12, function(x) mean(x, na.rm = T))
sd(run.mean.chl)
plot(adjChl ~ Date, data = chl, type = "b",
main = "Chlorophyll a - BATS, std. dev. = 0.112", ylab = "logChl",
ylim = c(1, 3), xlim = c(chl$Date[1], zoopDW$Date[length(zoopDW$Date)]))
par(new = T)
# manually coding the dates for the rolling mean b/c uncertain about above alignment
plot(run.mean.chl ~ chl$Date[6:391], type = "l",
col = "red", lwd = 3,
ylim = c(1, 3), xlim = c(chl$Date[1], zoopDW$Date[length(zoopDW$Date)]),
ylab = "", xlab = "")