-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRosieZoops.R
410 lines (307 loc) · 16.2 KB
/
RosieZoops.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
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
#Play aroudn with zooplankton data
library(tidyverse)
library(lubridate)
library(zooper)
library(deltamapr)
library(sf)
library(lme4)
library(lmerTest)
# Hypotheses:
# 1. Decreasing X2 will maximize the area of Delta Smelt habitat with appropriate temperatures, turbidity, and salinity, which will result in higher Delta Smelt growth and survival.
# 2. Decreasing X2 will increase biomass of calanoid copepods in the low salinity zone through increased transport of freshwater species from upstream, which will result in higher Delta Smelt growth and survival.
# 3. Operating the SMSCGs during the summer and fall will maximize the duration and area of Delta Smelt habitat with appropriate temperatures, turbidity, and salinity that can be accomplished with 100 TAF of water, which will result in higher Delta Smelt Growth and survival.
# 4. Operating the SMSCGs during the summer and fall will increase biomass of calanoid copepods in Suisun Marsh through increased transport of freshwater species from upstream, which will result in higher Delta Smelt Growth and survival.
# 5. Operating the SMSGs will increase the area of appropriate Delta Smelt Habitat in Grizzly Bay.
# To address each of these hypotheses, we will rely on three primary comparisons:
# 1. Inter-annual comparisons – We will compare constituents during 2023 to conditions in previous wet years with X2 actions (2017, 2019), previous years with SMSCG actions (2018) and dry years with no action (2020-2022).
# 2. Regional comparisons – We expect X2 actions to improve conditions in Suisun Bay. We expect SMSCG actions to improve conditions in Suisun Marsh. Neither action will change conditions in the River, and the River will always be hotter and clearer (less ideal).
#let's pull all the zooplankton data from Jun-October, 2017-present
SMSCGzoops = Zoopsynther(Data_type = "Community", Sources = c("EMP", "FMWT", "STN", "DOP"),
Years = 2017:2022, Size_class = "Meso") %>%
filter(!Undersampled)
limno = Zoopsynther(Data_type = "Community", Sources = c("EMP", "FMWT", "STN", "DOP"),
Years = 2017:2022, Size_class = "Meso") %>%
filter(Taxname %in% c("Limnoithona tetraspina", "Limnoithona_UnID", "Limnoithona sinensis"))
SMSCGzoopsx = bind_rows(SMSCGzoops, limno)
zoop_biomass <- read_csv("./Data/zoop_Copepod and Cladoceran Biomass Values.csv")
#filter the CPUE file so it only has the stations we're interested in
Regions = filter(R_EDSM_Subregions_1718P1, SubRegion %in% c("Suisun Marsh", "Mid Suisun Bay", "Grizzly Bay",
"Lower Sacramento River", "Honker Bay", "Sacramento River near Rio Vista")) %>%
mutate(Region = case_when(SubRegion %in% c("Mid Suisun Bay", "Honker Bay")~ "Suisun Bay",
SubRegion %in% c("Lower Sacramento River", "Sacramento River near Rio Vista") ~ "River",
TRUE ~ SubRegion))
save(Regions, file = "data/SMSCGRegions.RData")
SMSCGzoops2.1 = st_as_sf(filter(SMSCGzoopsx, !is.na(Latitude)), coords = c("Longitude", "Latitude"),
crs = 4326) %>%
st_transform(crs = st_crs(Regions)) %>%
st_join(Regions) %>%
filter(!is.na(SubRegion))
ggplot()+
geom_sf(data=Regions)+
geom_sf(data=SMSCGzoops2, aes(shape = Source))
library(ggspatial)
ggplot()+
geom_sf(data = WW_Delta, color = "grey", fill = "lightskyblue")+
theme_bw()+
coord_sf(ylim = c(38, 38.4), xlim = c(-122.2, -121.5))+
annotation_north_arrow(location = "tl")+
annotation_scale()
SMSCGzoops2 = mutate(SMSCGzoops2.1, Region = case_when(SubRegion %in% c("Mid Suisun Bay", "Honker Bay")~ "Suisun Bay",
SubRegion %in% c("Lower Sacramento River", "Sacramento River near Rio Vista") ~ "River",
TRUE ~ SubRegion),
Month = month(Date),
Taxa = case_when(!is.na(Order) ~ Order,
TRUE ~ Taxname)) %>%
filter(Month %in% c(6:10))
ggplot(SMSCGzoops2, aes(x = Year, y = CPUE, fill = Taxa))+
geom_col()+
scale_fill_brewer(palette = "Dark2")+
facet_wrap(~Region)+
theme_bw()
#Calculate total CPUE# per sample
SMSCGzoopstot = group_by(SMSCGzoops2, SampleID, Month,Region, Year, Station) %>%
st_drop_geometry() %>%
summarize(CPUE = sum(CPUE, na.rm =T))
#calculate average CPUE per taxa
SMSCGzoopsmean = group_by(SMSCGzoops2, SampleID, Month, Taxa, Region, Year) %>%
st_drop_geometry() %>%
summarize(CPUE = sum(CPUE, na.rm =T)) %>%
group_by(Taxa, Region, Year) %>%
summarize(CPUE = mean(CPUE, na.rm =T))
#Plot it a few different ways
ggplot(filter(SMSCGzoopsmean, Year != 2022), aes(x = Year, y = CPUE, fill = Taxa))+
geom_col()+
scale_fill_brewer(palette = "Dark2", labels = c("Calanoid copepods", "Barnacle larvae",
"Cladocera", "Cyclopoid copepods",
"Shrimp and crab larvae", "Harpactacoid copepods"))+
facet_wrap(~Region)+
theme_bw()
ggplot(filter(SMSCGzoopsmean, Year != 2022), aes(x = Region, y = CPUE, fill = Taxa))+
geom_col()+
scale_fill_brewer(palette = "Dark2", labels = c("Calanoid copepods", "Barnacle larvae",
"Cladocera", "Cyclopoid copepods",
"Shrimp and crab larvae", "Harpactacoid copepods"))+
facet_wrap(~Year)+
theme_bw()
# #now some example plots so folks know what I'm talking about
testdata = read_csv("data/Testdata.csv") %>%
mutate(YearType = case_when(YearType %in% c("CD", "Dry")~ "Dry - no action",
TRUE ~ YearType))
ggplot(testdata, aes(x = Year, y = CPUEm, fill = YearType))+
geom_col(color = "black")+
geom_errorbar(aes(ymin = CPUEm -sdcpue, ymax = CPUEm + sdcpue))+
facet_wrap(Scenario~Region)+
theme_bw()+
ylab("Example metric")+
scale_y_continuous(breaks = NULL)+
scale_fill_manual(values = c("orange", "darkred", "lightblue", "palegreen"))+
scale_x_continuous(breaks = c(2017, 2018, 2019, 2020, 2021, 2022, 2023))+
theme(axis.text.x = element_text(angle = 45, hjust =1))+
xlab(NULL)
#################################################################
#convert year to factor
SMSCGzoopstot$Year = as.factor(SMSCGzoopstot$Year)
#run a mixed model with random effect of month
lm1 = lmer(log(CPUE+1)~ Year*Region + (1|Month) , data = SMSCGzoopstot)
summary(lm1)
library(car)
Anova(lm1, type = "III")
#plot the output
library(effects)
plot(allEffects(lm1), x.var = "Region")
#pairwise comparisons
library(emmeans)
emmeans(lm1, pairwise ~ "Region", by = "Year")
emmeans(lm1, pairwise ~ "Year", by = "Region")
emmeans(lm1, pairwise ~ "Year")
#prettier effects plot
effs = allEffects(lm1)[[1]]
effs_df = data.frame(effs$x, SE = effs$se, Fit = effs$fit)
ggplot(effs_df, aes(x = Region, y = Fit)) + geom_point()+
facet_wrap(~Year)+
geom_errorbar(aes(ymin = Fit-SE, ymax = Fit+SE))+ theme_bw()
#############################################
#quick plot of psudodiaptomus versus salinity
SMSCGzoops = Zoopsynther(Data_type = "Community", Sources = c("EMP", "FMWT", "STN", "DOP"),
Years = 2000:2022, Size_class = "Meso") %>%
filter(!Undersampled)
SMSCGzoops2023 = read_csv("Data/SMSCG_CBNet_2018to2023CPUE_07Feb2024.csv") %>%
filter(Year == 2023) %>%
rename(Source = Project) %>%
mutate(Region = case_when(Region %in% c("Suisun Bay",
"Suisun Bay (Honker)", "Honker Bay")~ "Suisun Bay",
Region %in% c("Suisun Bay (Grizzly)", "Grizzly Bay", "Suisun Bay (Mont. Sl.)") ~ "Grizzly Bay",
Region %in% c("SuiMar", "Montezuma Slough")~ "Suisun Marsh",
Region %in% c("Lower Sacramento River", "Confluence")~ "River"),
Date = mdy(Date))
SMSCGzoops2023long = pivot_longer(SMSCGzoops2023, ACARTELA:last_col(), names_to = "taxon", values_to= "CPUE") %>%
filter(taxon %in% c("PDIAPFOR", "PDIAPJUV")) %>%
group_by(Source, Year, Month, Date, Station, Region) %>%
summarize(CPUE = sum(CPUE))
load("Data/SMSCGRegions.RData")
SMSCGzoops2 = st_as_sf(filter(SMSCGzoops, !is.na(Latitude)), coords = c("Longitude", "Latitude"), crs = 4326) %>%
st_transform(crs = st_crs(Regions)) %>%
st_join(Regions) %>%
filter(!is.na(SubRegion)) %>%
st_drop_geometry()
SMSCGzoops2 = mutate(SMSCGzoops2, Region = case_when(SubRegion %in% c("Mid Suisun Bay", "Honker Bay")~ "Suisun Bay",
SubRegion %in% c("Lower Sacramento River", "Sacramento River near Rio Vista") ~ "River",
TRUE ~ SubRegion),
Month = month(Date)) %>%
filter(Month %in% c(6:10), Species == "Pseudodiaptomus forbesi")
yrs = read_csv("data/wtryrtype.csv") %>%
mutate(Year = WY)
SMSCGzoops3 = group_by(SMSCGzoops2, Source, SampleID, Date, Station, Region, SalSurf, Month, Year) %>%
summarize(CPUE = sum(CPUE)) %>%
bind_rows(SMSCGzoops2023long) %>%
left_join(yrs) %>%
mutate(Yrtype = factor(`Yr-type`, levels = c("C", "D", "BN", "AN", "W")))
samples = group_by(SMSCGzoops3, Region, Year) %>%
summarize(N = n())
ggplot(SMSCGzoops3, aes(x = as.factor(Year), y = log(CPUE+1), fill = Yrtype)) + geom_boxplot()+
facet_wrap(~Region)+
xlab("Year - June-October only")+
ylab("P. forbesi log(CPUE+1)")+
scale_fill_manual(values = c("darkred", "orange", "yellow", "lightgreen", "darkblue"),
name = "Year Type")+
theme_bw()+
geom_label(data = samples, aes(x = as.factor(Year),label = N), y =1, inherit.aes = FALSE,
label.padding = unit(0.1, "lines"), size =3)+
theme(axis.text.x = element_text(angle = 90, hjust =1, vjust =0.5),
legend.position = "bottom")
ggsave("Plots/Psudodiaptomus_23years.tiff", device = "tiff", width =10, height =6)
ggplot(SMSCGzoops3, aes(x = SalSurf, y = log(CPUE+1), fill = Yrtype)) + geom_point()+
facet_grid(Month~Region)+
xlab("Salinity")+
ylab("Pseudodiaptomus log(CPUE+1)")+
scale_fill_manual(values = c("darkred", "orange", "yellow", "lightgreen", "darkblue"))+
theme_bw()+
theme(axis.text.x = element_text(angle = 90, hjust =1, vjust =0.5))
ggplot(SMSCGzoops3, aes(x = yday(Date), y = log(CPUE+1), color = SalSurf)) + geom_point()+
facet_wrap(~Region)+
geom_smooth(method = "lm")+
xlab("Day of Year")+
scale_color_viridis_c()+
ylab("Pseudodiaptomus log(CPUE+1)")+
theme_bw()+
theme(axis.text.x = element_text(angle = 90, hjust =1, vjust =0.5))
################################################################################
ggplot(SMSCGzoops3, aes(x = SalSurf, y = log(CPUE+1)))+ geom_point()+
facet_wrap(~Region)+
geom_smooth(method = "lm")+
theme_bw()+
geom_vline(xintercept = 6, linetype =2, color = "darkred")+
xlab("Salinity (psu)")+
ylab("Log Psudodiaptoums CPUE")
# Should i be doing just forbesi? or doe s it matter?
Foo = group_by(SMSCGzoops, Species) %>%
summarize(tot = sum(CPUE))
#######################################################################
#Pascale wants spring zooplankton
SMSCGzoops = Zoopsynther(Data_type = "Community", Sources = c("EMP", "20mm", "FMWT", "STN", "DOP"),
Years = 2000:2022, Size_class = "Meso") %>%
filter(!Undersampled)
#filter the CPUE file so it only has the stations we're interested in
Regions = filter(R_EDSM_Subregions_1718P1, SubRegion %in% c("Suisun Marsh", "Mid Suisun Bay", "Grizzly Bay",
"Lower Sacramento River", "Honker Bay", "Sacramento River near Rio Vista")) %>%
mutate(Region = case_when(SubRegion %in% c("Mid Suisun Bay", "Honker Bay")~ "Suisun Bay",
SubRegion %in% c("Lower Sacramento River", "Sacramento River near Rio Vista") ~ "River",
TRUE ~ SubRegion))
save(Regions, file = "data/SMSCGRegions.RData")
SMSCGzoops2.1 = st_as_sf(filter(SMSCGzoops, !is.na(Latitude)), coords = c("Longitude", "Latitude"),
crs = 4326) %>%
st_transform(crs = st_crs(Regions)) %>%
st_join(Regions) %>%
filter(!is.na(SubRegion))
load("data/Dayflow_allw2023.RData")
outflow = select(Dayflow, Year, Date, OUT)
springzoops = SMSCGzoops2.1 %>%
mutate(Month = month(Date)) %>%
filter(Taxname %in%
c("Pseudodiaptomus forbesi", "Pseudodiaptomus_UnID",
"Cladocera_UnID", "Daphnia_UnID","Daphniidae_UnID")) %>%
st_drop_geometry()
#monthly means
sprzop = springzoops %>%
filter(Region %in% c("Suisun Marsh", "Suisun Bay", "Grizzly Bay")) %>%
group_by(Month, Year, Taxname) %>%
summarize(CPUE = mean(CPUE))
#add outflow
outflowmonthly = outflow %>%
mutate(Month = month(Date)) %>%
group_by(Month, Year) %>%
summarize(OUT = mean(OUT))
springzoops2 = left_join(sprzop, outflowmonthly)
ggplot(filter(springzoops2, Month %in% c(3,4,5)), aes(x = log(OUT), y = log(CPUE), color = Taxname)) + geom_point()+
theme_bw()+
geom_smooth(method = "lm")
ggplot(filter(springzoops2, Month %in% c(6,7,8,9)), aes(x = log(OUT), y = log(CPUE), color = Taxname)) + geom_point()+
theme_bw()+
geom_smooth(method = "lm")
##################################
#spring flow versus summer pseudodiaptomus
sprzop2 = springzoops %>%
filter(Region %in% c("Suisun Marsh", "Suisun Bay", "Grizzly Bay"), Month %in% c(6,7,8,9),
Taxname == "Pseudodiaptomus forbesi") %>%
group_by(Year, Taxname) %>%
summarize(CPUEm = mean(CPUE), sdcpue = sd(CPUE), secpue = sdcpue/3)
outyearly = filter(outflowmonthly, Month %in% c(2,3,4,5)) %>%
group_by(Year) %>%
summarize(SpringOut = mean(OUT))
summerpseudo = left_join(sprzop2, outyearly)
ggplot(summerpseudo, aes(x = log(SpringOut), y = log(CPUEm))) + geom_point()+
geom_smooth(method = "lm")+
geom_errorbar(aes(ymin = log(CPUEm -secpue ), ymax = log(CPUEm + secpue)))+
theme_bw()+
ylab("Log-Transformed Pseudodiaptomus CPUE")+
xlab("Log-Transformed mean Feb-May Delta Outflow")
#OK, now spring flow versus spring aboundacne
sprzop3 = springzoops %>%
filter(Region %in% c("Suisun Marsh", "Suisun Bay", "Grizzly Bay"), Month %in% c(2,3,4,5),
Taxname == "Pseudodiaptomus forbesi") %>%
group_by(Year, Taxname) %>%
summarize(CPUEm = mean(CPUE), sdcpue = sd(CPUE), secpue = sdcpue/3) %>%
left_join(outyearly)
ggplot(sprzop3, aes(x = log(SpringOut), y = log(CPUEm))) + geom_point()+
geom_smooth(method = "lm")+
geom_errorbar(aes(ymin = log(CPUEm -secpue ), ymax = log(CPUEm + secpue)))+
theme_bw()+
ylab("Log-Transformed Spring Pseudodiaptomus CPUE")+
xlab("Log-Transformed mean Feb-May Delta Outflow")
psduedo = filter(springzoops, Genus == "Pseudodiaptomus")
ggplot(psduedo, aes(x = Month, y = log(CPUE+1)))+
geom_col()+
facet_wrap(~Lifestage)
#break out by life stage
sprzop2 = springzoops %>%
filter(Region %in% c("Suisun Marsh", "Suisun Bay", "Grizzly Bay"), Month %in% c(6,7,8,9),
Genus == "Pseudodiaptomus") %>%
group_by(Year, Taxname, Lifestage) %>%
summarize(CPUEm = mean(CPUE), sdcpue = sd(CPUE), secpue = sdcpue/3)
outyearly = filter(outflowmonthly, Month %in% c(2,3,4,5)) %>%
group_by(Year) %>%
summarize(SpringOut = mean(OUT))
summerpseudo = left_join(sprzop2, outyearly)
ggplot(summerpseudo, aes(x = log(SpringOut), y = log(CPUEm))) + geom_point()+
geom_smooth(method = "lm")+
geom_errorbar(aes(ymin = log(CPUEm -secpue ), ymax = log(CPUEm + secpue)))+
facet_wrap(~Lifestage)+
theme_bw()+
ylab("Log-Transformed Summer Pseudodiaptomus CPUE")+
xlab("Log-Transformed mean Feb-May Delta Outflow")
sprzop3 = springzoops %>%
filter(Region %in% c("Suisun Marsh", "Suisun Bay", "Grizzly Bay"), Month %in% c(2:5),
Genus == "Pseudodiaptomus") %>%
group_by(Year, Taxname, Lifestage) %>%
summarize(CPUEm = mean(CPUE), sdcpue = sd(CPUE), secpue = sdcpue/3)
outyearly = filter(outflowmonthly, Month %in% c(2,3,4,5)) %>%
group_by(Year) %>%
summarize(SpringOut = mean(OUT))
summerpseudo = left_join(sprzop3, outyearly)
ggplot(summerpseudo, aes(x = log(SpringOut), y = log(CPUEm))) + geom_point()+
geom_smooth(method = "lm")+
geom_errorbar(aes(ymin = log(CPUEm -secpue ), ymax = log(CPUEm + secpue)))+
facet_wrap(~Lifestage)+
theme_bw()+
ylab("Log-Transformed Spring Pseudodiaptomus CPUE")+
xlab("Log-Transformed mean Feb-May Delta Outflow")