-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStatistical analyses_McLaskey2023.R
1796 lines (1322 loc) · 72.3 KB
/
Statistical analyses_McLaskey2023.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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# 2023-02-21 pulling all stats for McLaskey et al. 2023 paper together
library(tidyverse)
library(ggfortify)
library(vegan)
library(Hmisc)
library(corrplot)
library(cowplot)
library(pairwiseAdonis)
allData <- read.csv("processed_data/QU39 2015 zoop POM FA SI Chl biomass 20231102.csv") # keeping 5/11 250 net samples in
str(allData)
allData$Date <- as.Date(allData$Date, "%Y-%m-%d")
allData <- allData %>% filter(Date < as.Date("2015-12-31"))
allData$Month <- format(as.Date(allData$Date), "%m")
allData$Month <- as.factor(allData$Month)
allData <- allData %>% mutate(season = ifelse( Month %in% c("03","04","05"), "spring" ,
ifelse(Month %in% c("06", "07", "08"), "summer",
ifelse(Month %in% c("09", "10", "11"), "fall",
"winter"))))
allData$season <- factor(allData$season, levels = c("spring", "summer","fall", "winter"))
allData$Size.Fraction <- as.factor(allData$Size.Fraction)
levels(allData$Size.Fraction) # shows the different factor levels
allData$Size.Fraction <- factor(allData$Size.Fraction, levels = c("2000", "1000","500", "250", "125","64", "POM"))
# Modify POM dates to match zoop collection dates ------------------------------------------------------
# To make comparisons between POM parameters and zooplankton,
# I need to modify POM dates to match the closest zooplankton collection
# And I need to manually change some POM dates to be able to match up w zoop collections
# All are within two days, except the last sampling
allData$Date <- as.character(allData$Date)
allData <- allData %>% mutate(Date2 = ifelse(Date=="2015-04-14", "2015-04-15",
ifelse(Date=="2015-05-26", "2015-05-27",
ifelse(Date=="2015-07-07", "2015-07-06",
ifelse(Date=="2015-08-12", "2015-08-10",
ifelse(Date=="2015-09-09", "2015-09-10",
ifelse(Date=="2015-10-06", "2015-10-05",
ifelse(Date=="2015-11-03", "2015-11-02",
ifelse(Date=="2015-12-11", "2015-11-30", # this is the greatest different between collections
ifelse(Date=="2015-03-18", "2015-03-17",
ifelse(Date=="2015-06-15", "2015-06-16",
ifelse(Date=="2015-06-24", "2015-06-22",
ifelse(Date=="2015-05-13", "2015-05-11",
(Date))))))))))))))
allData$Date <- as.Date(allData$Date, "%Y-%m-%d")
# remove samples with no data
fatty.acid.all <- allData[!is.na(allData$C16.0_PERCENT),]
# create version of zooplankton only
QU39.2015.64 <- fatty.acid.all[fatty.acid.all$Size.Fraction!="POM",]
# Fig 1 - Chl and Zoop Biomass ---------------------------------------------
# *Chlorophyll ------------------------------------------------------------
Chl.2015 <- allData %>% select(Date, chl_GF.F, chl_20um, chl_3um) %>%
filter(Date < as.Date("2015-12-31") & Date > as.Date("2015-01-01"))
# Chl size class concentrations
chlSumm.concs.small <- allData %>% select(Date, chl_GF.F, chl_20um, chl_3um)
chlSumm.concs.small.long <- chlSumm.concs.small %>% pivot_longer(-Date, names_to = "Size.class", values_to = "Conc")
chlSumm.concs.small.long$Size.class <- as.factor(chlSumm.concs.small.long$Size.class)
ord_size_class <- c( "chl_GF.F", "chl_3um", "chl_20um")
chlSumm.concs.small.long<- chlSumm.concs.small.long %>% mutate(Size.class = factor(Size.class,
levels = ord_size_class))
pChl.allconcs <- ggplot(chlSumm.concs.small.long, aes(Date, Conc, fill=Size.class)) + geom_area() +
theme_bw()+
scale_fill_manual(values=c("#c2e699","#78c679", "#006837"),
labels =c("0.7", "3", "20")) +
labs(x=element_blank(), y=expression(paste("Chl a (",mu,"g L"^-1*")")), fill="Size Class") +
theme(panel.grid.minor=element_blank(),
legend.position = "none",
axis.text.x = element_blank(),
axis.title.x = element_blank(),
panel.border = element_rect(linewidth = 0.3)) +
scale_x_date(limits=c(as.Date("2015-01-01"), as.Date("2015-12-31")),
expand=c(0,0)) +
scale_y_continuous(expand=c(0,0)) +
geom_text(label="(b)", x=as.Date("2014-11-20"), y=19.5, color="black") +
coord_cartesian(clip = "off")
pChl.allconcs
# plot for Chl legend
pnew <- ggplot(chlSumm.concs.small.long, aes(Date, Conc, fill=Size.class)) + geom_area() +
scale_fill_manual(values=c("#c2e699","#78c679", "#006837"),
labels = c("0.7", "3", "20")) +
labs(fill="Size")
pnew
legend.Chl <- get_legend(
# create some space to the left of the legend
pnew + theme(legend.title = element_text(size=9),
legend.text = element_text(size = 7),
legend.position = c(0.45,0.6))
)
# *Zooplankton Biomass -------------------------------------------------------------------------
zoop.biomass2015 <- allData %>% select(Date, Size.Fraction, Biomass.mg.m3)
zoop.biomass2015 <- zoop.biomass2015[!is.na(zoop.biomass2015$Biomass.mg.m3),]
zoop.biomass2015$Size.Fraction <- factor(zoop.biomass2015$Size.Fraction, levels = c("64", "125","250", "500", "1000","2000" ))
pZoop.biomass <- ggplot(zoop.biomass2015, aes(x=Date, y=Biomass.mg.m3, fill=Size.Fraction)) + geom_area() +
theme_bw() +
labs(y=expression(paste("Biomass (mg m"^-3*")")),
x=element_blank()) +
theme(panel.grid.minor=element_blank(),
axis.title.x = element_blank(),
axis.text.x = element_blank(),
legend.position = "none",
panel.border = element_rect(linewidth = 0.3) ) +
scale_fill_manual(values = c("#47ebb4", "#66ccff","#9966ff",
"#db5764", "#ff7433", "#ffbf00")) +
scale_x_date(limits=c(as.Date("2015-01-01"), as.Date("2015-12-31")),
expand=c(0,0)) +
scale_y_continuous(expand=c(0,0)) +
geom_text(label="(d)", x=as.Date("2014-11-20"), y=66, color="black") +
coord_cartesian(clip = "off")
pZoop.biomass
pZoop.props <- ggplot(zoop.biomass2015, aes(x=Date, y=Biomass.mg.m3, fill=Size.Fraction)) + geom_area(position = "fill") +
labs(y="Biomass proportion", x=element_blank()) +
theme_bw() +
theme(panel.grid.minor=element_blank(),
legend.position = "none" ,
panel.border = element_rect(linewidth = 0.3)) +
scale_fill_manual(values = c("#47ebb4", "#66ccff","#9966ff",
"#db5764", "#ff7433", "#ffbf00")) +
scale_x_date(limits=c(as.Date("2015-01-01"), as.Date("2015-12-31")),
expand=c(0,0)) +
scale_y_continuous(expand=c(0,0), labels = c("0", "25%", "50%", "75%", "")) +
geom_text(label="(e)", x=as.Date("2014-11-20"), y=0.98, color="black") +
coord_cartesian(clip = "off")
pZoop.props
# plot for legend
pnew2 <- ggplot(zoop.biomass2015, aes(x=Date, y=Biomass.mg.m3, fill=Size.Fraction)) + geom_area(position = "fill") +
theme_bw() +
scale_fill_manual(values = c("#47ebb4", "#66ccff","#9966ff",
"#db5764", "#ff7433", "#ffbf00")) +
labs(fill= "Size")
pnew2
legend.Zoop <- get_legend(
# create some space to the left of the legend
pnew2 + theme(legend.title = element_text(size=9),
legend.text = element_text(size = 7),
legend.position = c(0.45,0.6))
)
png("Biomass time series Fig 2 20230310.png", width=190, height=110, units="mm", res=300)
plot_grid(pChl.allconcs, pChl.allProps, legend.Chl,
pZoop.biomass, pZoop.props, legend.Zoop,
rel_widths = c(1.5, 1.5, 0.3), nrow=2,
rel_heights = c(0.9, 1))
dev.off()
tiff("Biomass time series Fig 2 20230310.tiff", width=190, height=110, units="mm", res=300)
plot_grid(pChl.allconcs, pChl.allProps, legend.Chl,
pZoop.biomass, pZoop.props, legend.Zoop,
rel_widths = c(1.5, 1.5, 0.3), nrow=2,
rel_heights = c(0.9, 1))
dev.off()
# *POM FA time series ------------------------------------------------------
QU39.2015.POM <- fatty.acid.all[fatty.acid.all$Size.Fraction=="POM",]
p1 <- ggplot(QU39.2015.POM, aes(x=Date, y=SumFA_ug.L, color=Size.Fraction, fill=Size.Fraction)) +
geom_point(size=2) + theme_bw() +
scale_color_manual(values = c("#734f22")) +
scale_x_date( limits = as.Date(c("2015-01-01", "2015-12-31")),
expand = c(0,0)) +
scale_y_continuous(position="right",
expand = c(0,0),
limits = c(0,71)) +
theme(legend.position = "none",
panel.grid.minor=element_blank(),
panel.grid.major=element_blank(),
axis.text.x = element_blank(),
panel.border = element_blank(),
axis.text.y = element_text(size = 14)) +
labs(y = "", x="") +
theme(panel.background = element_rect(fill = "transparent", colour = NA),
plot.background = element_rect(fill = "transparent", colour = NA))
p1
ggsave(
plot = p1,
filename = "POM FA time series 3 no smooth 20230916 .png",
bg = "transparent"
)
QU39.2015.POM$Size.Fraction <- "POM fatty acids"
p2 <- ggplot(QU39.2015.POM, aes(x=Date, y=SumFA_ug.L, color=Size.Fraction, fill=Size.Fraction)) +
geom_point() + theme_bw() +
scale_color_manual(values = c("#734f22"),
labels = "POM FA") +
scale_x_date( limits = as.Date(c("2015-01-01", "2015-12-31")),
expand = c(0,0)) +
scale_y_continuous(position="right",
expand = c(0,0),
limits = c(0,71)) +
theme(panel.grid.minor=element_blank(),
panel.grid.major=element_blank(),
axis.text.x = element_blank(),
panel.border = element_blank(),
axis.text.y = element_text(size = 14)) +
labs(y = "", x="",
color = expression(paste("POM FA (",mu,"g L"^-1*")")))
p2
legend.TFA <- get_legend(
# create some space to the left of the legend
p2 + theme(legend.title = element_text(size=9),
legend.text = element_text(size = 7),
legend.position = c(0.45,0.6))
)
# Trophic position --------------------------------------------------------
# Need to calculate 14 day running average of POM Del15N
allData.sm <- allData %>% select(Date, Size.Fraction, delta15n)
allData.sm <- allData.sm[complete.cases(allData.sm$delta15n),]
allData.sm.agg = aggregate(allData.sm[,3],
by= list(allData.sm$Date, allData.sm$Size.Fraction),
FUN = mean, na.omit=TRUE)
colnames(allData.sm.agg) <- c("Date", "Size.Fraction", "delta15n")
allData.sm.wide <- allData.sm.agg %>% pivot_wider(values_from = delta15n, names_from = Size.Fraction)
# Going to use JDay to calculate window rather than dates
allData.sm.wide$JDay <- format(as.Date(allData.sm.wide$Date), "%j")
allData.sm.wide$JDay <- as.numeric(allData.sm.wide$JDay)
# make 2014 dates negative for moving window calcs
allData.sm.wide$JDay[allData.sm.wide$Date < as.Date("2015-01-01")] <- allData.sm.wide$JDay[allData.sm.wide$Date < as.Date("2015-01-01")] - 365
# CAlculate 14 day moving average of POM DelN15
allData.sm.wide$meanPOM7 <- NA
allData.sm.wide$meanPOM14 <- NA
allData.sm.wide$meanPOM21 <- NA
allData.sm.wide$meanPOM28 <- NA
for(i in 1:nrow(allData.sm.wide)){
allData.sm.wide$meanPOM7[i] <- mean(allData.sm.wide$POM[allData.sm.wide$JDay >= (allData.sm.wide$JDay[i]-7) &
allData.sm.wide$JDay<=allData.sm.wide$JDay[i] ], na.rm = T)
allData.sm.wide$meanPOM14[i] <- mean(allData.sm.wide$POM[allData.sm.wide$JDay >= (allData.sm.wide$JDay[i]-14) &
allData.sm.wide$JDay<=allData.sm.wide$JDay[i] ], na.rm = T)
allData.sm.wide$meanPOM21[i] <- mean(allData.sm.wide$POM[allData.sm.wide$JDay >= (allData.sm.wide$JDay[i]-21) &
allData.sm.wide$JDay<=allData.sm.wide$JDay[i] ], na.rm = T)
allData.sm.wide$meanPOM28[i] <- mean(allData.sm.wide$POM[allData.sm.wide$JDay >= (allData.sm.wide$JDay[i]-28) &
allData.sm.wide$JDay<=allData.sm.wide$JDay[i] ], na.rm = T)
}
# Use average of 2014-12-16 and 2015-01-13 POM for 2015-01-06 baseline
allData.sm.wide$meanPOM14[allData.sm.wide$Date==as.Date("2015-01-06")] <-
mean(c(allData.sm.wide$POM[allData.sm.wide$Date==as.Date("2014-12-16")],
allData.sm.wide$POM[allData.sm.wide$Date==as.Date("2015-01-13")])
)
# Calculate trophic position after El-Sabaawi et al. 2009
allData.sm.wide$TP.2000 <- ((allData.sm.wide$`2000` - allData.sm.wide$meanPOM14) / 3.4) +1
allData.sm.wide$TP.1000 <- ((allData.sm.wide$`1000` - allData.sm.wide$meanPOM14)/ 3.4) +1
allData.sm.wide$TP.500 <- ((allData.sm.wide$`500` - allData.sm.wide$meanPOM14)/ 3.4) +1
allData.sm.wide$TP.250 <- ((allData.sm.wide$`250` - allData.sm.wide$meanPOM14)/ 3.4) +1
allData.sm.wide$TP.125 <- ((allData.sm.wide$`125` - allData.sm.wide$meanPOM14)/ 3.4) +1
allData.sm.wide$TP.64 <- ((allData.sm.wide$`64` - allData.sm.wide$meanPOM14)/ 3.4) +1
allData.sm.long <- allData.sm.wide %>% pivot_longer(TP.2000:TP.64, names_to = "Size", values_to = "TP")
allData.sm.long$Size <- factor(allData.sm.long$Size, levels = c("TP.2000", "TP.1000","TP.500", "TP.250", "TP.125","TP.64"))
allData.sm.wide.smaller <- allData.sm.wide %>% select(Date, TP.2000:TP.64)
allData.smaller.long <- allData.sm.wide.smaller %>% pivot_longer(TP.2000:TP.64, names_to = "Size", values_to = "TP")
allData.smaller.long$Size <- substring(allData.smaller.long$Size, 4,7)
colnames(allData.smaller.long)[2] <- "Size.Fraction"
QU39.2015.64 <- full_join(allData.smaller.long, QU39.2015.64)
# *Corr plot TP vs FATM ---------------------------------------------------------------
QU39.2015.64$SFA.PUFA <- QU39.2015.64$percent.SFA / QU39.2015.64$percent.PUFA
QU39.2015.64.smer <- QU39.2015.64 %>% select(Date, Size.Fraction, SFA.PUFA, Carn18.1N9_n7, DHA.EPA)
QU39.2015.64.agg = aggregate(QU39.2015.64.smer[,c(3:5)],
by = list(QU39.2015.64.smer$Date, QU39.2015.64.smer$Size.Fraction),
FUN = mean, na.rm=TRUE)
colnames(QU39.2015.64.agg)[1:2] <- c("Date", "Size.Fraction")
QU39.2015.64.agg <- QU39.2015.64.agg[!is.nan(QU39.2015.64.agg$SFA.PUFA),]
QU39.2015.64.wide <- QU39.2015.64.agg %>% pivot_wider(names_from = Size.Fraction, values_from = c(SFA.PUFA, Carn18.1N9_n7,
DHA.EPA))
colnames(QU39.2015.64.wide)[2:7] <- paste0("FATM_", colnames(QU39.2015.64.wide)[2:7])
QU39.2015.64.smer2 <- QU39.2015.64 %>% select(Date, Size.Fraction, TP)
QU39.2015.64.agg2 = aggregate(QU39.2015.64.smer2[,c(3)],
by = list(QU39.2015.64.smer2$Date, QU39.2015.64.smer2$Size.Fraction),
FUN = mean, na.rm=TRUE)
colnames(QU39.2015.64.agg2) <- c("Date", "Size.Fraction", "TP")
QU39.2015.64.agg2 <- QU39.2015.64.agg2[!is.nan(QU39.2015.64.agg2$TP),]
QU39.2015.64.wide2 <- QU39.2015.64.agg2 %>% pivot_wider(names_from = Size.Fraction, values_from = TP)
colnames(QU39.2015.64.wide2)[2:7] <- paste0("TP_", colnames(QU39.2015.64.wide2)[2:7])
QU39.2015.64.wide.all <- full_join(QU39.2015.64.wide, QU39.2015.64.wide2)
resTP <- rcorr(as.matrix(QU39.2015.64.wide.all[c(2:25)]), type = "spearman")
corrplot(resTP$r, type="lower", method="number",
p.mat = resTP$P, sig.level = 0.0028)
# write.csv(as.table(resTP$P), "TP corr p values.csv")
# Look at normality of variables
for(i in 2:25){
temp <- as.matrix(QU39.2015.64.wide.all[complete.cases(QU39.2015.64.wide.all[i]),i])
qqnorm(temp, main = colnames(QU39.2015.64.wide.all)[i])
qqline(temp)
}
# del13C correlation ------------------------------------------------------
# aggregate the multiple zooplankton nets (two 64 um nets done on same day) before I can pivot
allSIdatasm2 <- allData %>% select( Date2, Size.Fraction, delta13c)
allSIdatasm2 <- allSIdatasm2[!is.na(allSIdatasm2$delta13c),]
SI.agg = aggregate(allSIdatasm2[,c(3)],
by = list(allSIdatasm2$Date2, allSIdatasm2$Size.Fraction),
FUN = mean, na.omit=TRUE)
colnames(SI.agg) <- c("Date", "Size.Fraction", "delta13c")
SI.agg.wide <- SI.agg %>% pivot_wider(names_from = Size.Fraction, values_from = delta13c)
res2<-rcorr(as.matrix(SI.agg.wide[2:8]), type = "spearman")
corrplot(res2$r, type="full", method="number",
p.mat = res2$P, sig.level = 0.05, tl.col = "black",
title = "delta13c")
# Look at normality of variables
for(i in 2:8){
temp <- as.matrix(SI.agg.wide[complete.cases(SI.agg.wide[i]),i])
qqnorm(temp, main = colnames(SI.agg.wide)[i])
qqline(temp)
}
# Look at normality of variables
write.csv(as.table(res2$r), "Del13C corr R values.csv")
# FATM correlations -------------------------------------------------------
# All FATM to test
# C16PUFA, percent.SFA
# C18.1n.7_PERCENT, C18.1n.9c_PERCENT, C18.3n.3_PERCENT, C18.4n.3_PERCENT,
# C20.5n.3_PERCENT, C22.6n.3_PERCENT, Bacteria_15_17, C20.4n.6_PERCENT
fatty.acid.smer <- fatty.acid.all %>% select(Date2, Size.Fraction, C20.4n.6_PERCENT)
# Galloway and Winder 2015 arcsine-square root transformed the FA proportions
asinTransform <- function(p) { asin(sqrt(p)) }
# arcsine transform FA props
fatty.acid.smer[,3] <- asinTransform(fatty.acid.smer[,3])
fatty.acid.agg = aggregate(fatty.acid.smer[,c(3)],
by = list(fatty.acid.all$Date2, fatty.acid.all$Size.Fraction),
FUN = mean, na.rm=TRUE)
colnames(fatty.acid.agg) <- c("Date", "Size.Fraction", "FA")
fatty.acid.wide <- fatty.acid.agg %>% pivot_wider(names_from = Size.Fraction, values_from = FA)
res2 <- rcorr(as.matrix(fatty.acid.wide[2:8]), type = "spearman")
corrplot(res2$r, type="lower", method="number",
p.mat = res2$P, sig.level = 0.05, insig = "blank")
# Look at normality of variables
for(i in 2:8){
temp <- as.matrix(fatty.acid.wide[complete.cases(fatty.acid.wide[i]),i])
qqnorm(temp)
qqline(temp)
}
write.csv(as.table(res2$r), "ARA corr R2 values.csv")
# Fig 4 formatted corr tables ----------------------------------------------------
TPcorrtable <- read.csv("processed_data/trophic position correlation table.csv", stringsAsFactors = FALSE)
str(TPcorrtable)
# columns with the R2 values to base color shading on
TPcorrtable_r <- TPcorrtable %>% select(FATM, r.64:r.2000)
TPcorrtable_r_long <- TPcorrtable_r %>% pivot_longer(r.64:r.2000, names_to = "Size", values_to = "R")
# columns with what I want printed in the cells
TPcorrtable_label <- TPcorrtable %>% select(FATM, z.64:z.2000)
# Make colnames match table of R values so I can bind them later
colnames(TPcorrtable_label)[2:7] <- colnames(TPcorrtable_r)[2:7]
TPcorrtable_label_long <- TPcorrtable_label %>% pivot_longer(r.64:r.2000, names_to = "Size",
values_to = "Label",
values_transform = as.character)
TPcorrtable_all_long <- full_join(TPcorrtable_r_long, TPcorrtable_label_long)
# column names for variables I want on horizontal side
TPcorrtable_all_long$FATM
mylevels1 <- c("SFA/PUFA", "18:1n-9 / 18:1n-7", "DHA:EPA")
mylevels2 <- c("r.64", "r.125" , "r.250","r.500","r.1000", "r.2000")
TPcorrtable_all_long <- TPcorrtable_all_long %>% filter(FATM %in% mylevels1)
# reorder the variables to how I want them
TPcorrtable_all_long$FATM <- ordered(TPcorrtable_all_long$FATM, levels = mylevels1)
TPcorrtable_all_long$Size <- ordered(TPcorrtable_all_long$Size, levels = mylevels2)
# label names I actually want to use for variables on horizontal side
labels1 <- c(expression(paste("64 ",mu,"m")),
expression(paste("125 ",mu,"m")),
expression(paste("250 ",mu,"m")),
expression(paste("500 ",mu,"m")),
expression(paste("1000 ",mu,"m")),
expression(paste("2000 ",mu,"m")))
# label names I actually want to use for variables on horizontal side
labels3 <- c("DHA:EPA", expression(paste("18:1",omega,"9 / 18:1",omega,"7")),"SFA / PUFA" )
#labels2 <- c("SFA/PUFA" , expression(paste("18:1",omega,"9 / 18:1",omega,"7")),"DHA:EPA")
mycolors <- c( "#47ebb4", "#66ccff", "#9966ff",
"#db5764","#ff7433", "#ffbf00")
# slightly modified
mycolors <- c( "#3bc496", "#51a5cf", "#9966ff",
"#db5764","#ff7433", "#ffbf00")
mycolors <- c( "#3bc496", "#51a5cf", "#9966ff",
"#db5764","#ff7433", "#e8aa00")
# Making the plot
pCorr2 <- TPcorrtable_all_long %>%
ggplot(aes(Size, FATM, fill=R, label=Label), linewidth=2) +
geom_tile() +
labs(x = NULL, y = NULL, fill = "Spearman's\nCorrelation") +
scale_fill_gradient2(mid="#ffffff",low=paste("red"),high=paste("blue"), limits=c(-1,1), na.value="ivory2") +
geom_text() +
theme_classic() +
scale_x_discrete(expand=c(0,0), position = "top", labels=(labels1)) +
scale_y_discrete(expand=c(0,0),
labels=rev(labels3)) +
theme(legend.position = "none",
axis.text.y = element_text(hjust = 0, size= 11),
axis.text.x = element_text(colour = mycolors, size= 13)) +
geom_text(label="(b)", x=-0.8, y=3.69, color="black") +
coord_cartesian(clip = "off")
#png("TP correlations Fig 6 20231109.png", width=150, height=45, units="mm", res=300)
pCorr2
#dev.off()
# POM FATM correlation table
POMcorrtable <- read.csv("processed_data/POM FATM correlation table.csv", stringsAsFactors = FALSE)
str(POMcorrtable)
POMcorrtable$z.1000 <- as.character(POMcorrtable$z.1000)
# remove 18:1n-7
POMcorrtable <- POMcorrtable[-7,]
# columns with the R2 values to base color shading on
POMcorrtable_r <- POMcorrtable %>% select(FATM, r.64:r.2000)
POMcorrtable_r_long <- POMcorrtable_r %>% pivot_longer(r.64:r.2000, names_to = "Size", values_to = "R")
# columns with what I want printed in the cells
POMcorrtable_label <- POMcorrtable %>% select(FATM, z.64:z.2000)
# Make colnames match table of R values so I can bind them later
colnames(POMcorrtable_label)[2:7] <- colnames(POMcorrtable_r)[2:7]
POMcorrtable_label_long <- POMcorrtable_label %>% pivot_longer(r.64:r.2000, names_to = "Size",
values_to = "Label",
values_transform = as.character)
POMcorrtable_all_long <- full_join(POMcorrtable_r_long, POMcorrtable_label_long)
# column names for variables I want on horizontal side
POMcorrtable_all_long$FATM
mylevels1 <- c( "SFA", "DHA", "EPA", "C16 PUFAs ", "SDA (18:4w4)", "ALA (18:3w3)","?13C ")
mylevels2 <- c("r.64", "r.125" , "r.250","r.500","r.1000", "r.2000")
# POMcorrtable_all_long <- POMcorrtable_all_long %>% filter(FATM %in% mylevels1)
# reorder the variables to how I want them
POMcorrtable_all_long$FATM <- ordered(POMcorrtable_all_long$FATM, levels = mylevels1)
POMcorrtable_all_long$Size <- ordered(POMcorrtable_all_long$Size, levels = mylevels2)
# label names I actually want to use for variables on horizontal side
labels1 <- c(expression(paste("64 ",mu,"m")),
expression(paste("125 ",mu,"m")),
expression(paste("250 ",mu,"m")),
expression(paste("500 ",mu,"m")),
expression(paste("1000 ",mu,"m")),
expression(paste("2000 ",mu,"m")))
# label names I actually want to use for variables on horizontal side
labels2 <- c(expression(paste(delta^{13}, "C")),
expression(paste("ALA (18:3",omega,"3) ", italic("chlorophytes"))),
expression(paste("SDA (18:3",omega,"3)", italic("cryptophytes"))),
expression(paste("C16 PUFAs\n", italic("diatoms"))),
expression(paste("EPA\n", italic("diatoms"))),
expression(paste("DHA\n", italic("dinoflagelates"))),
"18:1\u03C97\nchlorophytes")
labels2 <- c(expression(paste(delta^{13}, "C")),
"ALA (18:3\u03C93)\nchlorophytes",
"SDA (18:3\u03C94)\ncryptophytes",
"C16 PUFAs\ndiatoms",
"EPA\ndiatoms",
"DHA\ndinoflagelates",
"SFA")
# Making the plot
mycolors <- c( "#47ebb4", "#66ccff", "#9966ff",
"#db5764","#ff7433", "#ffbf00")
# slightly modified
mycolors <- c( "#3bc496", "#51a5cf", "#9966ff",
"#db5764","#ff7433", "#e8aa00")
pCorrPOM <- POMcorrtable_all_long %>%
ggplot(aes(Size, FATM, fill=R, label=Label), linewidth=2) +
geom_tile() +
labs(x = NULL, y = NULL, fill = "Spearman's\nCorrelation") +
scale_fill_gradient2(mid="#ffffff",low=paste("red"),high=paste("blue"), limits=c(-1,1), na.value="ivory2") +
geom_text() +
theme_classic() +
scale_x_discrete(expand=c(0,0), position = "top", labels=(labels1)) +
scale_y_discrete(expand=c(0,0),
labels=rev(labels2), ) +
theme(legend.position = "none",
axis.text.y = element_text(hjust = 0, vjust = 1,
colour = "#089908", size= 12, face="bold"),
axis.text.x = element_text(colour = mycolors, size= 13, face="bold")) +
geom_text(label="(a)", x=-0.78, y=7.8, color="black") +
coord_cartesian(clip = "off")
#png("POM correlations Fig 5 20221202.png", width=150, height=100, units="mm", res=300)
pCorrPOM
#dev.off()
# Make combined version w both as two panels, with a legend for fill color
pCorrLegend <- POMcorrtable_all_long %>%
ggplot(aes(Size, FATM, fill=R, label=Label), linewidth=2) +
scale_fill_gradient2(mid="#ffffff",low=paste("red"),high=paste("blue"), limits=c(-1,1), na.value="ivory2") +
geom_tile() +
labs(fill="correlation\ncoefficient")
# extract the legend from the montly PCA
corrLegend <- get_legend(
# create some space to the left of the legend
pCorrLegend + theme(legend.title = element_text(size=9),
legend.text = element_text(size = 7),
legend.position = c(0.5, 0.3))
)
png("Combined POM correlations Fig 4 20231114.png", width=180, height=130, units="mm", res=300)
plot_grid(pCorrPOM,corrLegend,pCorr2 ,
rel_widths = c(1.5, 0.2), nrow=2,
rel_heights = c(2, 1),
align = "v",
axis = "l")
dev.off()
tiff("Combined POM correlations Fig 4 20231129.tif", width=180, height=130, units="mm", res=300)
plot_grid(pCorrPOM,corrLegend,pCorr2 ,
rel_widths = c(1.5, 0.2), nrow=2,
rel_heights = c(2, 1),
align = "v",
axis = "l")
dev.off()
# *Fig 2 ------------------------------------------------------------------
ptrophicP <- ggplot(allData.sm.long, aes(x=Date, y=TP, color=Size, fill=Size)) +
geom_point() + theme_classic() + geom_smooth(se=F, span=0.75) +
scale_color_manual(values = c("#ffbf00", "#ff7433","#db5764",
"#9966ff", "#66ccff", "#47ebb4", "#00b300")) +
scale_fill_manual(values = c("#ffbf00", "#ff7433","#db5764",
"#9966ff", "#66ccff", "#47ebb4", "#00b300")) +
labs(y="Trophic Position", x="", color="Size class" , fill="Size class") +
scale_x_date(expand = c(0,0),
breaks = as.Date(c("2015-01-01", "2015-04-01", "2015-07-01",
"2015-10-01", "2016-01-01")),
labels = c("Jan", "Apr", "Jul", "Oct", "Jan"),
limits = as.Date(c("2015-01-01", "2015-12-31"))) +
theme(legend.position = "none",
plot.margin = unit(c(0.1, 0.2, 0, 0.2), "cm"),
panel.grid.minor = element_blank(),
axis.text = element_text(size=9),
axis.title = element_text(size=9)) +
geom_text(label="(c)", x=as.Date("2014-11-13"), y=3, color="black") +
coord_cartesian(clip = "off")
ptrophicP
pDel13C <- ggplot(allData, aes(x=Date, y=delta13c, color=Size.Fraction, fill=Size.Fraction)) +
geom_point() + theme_classic() + geom_smooth(se=F) +
scale_color_manual(values = c("#ffbf00", "#ff7433","#db5764",
"#9966ff", "#66ccff", "#47ebb4", "#00b300")) +
scale_fill_manual(values = c("#ffbf00", "#ff7433","#db5764",
"#9966ff", "#66ccff", "#47ebb4", "#00b300")) +
labs(y=expression(paste(delta^{13}, "C")), x="", color="" , fill="", size=8) +
scale_x_date(expand = c(0,0),
limits = as.Date(c("2015-01-01", "2015-12-31"))) +
theme(legend.position = c(0.32,0.3), legend.text = element_text(size = 7),
legend.background = element_rect(fill='transparent'), #transparent legend bg
plot.margin = unit(c(0.1, 0, -0.3, 0.2), "cm"),
panel.grid.minor = element_blank(),
axis.text.x = element_blank(),
axis.text = element_text(size=9),
axis.title = element_text(size=9),
legend.key.height = unit(0.35, 'cm')) +
geom_text(label="(b)", x=as.Date("2014-11-13"), y=-18, color="black") +
coord_cartesian(clip = "off")
pDel13C
pPOMFA <- ggplot(fatty.acid.all, aes(x=Date, y=SumFA_ug.L, color=Size.Fraction, fill=Size.Fraction)) +
geom_point() + theme_classic() + geom_smooth(se=F) +
scale_color_manual(values = c("#ffbf00", "#ff7433","#db5764",
"#9966ff", "#66ccff", "#47ebb4", "#00b300")) +
scale_fill_manual(values = c("#ffbf00", "#ff7433","#db5764",
"#9966ff", "#66ccff", "#47ebb4", "#00b300")) +
labs(y=expression(paste("POM FA (",mu,"g L"^-1*")")),
x="", color="" , fill="") +
scale_y_continuous(expand = c(0,0)) +
scale_x_date(breaks = as.Date(c("2015-01-01", "2015-04-01", "2015-07-01",
"2015-10-01")),
labels = c("Jan", "Apr", "Jul", "Oct"),
limits = as.Date(c("2015-01-01", "2015-12-31")),
expand = c(0,0)) +
theme(legend.position = "none",
plot.margin = unit(c(0.1, 0, -0.3, 0.2), "cm"),
panel.grid.minor = element_blank(),
axis.text.x = element_blank(),
axis.text = element_text(size=7),
axis.title = element_text(size=9),
legend.key.height = unit(0.3, 'cm'),
panel.background = element_rect(fill = "transparent", colour = NA),
plot.background = element_rect(fill = "transparent", colour = NA),) +
coord_cartesian(clip = "off")
pPOMFA
ggsave(
plot = pPOMFA,
filename = "20230912 POM TFA time series 2.png",
bg = "transparent"
)
png("20230912 POM TFA time series.png", width=90, height=56, units="mm", res=300)
pPOMFA
dev.off()
fatty.acid.all$Size.Fraction <- factor(fatty.acid.all$Size.Fraction, levels = c("2000", "1000","500", "250", "125","64", "POM"))
pSumFA <- ggplot(fatty.acid.all, aes(x=Date, y=SumFA_mg.g, color=Size.Fraction, fill=Size.Fraction)) +
geom_point() + theme_classic() + geom_smooth(se=F) +
scale_color_manual(values = c("#ffbf00", "#ff7433","#db5764",
"#9966ff", "#66ccff", "#47ebb4", "#00b300")) +
scale_fill_manual(values = c("#ffbf00", "#ff7433","#db5764",
"#9966ff", "#66ccff", "#47ebb4", "#00b300")) +
labs(y=expression(paste("Total FA (",mu,"g mg"^-1*")")),
x="", color="" , fill="") +
scale_y_continuous(expand = c(0,0), limits = c(0,200)) +
scale_x_date(breaks = as.Date(c("2015-01-01", "2015-04-01", "2015-07-01",
"2015-10-01")),
labels = c("Jan", "Apr", "Jul", "Oct"),
limits = as.Date(c("2015-01-01", "2015-12-31")),
expand = c(0,0)) +
theme(legend.position = "none", legend.text = element_text(size = 6),
legend.background = element_rect(fill='transparent'), #transparent legend bg
plot.margin = unit(c(0.1, 0, -0.3, 0.2), "cm"),
panel.grid.minor = element_blank(),
axis.text.x = element_blank(),
axis.text = element_text(size=7),
axis.title = element_text(size=9),
legend.key.height = unit(0.3, 'cm')) +
geom_text(label="(a)", x=as.Date("2014-11-08"), y=199, color="black") +
coord_cartesian(clip = "off")
pSumFA
prow <- plot_grid(pSumFA, pDel13C, ptrophicP,
align = 'v',
nrow = 3,
rel_heights = c(0.9,0.9,1))
prow
png("20231108 carbon and TP time series.png", width=90, height=175, units="mm", res=300)
prow
dev.off()
# abundant FAs in Zoops ------------------------------------------------------------
# Galloway and Winder 2015 arcsine-square root transformed the FA proportions
asinTransform <- function(p) { asin(sqrt(p)) }
# Make reduced data matrix of Zoop FAs
QU39.2015.64 <- QU39.2015.64[!is.na(QU39.2015.64$C22.6n.3_PERCENT),]
Q20.matrix <- QU39.2015.64 %>% select(C14.0_PERCENT:C22.1n.11_PERCENT, Bacteria_15_17)
Q20.matrix <- Q20.matrix %>% select(-C24.0_PERCENT, -C22.5n.6._PERCENT)
# What are the average contributions of each FA
contributions <- data.frame(matrix(nrow=ncol(Q20.matrix), ncol=3))
for(i in 1:ncol(Q20.matrix)){
contributions[i,1] <- names(Q20.matrix[i])
contributions[i,2] <- mean(Q20.matrix[,i])*100
temp <- (Q20.matrix[i]==0)
contributions[i,3] <- length(temp[temp==TRUE])
}
names(contributions) <- c("FA", "Mean.Percentage", "Num zeros")
# separate out peaks that are >1%
abundant.all <- contributions$FA[contributions$Mean.Percentage>=1]
abundant.all <- abundant.all[!is.na(abundant.all)]
# Remove 18:2n-6 because it coeluted with 16:4n-1
(abundant.all <- abundant.all[-c(8)])
# remove FATM for zooplankton
abundant <- abundant.all[-c(9,14,12)]
Q20.matrix.abund <- Q20.matrix[,abundant]
colnames(Q20.matrix.abund) <- paste(str_remove(colnames(Q20.matrix.abund), "_PERCENT"), "", sep = "")
# Arcsine square root Transform data
Q20.matrix.abund.transformed <- asinTransform(Q20.matrix.abund)
# FA summary table --------------------------------------------------------
QU39.2015.64.sm <- QU39.2015.64[!is.na(QU39.2015.64$C16.0_PERCENT),]
# QU39.2015.64.bySize <- QU39.2015.64.sm %>% select(Size.Fraction, C14.0_PERCENT:C22.1n.11_PERCENT, C12.0_PERCENT:Diatom.II_PERCENT)
QU39.2015.64.bySize <- QU39.2015.64.sm %>% select(Size.Fraction, all_of(abundant.all))
QU39.2015.64.bySize <- QU39.2015.64.bySize[complete.cases(QU39.2015.64.bySize),]
QU39.2015.64.bySize <- QU39.2015.64.bySize %>% mutate(SumFA=rowSums(.[c(2:16)]))
QU39.2015.64.bySize.grouped <- group_by(QU39.2015.64.bySize, Size.Fraction) # create an internal grouping structure
# Summary functions
summ.all.mean <- summarise_all(QU39.2015.64.bySize.grouped, mean)
summ.all.sd <- summarise_all(QU39.2015.64.bySize.grouped, sd)
# write.csv(summ.all.sd, "20230206 FA contributions by size class sd.csv")
# write.csv(QU39.2015.64.bySize, "20230206 FA table.csv")
# n for each size class
table(QU39.2015.64.sm$Size.Fraction)
# NMDS --------------------------------------------------------------------
Size <- QU39.2015.64$Size.Fraction
Months <- QU39.2015.64$Month
Seasons <- QU39.2015.64$season
taxa.names <- QU39.2015.64$Size.Fraction
taxa.names <- droplevels(taxa.names)
#create matrix for NMDS calculation:
species.matrix.sm <- as.matrix(Q20.matrix.abund)
#change diet dataframe into a matrix
class(species.matrix.sm) <- "numeric"
#make sure your numbers are treated as numbers
proportions_matrix <- decostand(species.matrix.sm, "total")
#calculations proportional biomass for each fish stomach
#total = 1 so it's expressed as a decimal. It is NOT total = 100 and a percentage.
transformed_matrix <- asin(sqrt(proportions_matrix))
#arc sine square root transformation of diet data
set.seed(50)
eco.nmds.bc <- metaMDS(transformed_matrix, distance="bray",labels=Size, trymax = 100, autotransform = FALSE)
# eco.nmds.bc <- metaMDS(transformed_matrix, distance="bray",labels=Size, trymax = 100, autotransform = FALSE, k=3)
eco.nmds.bc[1]
stressplot(eco.nmds.bc)
plot(eco.nmds.bc, type = "p")
species.scores <- as.data.frame(scores(eco.nmds.bc, "species")) #Using the scores function from vegan to extract the species scores and convert to a data.frame
species.scores$species <- paste(str_remove(row.names(species.scores), "_PERCENT"), "", sep = "")
species.scores #look at the data
# NMDS1: 22:1n-11, 15:0+17:0, 16:3n-4, 18:0
# NMDS2: 18:1n-9, 14:0, 16:3n-4, 16:2n-4
sites.scores <- as.data.frame(scores(eco.nmds.bc, "sites"))
sites.scores <- cbind(sites.scores, QU39.2015.64$Size.Fraction, QU39.2015.64$Date)
colnames(sites.scores)[3:4] <- c("Size.Fraction", "Date")
# *PERMANOVA ---------------------------------------------------------------
permanova_eco.bc<-adonis2(transformed_matrix ~ taxa.names*Seasons, permutations = 999, method="bray")
permanova_eco.bc<-adonis2(transformed_matrix ~ taxa.names*Months, permutations = 999, method="bray")
permanova_eco.bc<-adonis2(transformed_matrix ~ taxa.names, permutations = 999, method="bray")
permanova_eco.bc #if significant, then plot it
# significant w and wo POM
pairwise.adonis(transformed_matrix, taxa.names)
eco.nmds.bc <- metaMDS(transformed_matrix, distance="bray",labels=Size, trymax = 100, autotransform = FALSE)
NMDS.bc<-data.frame(NMDS1.bc=eco.nmds.bc$points[,1],NMDS2.bc=eco.nmds.bc$points[,2],group=taxa.names, month= Months, season=Seasons)
#dataframe for plotting NMDS
eco.nmds.bc$species
# https://stackoverflow.com/questions/13794419/plotting-ordiellipse-function-from-vegan-package-onto-nmds-plot-created-in-ggplo
#Ellipses are standard deviation, no scaling of data (can use standard error, scaling, and confidence limit options)
ord.bc<-ordiellipse(eco.nmds.bc,taxa.names,display="sites",kind="sd", conf = 0.95, label=T)
veganCovEllipse<-function (cov, center = c(0, 0), scale = 1, npoints = 100)
{
theta <- (0:npoints) * 2 * pi/npoints
Circle <- cbind(cos(theta), sin(theta))
t(center + scale * t(Circle %*% chol(cov)))
}
df_ell.bc <- data.frame()
for(g in levels(NMDS.bc$group)){
df_ell.bc <- rbind(df_ell.bc, cbind(as.data.frame(with(NMDS.bc[NMDS.bc$group==g,],
veganCovEllipse(ord.bc[[g]]$cov,ord.bc[[g]]$center))),group=g))
}
#https://www.rpubs.com/RGrieger/545184
df_ell.bc$group <- as.factor(df_ell.bc$group)
df_ell.bc$group <- factor(df_ell.bc$group, levels = c("2000", "1000","500", "250", "125","64"))
# Working on putting species (FAs) onto plot
species.dataframe <- data.frame(FA = rownames(eco.nmds.bc$species),
NMDS.1 = eco.nmds.bc$species[,1],
NMDS.2 = eco.nmds.bc$species[,2])
# NMDS plot colored by size class
# With shape for season