-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRibbonPlotsEmily.R
2129 lines (1691 loc) · 93.1 KB
/
RibbonPlotsEmily.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
#Ribbon plots
#based on: https://search.r-project.org/CRAN/refmans/riverplot/html/riverplot-package.html
#install.packages("riverplot")
library(riverplot)
library(tidyverse)
library(cowplot)
library(gridGraphics)
#Checking biomass per stem data
biomassperstem<-read.csv("/Users/farrer/Dropbox/EmilyComputerBackup/Documents/LAmarsh/Survey/Stats/Temporal/LAmarsh_Survey_2020_BiomassperStem.csv") #
bpsmean<-biomassperstem%>%
filter(Species2!="Unknown grass #2",Species2!="Unknown plant 1",Species2!="Unknown plant 2",Species2!="Triangle stem unknown biomass")%>%
group_by(Species2)%>%
tally()
as.data.frame(bpsmean)
#River plot data
#rivdat<-read.csv("/Users/farrer/Dropbox/EmilyComputerBackup/Documents/LAmarsh/Survey/Stats/Temporal/Phragbiomass2023.csv") #old with only bins 1-4
#rivdat<-read.csv("/Users/farrer/Dropbox/EmilyComputerBackup/Documents/LAmarsh/Survey/Stats/Temporal/Phragnative1.csv") #old, no 2023
rivdat<-read.csv("/Users/farrer/Dropbox/EmilyComputerBackup/Documents/LAmarsh/Survey/Stats/Temporal/Corrected_Biomass2023.csv") #
head(rivdat)
View(rivdat)
#Bin1: Value-based intervals with the cutoffs being 0 to 0.33, 0.33 to 0.66, and 0.66 to 1
#Bin2: Value-based intervals with the cutoffs being 0 to 0.25, 0.25 to 0.5, and 0.5 to 1
#Bin3: Percentile-based intervals based on the total dataset's 33rd and 66th percentile values acting as cutoffs
#Bin4: Percentile-based intervals sub-setted by Site using 33rd and 66th percentile values acting as cutoffs for site-specific groupings
#Bin5: Percentile-based intervals based on the total dataset's 33rd and 66th percentile values acting as cutoffs from the base year 2017
#Bin6: Percentile-based intervals sub-setted by Site using 33rd and 66th percentile values acting as cutoffs for site-specific groupings from the base year 2017. note some of these were "wrong" when I tried calculating them in R. they were probably just humna error in excel, so below I recalculate bin6 in R. I did not check the other bins for accuracy
#Bayou Sauvage weirdness
#Plot 112 and 113 are missing in year2 (2018) (not found so data not taken)
#in 2023 plot 109 had nothing in it (it was surveyed and had nothing) and 111 had only 2 stems of baccharis, no phrag. baccharis was not measured for biomass, but I can still call this plot a "low" phrag b/c it was 100% native
#Finding percentiles
#Coleman did it in terms of relative biomass not just Phrag biomass
quantile(rivdat$Phragmites.australis.Biomass.g., c(.33, .66))
#odd the 33rd percentile is 0, how did that go down? i think this is what happened and it is awkward
cbind(rivdat$Phragmites.australis.Biomass.g.,rivdat$Bin3)
length(which(rivdat$Bin3=="Low"))
length(which(rivdat$Bin3=="Mid"))
length(which(rivdat$Bin3=="High"))
##### Calculating bin 6 for each site for all years #####
BPbin6<-rivdat%>%
filter(Site=="Barataria",Year=="2017")
#quantile(BPbin6$Phragmites.australis.Biomass.g., c(.33, .66))
quantile(BPbin6$Percentage.Phrag, c(.33, .66))
BPbin6b<-rivdat%>%
filter(Site=="Barataria")%>%
mutate(bin6b = cut(Percentage.Phrag, c(-1, 0.1259551, 0.5289319,1), labels=c("Low","Mid","High") ,right = T))
which(BPbin6b$Bin6!=BPbin6b$bin6b)
#write.csv(BPbin6b,"BPbin6b.csv")
BBbin6<-rivdat%>%
filter(Site=="Big Branch",Year=="2017")
quantile(BBbin6$Percentage.Phrag, c(.33, .66))
BBbin6b<-rivdat%>%
filter(Site=="Big Branch")%>%
mutate(bin6b = cut(Percentage.Phrag, c(-1, 0.2457260, 0.9054701,1), labels=c("Low","Mid","High") ,right = T))
which(BBbin6b$Bin6!=BBbin6b$bin6b)
BSbin6<-rivdat%>%
filter(Site=="Bayou Sauvage",Year=="2017")
quantile(BSbin6$Percentage.Phrag, c(.33, .66))
BSbin6b<-rivdat%>%
filter(Site=="Bayou Sauvage")%>%
mutate(bin6b = cut(Percentage.Phrag, c(-1, 0.4081905, 0.9775267,1), labels=c("Low","Mid","High") ,right = T))
#plot 122 is low for me, mid for Coleman
which(BSbin6b$Bin6!=BSbin6b$bin6b)
BSbin6b%>%
filter(Plot%in%c(109,111))
BSbin6b%>%
filter(Plot%in%c(113))
BSbin6b%>%
filter(Plot%in%c(122))
PRbin6<-rivdat%>%
filter(Site=="Pearl River",Year=="2017")
quantile(PRbin6$Percentage.Phrag, c(.33, .66))
PRbin6b<-rivdat%>%
filter(Site=="Pearl River")%>%
mutate(bin6b = cut(Percentage.Phrag, c(-1, 0.0550037, 0.4517804,1), labels=c("Low","Mid","High") ,right = T))
which(PRbin6b$Bin6!=PRbin6b$bin6b)
PRbin6b[c(14,89),]
##### Separating by site - Barataria 2023 #####
# Using Bin6
#rivdatBP<-rivdat%>%
# filter(Site=="Barataria")%>%
BPbin6c<-BPbin6b%>%
select(-Bin6)%>%
dplyr::rename(Bin6=bin6b)
rivdatBP<-BPbin6c%>%
arrange(Plot,Year)%>%
select(Year,Plot,Bin6)%>%
mutate(Bin6=recode_factor(Bin6,"Low"="N","Mid"="T","High"="P"))%>%
mutate(Year=recode_factor(Year,"2017"="y1","2018"="y2","2019"="y3","2020"="y4","2021"="y5","2022"="y6","2023"="y7"))%>%
pivot_wider(names_from=Year,values_from=Bin6)%>%
mutate(y1=recode_factor(y1,"N"="N1","T"="T1","P"="P1"))%>%
mutate(y2=recode_factor(y2,"N"="N2","T"="T2","P"="P2"))%>%
mutate(y3=recode_factor(y3,"N"="N3","T"="T3","P"="P3"))%>%
mutate(y4=recode_factor(y4,"N"="N4","T"="T4","P"="P4"))%>%
mutate(y5=recode_factor(y5,"N"="N5","T"="T5","P"="P5"))%>%
mutate(y6=recode_factor(y6,"N"="N6","T"="T6","P"="P6"))%>%
mutate(y7=recode_factor(y7,"N"="N7","T"="T7","P"="P7"))%>%
unite("y1_y2",y1,y2,sep="_",remove=F)%>%
unite("y2_y3",y2,y3,sep="_",remove=F)%>%
unite("y3_y4",y3,y4,sep="_",remove=F)%>%
unite("y4_y5",y4,y5,sep="_",remove=F)%>%
unite("y5_y6",y5,y6,sep="_",remove=F)%>%
unite("y6_y7",y6,y7,sep="_",remove=F)%>%
filter(is.na(y2)==F)%>%
select(Plot,y1_y2,y2_y3,y3_y4,y4_y5,y5_y6,y6_y7)%>%
pivot_longer(y1_y2:y6_y7,names_to="years",values_to = "change")%>%
mutate(ones=1)%>%
group_by(years,change)%>%
summarise(sum=sum(ones))%>%
mutate(change=factor(change,levels=c("N1_N2", "N1_T2", "N1_P2", "T1_N2", "T1_T2", "T1_P2", "P1_N2", "P1_T2", "P1_P2",
"N2_N3", "N2_T3", "N2_P3", "T2_N3", "T2_T3", "T2_P3", "P2_N3", "P2_T3", "P2_P3",
"N3_N4", "N3_T4", "N3_P4", "T3_N4", "T3_T4", "T3_P4", "P3_N4", "P3_T4", "P3_P4",
"N4_N5", "N4_T5", "N4_P5", "T4_N5", "T4_T5", "T4_P5", "P4_N5", "P4_T5", "P4_P5",
"N5_N6", "N5_T6", "N5_P6", "T5_N6", "T5_T6", "T5_P6", "P5_N6", "P5_T6", "P5_P6",
"N6_N7", "N6_T7", "N6_P7", "T6_N7", "T6_T7", "T6_P7", "P6_N7", "P6_T7", "P6_P7")))%>%
arrange(change)%>%
separate(change, c("N1","N2"), remove=F)
as.data.frame(rivdatBP)
nodesp <- structure(
list(
ID = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,12L,13L,14L,15L, 16L,17L,18L,19L,20L,21L), .Label = c("N1", "T1", "P1", "N2", "T2", "P2","N3","T3", "P3", "N4","T4","P4","N5","T5","P5","N6","T6","P6","N7","T7","P7"), class = "factor"),
x = c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L,4L,4L,4L,5L,5L,5L, 6L, 6L, 6L, 7L, 7L, 7L),
col = c("#56ae6c","#6881d8","#ba543d","#56ae6c","#6881d8","#ba543d","#56ae6c","#6881d8","#ba543d","#56ae6c","#6881d8","#ba543d","#56ae6c","#6881d8","#ba543d","#56ae6c","#6881d8","#ba543d","#56ae6c","#6881d8","#ba543d")),
.Names = c("ID", "x", "col"),
row.names = c("N1", "T1", "P1", "N2", "T2", "P2","N3","T3", "P3", "N4","T4","P4","N5","T5","P5","N6","T6","P6","N7","T7","P7" ), class = "data.frame")
edgesp <- structure(
list(
N1 = rivdatBP$N1,
N2 = rivdatBP$N2,
Value = rivdatBP$sum
), .Names = c("N1", "N2", "Value"), row.names = c(NA, -42L), class = "data.frame")#
RPBP <- makeRiver(nodesp, edgesp,node_labels = c("Native","Trans","Phrag",rep("",18)))
pdf("/Users/farrer/Dropbox/EmilyComputerBackup/Documents/LAmarsh/Survey/Manuscripts/Temporal/Figs/riverplotBP.pdf",width=4.8,height=3.2)
plot(RPBP, plot_area=0.9,srt=0) #srt=0 makes labels horizontal
text(x=seq(.08,1,by=.14),c(0,0),labels=c(2017:2023),cex=.8)
dev.off()
#plot(RPBP, plot_area=0.9)
#nice green color #729b57
##### Separating by site - Big Branch 2023 #####
#Using Bin6
#rivdatBB<-rivdat%>%
# filter(Site=="Big Branch")%>%
BBbin6c<-BBbin6b%>%
select(-Bin6)%>%
dplyr::rename(Bin6=bin6b)
rivdatBB<-BBbin6c%>%
arrange(Plot,Year)%>%
select(Year,Plot,Bin6)%>%
mutate(Bin6=recode_factor(Bin6,"Low"="N","Mid"="T","High"="P"))%>%
mutate(Year=recode_factor(Year,"2017"="y1","2018"="y2","2019"="y3","2020"="y4","2021"="y5","2022"="y6","2023"="y7"))%>%
pivot_wider(names_from=Year,values_from=Bin6)%>%
mutate(y1=recode_factor(y1,"N"="N1","T"="T1","P"="P1"))%>%
mutate(y2=recode_factor(y2,"N"="N2","T"="T2","P"="P2"))%>%
mutate(y3=recode_factor(y3,"N"="N3","T"="T3","P"="P3"))%>%
mutate(y4=recode_factor(y4,"N"="N4","T"="T4","P"="P4"))%>%
mutate(y5=recode_factor(y5,"N"="N5","T"="T5","P"="P5"))%>%
mutate(y6=recode_factor(y6,"N"="N6","T"="T6","P"="P6"))%>%
mutate(y7=recode_factor(y7,"N"="N7","T"="T7","P"="P7"))%>%
unite("y1_y2",y1,y2,sep="_",remove=F)%>%
unite("y2_y3",y2,y3,sep="_",remove=F)%>%
unite("y3_y4",y3,y4,sep="_",remove=F)%>%
unite("y4_y5",y4,y5,sep="_",remove=F)%>%
unite("y5_y6",y5,y6,sep="_",remove=F)%>%
unite("y6_y7",y6,y7,sep="_",remove=F)%>%
filter(is.na(y2)==F)%>%
select(Plot,y1_y2,y2_y3,y3_y4,y4_y5,y5_y6,y6_y7)%>%
pivot_longer(y1_y2:y6_y7,names_to="years",values_to = "change")%>%
mutate(ones=1)%>%
group_by(years,change)%>%
summarise(sum=sum(ones))%>%
mutate(change=factor(change,levels=c("N1_N2", "N1_T2", "N1_P2", "T1_N2", "T1_T2", "T1_P2", "P1_N2", "P1_T2", "P1_P2",
"N2_N3", "N2_T3", "N2_P3", "T2_N3", "T2_T3", "T2_P3", "P2_N3", "P2_T3", "P2_P3",
"N3_N4", "N3_T4", "N3_P4", "T3_N4", "T3_T4", "T3_P4", "P3_N4", "P3_T4", "P3_P4",
"N4_N5", "N4_T5", "N4_P5", "T4_N5", "T4_T5", "T4_P5", "P4_N5", "P4_T5", "P4_P5",
"N5_N6", "N5_T6", "N5_P6", "T5_N6", "T5_T6", "T5_P6", "P5_N6", "P5_T6", "P5_P6",
"N6_N7", "N6_T7", "N6_P7", "T6_N7", "T6_T7", "T6_P7", "P6_N7", "P6_T7", "P6_P7")))%>%
arrange(change)%>%
separate(change, c("N1","N2"), remove=F)
as.data.frame(rivdatBB)
nodesp <- structure(
list(
ID = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,12L,13L,14L,15L, 16L,17L,18L,19L,20L,21L), .Label = c("N1", "T1", "P1", "N2", "T2", "P2","N3","T3", "P3", "N4","T4","P4","N5","T5","P5","N6","T6","P6","N7","T7","P7"), class = "factor"),
x = c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L,4L,4L,4L,5L,5L,5L, 6L, 6L, 6L,7L,7L,7L),
col = c("#56ae6c","#6881d8","#ba543d","#56ae6c","#6881d8","#ba543d","#56ae6c","#6881d8","#ba543d","#56ae6c","#6881d8","#ba543d","#56ae6c","#6881d8","#ba543d","#56ae6c","#6881d8","#ba543d","#56ae6c","#6881d8","#ba543d")),
.Names = c("ID", "x", "col"),
row.names = c("N1", "T1", "P1", "N2", "T2", "P2","N3","T3", "P3", "N4","T4","P4","N5","T5","P5","N6","T6","P6","N7","T7","P7" ), class = "data.frame")
edgesp <- structure(
list(
N1 = rivdatBB$N1,
N2 = rivdatBB$N2,
Value = rivdatBB$sum
), .Names = c("N1", "N2", "Value"), row.names = c(NA, -33L), class = "data.frame")#
RPBB <- makeRiver(nodesp, edgesp,node_labels = c("Native","Trans","Phrag",rep("",18)))
plot(RPBB, plot_area=0.9,srt=0)
pdf("/Users/farrer/Dropbox/EmilyComputerBackup/Documents/LAmarsh/Survey/Manuscripts/Temporal/Figs/riverplotBB.pdf",width=4.8,height=3.2)
plot(RPBB, plot_area=0.9,srt=0) #srt=0 makes labels horizontal
text(x=seq(.08,1,by=.14),c(0,0),labels=c(2017:2023),cex=.8)
dev.off()
##### Separating by site - Bayou Sauvage 2023 #####
# Using Bin6
#rivdatBS<-rivdat%>%
# filter(Site=="Bayou Sauvage")%>%
BSbin6c<-BSbin6b%>%
select(-Bin6)%>%
dplyr::rename(Bin6=bin6b)
#add a row for 111 being bin6 Low
dim(BSbin6c)# 143 x 66
BSbin6d<-rbind(BSbin6c,rep(NA,66))
BSbin6d$Plot[144]<-111
BSbin6d$Bin6[144]<-"Low"
BSbin6d$Year[144]<-2023
BSbin6e<-BSbin6d%>%
arrange(Year,Plot)%>%
filter(Plot!=109,Plot!=112,Plot!=113)
BSbin6f<-BSbin6d%>% #This is for use below in the transitions dataset part
arrange(Year,Plot)
rivdatBS<-BSbin6e%>%
arrange(Plot,Year)%>%
select(Year,Plot,Bin6)%>%
mutate(Bin6=recode_factor(Bin6,"Low"="N","Mid"="T","High"="P"))%>%
mutate(Year=recode_factor(Year,"2017"="y1","2018"="y2","2019"="y3","2020"="y4","2021"="y5","2022"="y6","2023"="y7"))%>%
pivot_wider(names_from=Year,values_from=Bin6)%>%
mutate(y1=recode_factor(y1,"N"="N1","T"="T1","P"="P1"))%>%
mutate(y2=recode_factor(y2,"N"="N2","T"="T2","P"="P2"))%>%
mutate(y3=recode_factor(y3,"N"="N3","T"="T3","P"="P3"))%>%
mutate(y4=recode_factor(y4,"N"="N4","T"="T4","P"="P4"))%>%
mutate(y5=recode_factor(y5,"N"="N5","T"="T5","P"="P5"))%>%
mutate(y6=recode_factor(y6,"N"="N6","T"="T6","P"="P6"))%>%
mutate(y7=recode_factor(y7,"N"="N7","T"="T7","P"="P7"))%>%
unite("y1_y2",y1,y2,sep="_",remove=F)%>%
unite("y2_y3",y2,y3,sep="_",remove=F)%>%
unite("y3_y4",y3,y4,sep="_",remove=F)%>%
unite("y4_y5",y4,y5,sep="_",remove=F)%>%
unite("y5_y6",y5,y6,sep="_",remove=F)%>%
unite("y6_y7",y6,y7,sep="_",remove=F)%>%
filter(is.na(y2)==F)%>%
select(Plot,y1_y2,y2_y3,y3_y4,y4_y5,y5_y6,y6_y7)%>%
pivot_longer(y1_y2:y6_y7,names_to="years",values_to = "change")%>%
mutate(ones=1)%>%
group_by(years,change)%>%
summarise(sum=sum(ones))%>%
mutate(change=factor(change,levels=c("N1_N2", "N1_T2", "N1_P2", "T1_N2", "T1_T2", "T1_P2", "P1_N2", "P1_T2", "P1_P2",
"N2_N3", "N2_T3", "N2_P3", "T2_N3", "T2_T3", "T2_P3", "P2_N3", "P2_T3", "P2_P3",
"N3_N4", "N3_T4", "N3_P4", "T3_N4", "T3_T4", "T3_P4", "P3_N4", "P3_T4", "P3_P4",
"N4_N5", "N4_T5", "N4_P5", "T4_N5", "T4_T5", "T4_P5", "P4_N5", "P4_T5", "P4_P5",
"N5_N6", "N5_T6", "N5_P6", "T5_N6", "T5_T6", "T5_P6", "P5_N6", "P5_T6", "P5_P6",
"N6_N7", "N6_T7", "N6_P7", "T6_N7", "T6_T7", "T6_P7", "P6_N7", "P6_T7", "P6_P7")))%>%
arrange(change)%>%
separate(change, c("N1","N2"), remove=F)
as.data.frame(rivdatBS)
nodesp <- structure(
list(
ID = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,12L,13L,14L,15L, 16L,17L,18L,19L,20L,21L), .Label = c("N1", "T1", "P1", "N2", "T2", "P2","N3","T3", "P3", "N4","T4","P4","N5","T5","P5","N6","T6","P6","N7","T7","P7"), class = "factor"),
x = c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L,4L,4L,4L,5L,5L,5L, 6L, 6L, 6L,7L,7L,7L),
col = c("#56ae6c","#6881d8","#ba543d","#56ae6c","#6881d8","#ba543d","#56ae6c","#6881d8","#ba543d","#56ae6c","#6881d8","#ba543d","#56ae6c","#6881d8","#ba543d","#56ae6c","#6881d8","#ba543d","#56ae6c","#6881d8","#ba543d")),
.Names = c("ID", "x", "col"),
row.names = c("N1", "T1", "P1", "N2", "T2", "P2","N3","T3", "P3", "N4","T4","P4","N5","T5","P5","N6","T6","P6","N7","T7","P7" ), class = "data.frame")
edgesp <- structure(
list(
N1 = rivdatBS$N1,
N2 = rivdatBS$N2,
Value = rivdatBS$sum
), .Names = c("N1", "N2", "Value"), row.names = c(NA, -35L), class = "data.frame")#
RPBS <- makeRiver(nodesp, edgesp, node_labels = c("Native","Trans","Phrag",rep("",18)))
plot(RPBS, plot_area=0.9)
pdf("/Users/farrer/Dropbox/EmilyComputerBackup/Documents/LAmarsh/Survey/Manuscripts/Temporal/Figs/riverplotBS.pdf",width=4.8,height=3.2)
plot(RPBS, plot_area=0.9,srt=0) #srt=0 makes labels horizontal
text(x=seq(.08,1,by=.14),c(0,0),labels=c(2017:2023),cex=.8)
dev.off()
##### Separating by site - Pearl River 2023 #####
#Using Bin6
#rivdatPR<-rivdat%>%
# filter(Site=="Pearl River")%>%
PRbin6c<-PRbin6b%>%
select(-Bin6)%>%
dplyr::rename(Bin6=bin6b)
rivdatPR<-PRbin6c%>%
arrange(Plot,Year)%>%
select(Year,Plot,Bin6)%>%
mutate(Bin6=recode_factor(Bin6,"Low"="N","Mid"="T","High"="P"))%>%
mutate(Year=recode_factor(Year,"2017"="y1","2018"="y2","2019"="y3","2020"="y4","2021"="y5","2022"="y6","2023"="y7"))%>%
pivot_wider(names_from=Year,values_from=Bin6)%>%
mutate(y1=recode_factor(y1,"N"="N1","T"="T1","P"="P1"))%>%
mutate(y2=recode_factor(y2,"N"="N2","T"="T2","P"="P2"))%>%
mutate(y3=recode_factor(y3,"N"="N3","T"="T3","P"="P3"))%>%
mutate(y4=recode_factor(y4,"N"="N4","T"="T4","P"="P4"))%>%
mutate(y5=recode_factor(y5,"N"="N5","T"="T5","P"="P5"))%>%
mutate(y6=recode_factor(y6,"N"="N6","T"="T6","P"="P6"))%>%
mutate(y7=recode_factor(y7,"N"="N7","T"="T7","P"="P7"))%>%
unite("y1_y2",y1,y2,sep="_",remove=F)%>%
unite("y2_y3",y2,y3,sep="_",remove=F)%>%
unite("y3_y4",y3,y4,sep="_",remove=F)%>%
unite("y4_y5",y4,y5,sep="_",remove=F)%>%
unite("y5_y6",y5,y6,sep="_",remove=F)%>%
unite("y6_y7",y6,y7,sep="_",remove=F)%>%
filter(is.na(y2)==F)%>%
select(Plot,y1_y2,y2_y3,y3_y4,y4_y5,y5_y6,y6_y7)%>%
pivot_longer(y1_y2:y6_y7,names_to="years",values_to = "change")%>%
mutate(ones=1)%>%
group_by(years,change)%>%
summarise(sum=sum(ones))%>%
mutate(change=factor(change,levels=c("N1_N2", "N1_T2", "N1_P2", "T1_N2", "T1_T2", "T1_P2", "P1_N2", "P1_T2", "P1_P2",
"N2_N3", "N2_T3", "N2_P3", "T2_N3", "T2_T3", "T2_P3", "P2_N3", "P2_T3", "P2_P3",
"N3_N4", "N3_T4", "N3_P4", "T3_N4", "T3_T4", "T3_P4", "P3_N4", "P3_T4", "P3_P4",
"N4_N5", "N4_T5", "N4_P5", "T4_N5", "T4_T5", "T4_P5", "P4_N5", "P4_T5", "P4_P5",
"N5_N6", "N5_T6", "N5_P6", "T5_N6", "T5_T6", "T5_P6", "P5_N6", "P5_T6", "P5_P6",
"N6_N7", "N6_T7", "N6_P7", "T6_N7", "T6_T7", "T6_P7", "P6_N7", "P6_T7", "P6_P7")))%>%
arrange(change)%>%
separate(change, c("N1","N2"), remove=F)
as.data.frame(rivdatPR)
nodesp <- structure(
list(
ID = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,12L,13L,14L,15L, 16L,17L,18L,19L,20L,21L), .Label = c("N1", "T1", "P1", "N2", "T2", "P2","N3","T3", "P3", "N4","T4","P4","N5","T5","P5","N6","T6","P6","N7","T7","P7"), class = "factor"),
x = c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L,4L,4L,4L,5L,5L,5L, 6L, 6L, 6L,7L,7L,7L),
col = c("#56ae6c","#6881d8","#ba543d","#56ae6c","#6881d8","#ba543d","#56ae6c","#6881d8","#ba543d","#56ae6c","#6881d8","#ba543d","#56ae6c","#6881d8","#ba543d","#56ae6c","#6881d8","#ba543d","#56ae6c","#6881d8","#ba543d")),
.Names = c("ID", "x", "col"),
row.names = c("N1", "T1", "P1", "N2", "T2", "P2","N3","T3", "P3", "N4","T4","P4","N5","T5","P5","N6","T6","P6","N7","T7","P7" ), class = "data.frame")
edgesp <- structure(
list(
N1 = rivdatPR$N1,
N2 = rivdatPR$N2,
Value = rivdatPR$sum
), .Names = c("N1", "N2", "Value"), row.names = c(NA, -30L), class = "data.frame")#
RPPR <- makeRiver(nodesp, edgesp,node_labels = c("Native","Trans","Phrag",rep("",18)))
plot(RPPR, plot_area=0.9)
pdf("/Users/farrer/Dropbox/EmilyComputerBackup/Documents/LAmarsh/Survey/Manuscripts/Temporal/Figs/riverplotPR.pdf",width=4.8,height=3.2)
plot(RPPR, plot_area=0.9,srt=0) #srt=0 makes labels horizontal
text(x=seq(.08,1,by=.14),c(0,0),labels=c(2017:2023),cex=.8)
dev.off()
##### Making a dataset of transitions from the riverplots #####
trBP<-BPbin6c%>%
arrange(Plot,Year)%>%
select(Year,Plot,Bin6)%>%
mutate(Bin6=recode_factor(Bin6,"Low"="N","Mid"="T","High"="P"))%>%
mutate(Year=recode_factor(Year,"2017"="y1","2018"="y2","2019"="y3","2020"="y4","2021"="y5","2022"="y6","2023"="y7"))%>%
pivot_wider(names_from=Year,values_from=Bin6)%>%
unite("y1_y2",y1,y2,sep="_",remove=F)%>%
unite("y2_y3",y2,y3,sep="_",remove=F)%>%
unite("y3_y4",y3,y4,sep="_",remove=F)%>%
unite("y4_y5",y4,y5,sep="_",remove=F)%>%
unite("y5_y6",y5,y6,sep="_",remove=F)%>%
unite("y6_y7",y6,y7,sep="_",remove=F)%>%
filter(is.na(y2)==F)%>%
select(Plot,y1_y2,y2_y3,y3_y4,y4_y5,y5_y6,y6_y7)%>%
pivot_longer(y1_y2:y6_y7,names_to="years",values_to = "change")%>%
separate(change, c("State1","State2"), remove=F)%>%
separate(change, c("State1","State2"), remove=F)%>%
mutate(NN=ifelse(State1=="N"&State2=="N",1,0))%>%
mutate(NT=ifelse(State1=="N"&State2=="T",1,0))%>%
mutate(NP=ifelse(State1=="N"&State2=="P",1,0))%>%
mutate(TT=ifelse(State1=="T"&State2=="T",1,0))%>%
mutate(TN=ifelse(State1=="T"&State2=="N",1,0))%>%
mutate(TP=ifelse(State1=="T"&State2=="P",1,0))%>%
mutate(PP=ifelse(State1=="P"&State2=="P",1,0))%>%
mutate(PN=ifelse(State1=="P"&State2=="N",1,0))%>%
mutate(PT=ifelse(State1=="P"&State2=="T",1,0))
data.frame(trBP)
trBP$Site<-"Barataria"
trBB<-BBbin6c%>%
arrange(Plot,Year)%>%
select(Year,Plot,Bin6)%>%
mutate(Bin6=recode_factor(Bin6,"Low"="N","Mid"="T","High"="P"))%>%
mutate(Year=recode_factor(Year,"2017"="y1","2018"="y2","2019"="y3","2020"="y4","2021"="y5","2022"="y6","2023"="y7"))%>%
pivot_wider(names_from=Year,values_from=Bin6)%>%
unite("y1_y2",y1,y2,sep="_",remove=F)%>%
unite("y2_y3",y2,y3,sep="_",remove=F)%>%
unite("y3_y4",y3,y4,sep="_",remove=F)%>%
unite("y4_y5",y4,y5,sep="_",remove=F)%>%
unite("y5_y6",y5,y6,sep="_",remove=F)%>%
unite("y6_y7",y6,y7,sep="_",remove=F)%>%
filter(is.na(y2)==F)%>%
select(Plot,y1_y2,y2_y3,y3_y4,y4_y5,y5_y6,y6_y7)%>%
pivot_longer(y1_y2:y6_y7,names_to="years",values_to = "change")%>%
separate(change, c("State1","State2"), remove=F)%>%
separate(change, c("State1","State2"), remove=F)%>%
mutate(NN=ifelse(State1=="N"&State2=="N",1,0))%>%
mutate(NT=ifelse(State1=="N"&State2=="T",1,0))%>%
mutate(NP=ifelse(State1=="N"&State2=="P",1,0))%>%
mutate(TT=ifelse(State1=="T"&State2=="T",1,0))%>%
mutate(TN=ifelse(State1=="T"&State2=="N",1,0))%>%
mutate(TP=ifelse(State1=="T"&State2=="P",1,0))%>%
mutate(PP=ifelse(State1=="P"&State2=="P",1,0))%>%
mutate(PN=ifelse(State1=="P"&State2=="N",1,0))%>%
mutate(PT=ifelse(State1=="P"&State2=="T",1,0))
data.frame(trBB)
trBB$Site<-"Big Branch"
#use BSbin6f if you want to include plots 109,112,113, use BSbin6e if you want all the years from those plots not included
trBS<-BSbin6f%>%
arrange(Plot,Year)%>%
select(Year,Plot,Bin6)%>%
mutate(Bin6=recode_factor(Bin6,"Low"="N","Mid"="T","High"="P"))%>%
mutate(Year=recode_factor(Year,"2017"="y1","2018"="y2","2019"="y3","2020"="y4","2021"="y5","2022"="y6","2023"="y7"))%>%
pivot_wider(names_from=Year,values_from=Bin6)%>%
unite("y1_y2",y1,y2,sep="_",remove=F)%>%
unite("y2_y3",y2,y3,sep="_",remove=F)%>%
unite("y3_y4",y3,y4,sep="_",remove=F)%>%
unite("y4_y5",y4,y5,sep="_",remove=F)%>%
unite("y5_y6",y5,y6,sep="_",remove=F)%>%
unite("y6_y7",y6,y7,sep="_",remove=F)%>%
#filter(is.na(y2)==F)%>%
select(Plot,y1_y2,y2_y3,y3_y4,y4_y5,y5_y6,y6_y7)%>%
pivot_longer(y1_y2:y6_y7,names_to="years",values_to = "change")%>%
separate(change, c("State1","State2"), remove=F)%>%
separate(change, c("State1","State2"), remove=F)%>%
filter(State1!="NA")%>%
filter(State2!="NA")%>%
mutate(NN=ifelse(State1=="N"&State2=="N",1,0))%>%
mutate(NT=ifelse(State1=="N"&State2=="T",1,0))%>%
mutate(NP=ifelse(State1=="N"&State2=="P",1,0))%>%
mutate(TT=ifelse(State1=="T"&State2=="T",1,0))%>%
mutate(TN=ifelse(State1=="T"&State2=="N",1,0))%>%
mutate(TP=ifelse(State1=="T"&State2=="P",1,0))%>%
mutate(PP=ifelse(State1=="P"&State2=="P",1,0))%>%
mutate(PN=ifelse(State1=="P"&State2=="N",1,0))%>%
mutate(PT=ifelse(State1=="P"&State2=="T",1,0))
data.frame(trBS)
trBS$Site<-"Bayou Sauvage"
trPR<-PRbin6c%>%
arrange(Plot,Year)%>%
select(Year,Plot,Bin6)%>%
mutate(Bin6=recode_factor(Bin6,"Low"="N","Mid"="T","High"="P"))%>%
mutate(Year=recode_factor(Year,"2017"="y1","2018"="y2","2019"="y3","2020"="y4","2021"="y5","2022"="y6","2023"="y7"))%>%
pivot_wider(names_from=Year,values_from=Bin6)%>%
unite("y1_y2",y1,y2,sep="_",remove=F)%>%
unite("y2_y3",y2,y3,sep="_",remove=F)%>%
unite("y3_y4",y3,y4,sep="_",remove=F)%>%
unite("y4_y5",y4,y5,sep="_",remove=F)%>%
unite("y5_y6",y5,y6,sep="_",remove=F)%>%
unite("y6_y7",y6,y7,sep="_",remove=F)%>%
filter(is.na(y2)==F)%>%
select(Plot,y1_y2,y2_y3,y3_y4,y4_y5,y5_y6,y6_y7)%>%
pivot_longer(y1_y2:y6_y7,names_to="years",values_to = "change")%>%
separate(change, c("State1","State2"), remove=F)%>%
separate(change, c("State1","State2"), remove=F)%>%
mutate(NN=ifelse(State1=="N"&State2=="N",1,0))%>%
mutate(NT=ifelse(State1=="N"&State2=="T",1,0))%>%
mutate(NP=ifelse(State1=="N"&State2=="P",1,0))%>%
mutate(TT=ifelse(State1=="T"&State2=="T",1,0))%>%
mutate(TN=ifelse(State1=="T"&State2=="N",1,0))%>%
mutate(TP=ifelse(State1=="T"&State2=="P",1,0))%>%
mutate(PP=ifelse(State1=="P"&State2=="P",1,0))%>%
mutate(PN=ifelse(State1=="P"&State2=="N",1,0))%>%
mutate(PT=ifelse(State1=="P"&State2=="T",1,0))
data.frame(trPR)
trPR$Site<-"Pearl River"
#when i first did this, i forgot that i deleted all data from plots 109, 112, and 113 b/c I was missing one year in each of those plots and it was necessary for the ribbon plot. [plot 112 and 113 missing in 2018, plot 109 had nothing in it in 2023, so im missing 5 transitions total] but I can put it back for these analyses. I put them back in but then realized I do still need to delete the ones with NAs 2018-2019,2023, so I filtered on that below
tr<-rbind(trBP,trBS,trBB,trPR)
tr2<-tr%>%
mutate(Year=as.numeric(recode(years,"y1_y2"="2018","y2_y3"="2019","y3_y4"="2020","y4_y5"="2021","y5_y6"="2022","y6_y7"="2023")))%>%
full_join(biomass4)%>%
filter(is.na(change)==F)%>%
mutate(NtoTP=ifelse(State1=="N"&State2%in%c("T","P"),1,0))%>%
mutate(NtoTPandTtoP=ifelse(NtoTP==1|TP==1,1,0))%>%
mutate(PtoNT=ifelse(State1=="P"&State2%in%c("N","T"),1,0))%>%
mutate(PtoNTandTtoN=ifelse(PtoNT==1|TN==1,1,0))%>%
mutate(stasis=case_match(change,"N_N"~1,"T_T"~1,"P_P"~1,"N_T"~0,"N_P"~0,"T_N"~0,"T_P"~0,"P_N"~0,"P_T"~0))%>%
mutate(Sitelab=case_match(Site,"Barataria"~"a) Barataria","Pearl River"~"b) Pearl River","Bayou Sauvage"~"c) Bayou Sauvage","Big Branch"~"d) Big Branch",.ptype = factor(levels = c("a) Barataria","b) Pearl River","c) Bayou Sauvage","d) Big Branch"))))
tr2$Site<-factor(tr2$Site,levels=c("Barataria","Pearl River","Bayou Sauvage","Big Branch"))
#ind<-which(is.na(tr2$NN)==T)
#View(tr2[ind,])
data.frame(tr2) #504 x 42, now 499 x 42
dim(tr2)
tail(data.frame(tr2))
##### Figures of transitions vs plot level salinity and water depth #####
#possible transitions to test. Like Stein et al 2016, we will subset data for plots that did not transition and plots that transitioned into one of the other categories:
#NN-NT
#NN-NP
#TT-TP
#TT-TN
#PP-PN
#PP-PT
#Making some quick datasets and plots of single transitions
#N going to T
NN_NT<-tr2%>%
filter(change%in%c("N_N","N_T"))
data.frame(NN_NT)
ggplot(data = NN_NT, aes(x = Salinity15cmppt, y = NT)) +
geom_point()+
geom_smooth(method='glm',method.args=list(family='binomial'))+
facet_wrap(~Site)
#N going to P
NN_NP<-tr2%>%
filter(change%in%c("N_N","N_P"))
data.frame(NN_NP)
ggplot(data = NN_NP, aes(x = Salinity15cmppt, y = NP)) +
geom_point()+
geom_smooth(method='glm',method.args=list(family='binomial'))+
facet_wrap(~Site)
#N going to either T or P
NN_NT_NP<-tr2%>%
filter(change%in%c("N_N","N_T","N_P"))
data.frame(NN_NT_NP)
ggplot(data = NN_NT_NP, aes(x = Salinity15cmppt, y = NtoTP)) +
geom_point()+
geom_smooth(method='glm',method.args=list(family='binomial'))+
facet_wrap(~Site)
#Making combined dataset of N going to either T or P and T going to P
NN_NT_NP_TT_TP<-tr2%>%
filter(change%in%c("N_N","N_T","N_P","T_T","T_P"))
data.frame(NN_NT_NP_TT_TP)
#Making combined dataset of P going to N or T and T going to N
PP_PN_PT_TT_TN<-tr2%>%
filter(change%in%c("P_P","P_N","P_T","T_T","T_N"))
data.frame(PP_PN_PT_TT_TN)
##### Salinity and Probability of going to more phrag #####
#pdf("/Users/farrer/Dropbox/EmilyComputerBackup/Documents/LAmarsh/Survey/Manuscripts/Temporal/Figs/test.pdf",width=2.4,height=2)
salphrag<-ggplot(data = NN_NT_NP_TT_TP, aes(x = Salinity15cmppt, y = NtoTPandTtoP,color=Site,linetype=Site)) +
theme_classic()+
theme(line=element_line(linewidth =.3),axis.text=element_text(size=9),axis.title = element_text(size = 10),strip.background = element_rect(colour="white", fill="white"),strip.text.x = element_text(size=10,hjust=0,vjust = 1, margin=margin(l=0)),axis.line=element_line(),legend.position="none")+
theme(plot.margin = unit(c(17,5,5,5), "pt"))+#
ylab("Probability of transition") +# toward more Phragmites
xlab("Salinity (ppt)")+
geom_point(size=.8)+
geom_smooth(method='glm',method.args=list(family='binomial'),se=F,size=.5)+
scale_linetype_manual(values = c("dashed", "solid", "solid", "dashed"))
#dev.off()
#Year is linear, years is a factor. For Bayou Sauvage we only have data from some plots in 2019 and from all plots in 2021 and 2023 (I think we have it for 2017 too but that is not included in the transition dataset)
#m1<-lme(NtoTPandTtoP~Site*Salinity15cmppt,random=~1|years,data = NN_NT_NP_TT_TP,na.action = na.omit,control =list(msMaxIter = 1000, msMaxEval = 1000)) #but this is normal
#m1<-gls(NtoTPandTtoP~Site*Salinity15cmppt+years*Site,data = NN_NT_NP_TT_TP,na.action = na.omit) #but this is normal, this doesn't converge b/c don't have salinity from some years from some sites (Bayou Sauvage)
#anova(m1,type="marginal")
#Full model with all sites
b1 <- glm(NtoTPandTtoP ~ Site*Salinity15cmppt, family = binomial(link="logit"), data = NN_NT_NP_TT_TP, na.action=na.exclude)
#summary(b1)
drop1(b1,test="Chisq",.~.)
#I could do this in glmer if I wanted to include a random effect of year, but I don't think I want to because the variation in salinity is over the years so we don't want year per se in the model
# library(lme4)
# b1<-glmer(NtoTPandTtoP ~ Site*Salinity15cmppt+(1|years),family=binomial(link="cloglog"),data = NN_NT_NP_TT_TP,na.action=na.exclude)#convergence issues, singular, not sure why
# drop1(b1,test="Chisq",.~.)
#Models by site
NN_NT_NP_TT_TP_BP<-NN_NT_NP_TT_TP%>%filter(Site=="Barataria")
NN_NT_NP_TT_TP_BS<-NN_NT_NP_TT_TP%>%filter(Site=="Bayou Sauvage")
NN_NT_NP_TT_TP_BB<-NN_NT_NP_TT_TP%>%filter(Site=="Big Branch")
NN_NT_NP_TT_TP_PR<-NN_NT_NP_TT_TP%>%filter(Site=="Pearl River")
#note when using cloglog (which you are supposed to do when the change of an event is "extremely" low or high) yields pretty much the same p value as the logit. my most extreme case is 7 out of 98, which maybe is not even really that extreme. so i am going to stick with logit
b1 <- glm(NtoTPandTtoP ~ Salinity15cmppt, family = binomial(link="logit"), data = NN_NT_NP_TT_TP_BP, na.action=na.exclude)#18 1's out of 82
#summary(b1)
drop1(b1,test="Chisq",.~.)
b1 <- glm(NtoTPandTtoP ~ Salinity15cmppt, family = binomial(link="logit"), data = NN_NT_NP_TT_TP_BS, na.action=na.exclude)#27 1's out of 81
#summary(b1)
drop1(b1,test="Chisq",.~.)
b1 <- glm(NtoTPandTtoP ~ Salinity15cmppt, family = binomial(link="logit"), data = NN_NT_NP_TT_TP_BB, na.action=na.exclude)#11 1's out of 98
#summary(b1)
drop1(b1,test="Chisq",.~.)
b1 <- glm(NtoTPandTtoP ~ Salinity15cmppt, family = binomial(link="logit"), data = NN_NT_NP_TT_TP_PR, na.action=na.exclude)#7 1's out of 98
#summary(b1)
drop1(b1,test="Chisq",.~.)
##### Salinity and probability of going to more native #####
salnat<-ggplot(data = PP_PN_PT_TT_TN, aes(x = Salinity15cmppt, y = PtoNTandTtoN,color=Site,linetype=Site)) +
theme_classic()+
theme(line=element_line(linewidth =.3),axis.text=element_text(size=9),axis.title = element_text(size = 10),strip.background = element_rect(colour="white", fill="white"),strip.text.x = element_text(size=10,hjust=0,vjust = 1, margin=margin(l=0)),axis.line=element_line(),legend.position="none")+
theme(plot.margin = unit(c(17,5,5,5), "pt"))+#
ylab("") +# toward more native
#ylab("Probability of transition") +# toward more native
xlab("Salinity (ppt)")+
geom_point(size=.8)+
geom_smooth(method='glm',method.args=list(family='binomial'),se=F,size=.5)+
scale_linetype_manual(values = c("dashed", "dashed", "solid", "solid"))
b1 <- glm(PtoNTandTtoN ~ Site*Salinity15cmppt, family = binomial(link="logit"), data = PP_PN_PT_TT_TN, na.action=na.exclude)
drop1(b1,test="Chisq",.~.)
PP_PN_PT_TT_TN_BP<-PP_PN_PT_TT_TN%>%filter(Site=="Barataria")
PP_PN_PT_TT_TN_BS<-PP_PN_PT_TT_TN%>%filter(Site=="Bayou Sauvage")
PP_PN_PT_TT_TN_BB<-PP_PN_PT_TT_TN%>%filter(Site=="Big Branch")
PP_PN_PT_TT_TN_PR<-PP_PN_PT_TT_TN%>%filter(Site=="Pearl River")
b1 <- glm(PtoNTandTtoN ~ Salinity15cmppt, family = binomial(link="logit"), data = PP_PN_PT_TT_TN_BP, na.action=na.exclude)#23 1s out of 62
drop1(b1,test="Chisq",.~.)
sum(PP_PN_PT_TT_TN_BP$PtoNTandTtoN);length(PP_PN_PT_TT_TN_BP$PtoNTandTtoN)
b1 <- glm(PtoNTandTtoN ~ Salinity15cmppt, family = binomial(link="logit"), data = PP_PN_PT_TT_TN_BS, na.action=na.exclude)#16 1s out of 50
drop1(b1,test="Chisq",.~.)
sum(PP_PN_PT_TT_TN_BS$PtoNTandTtoN);length(PP_PN_PT_TT_TN_BS$PtoNTandTtoN)
b1 <- glm(PtoNTandTtoN ~ Salinity15cmppt, family = binomial(link="logit"), data = PP_PN_PT_TT_TN_BB, na.action=na.exclude)#18 1s out of 50
drop1(b1,test="Chisq",.~.)
sum(PP_PN_PT_TT_TN_BB$PtoNTandTtoN);length(PP_PN_PT_TT_TN_BB$PtoNTandTtoN)
b1 <- glm(PtoNTandTtoN ~ Salinity15cmppt, family = binomial(link="logit"), data = PP_PN_PT_TT_TN_PR, na.action=na.exclude)#22 1s out of 50
drop1(b1,test="Chisq",.~.)
sum(PP_PN_PT_TT_TN_PR$PtoNTandTtoN);length(PP_PN_PT_TT_TN_PR$PtoNTandTtoN)
#Water depth
#Looking at variation in water depth by years to see if I should delete years 2017-2019. Overall, there is about a 10-13 cm range in plots at a site in any given year. so it is a little sketchy to attribute a site mean (of say 10) to all the plots, when in reality it would range from 4-16cm. But at some sites like big branch there were the water depths in 2017-19 way higher than the ranges in other years (in which case it would be fine to desingate all plots as 50 when the other years range under 25). Actually because most of the water depth variation is between years rather than within years, it migth be important to ues all the data we can. The full models are more significant with using all data, but the site level regressions are basically the same using 2017-19 vs not. It is also good to use all data b/c otherwise some of the site level regressions only have like 1 transition
temp<-tr2%>%
filter(Site=="Big Branch")
ggplot(temp,aes(x=WaterDepthcm)) +
geom_histogram() +
facet_wrap(~Year)
##### Water depth - Using all years, going more phrag #####
#Figs/Stats for all years
watphrag<-ggplot(data = NN_NT_NP_TT_TP, aes(x = WaterDepthcm, y = NtoTPandTtoP,color=Site,linetype=Site)) +
theme_classic()+
theme(line=element_line(linewidth =.3),axis.text=element_text(size=9),axis.title = element_text(size = 10),strip.background = element_rect(colour="white", fill="white"),strip.text.x = element_text(size=10,hjust=0,vjust = 1, margin=margin(l=0)),axis.line=element_line(),legend.position="none")+
theme(plot.margin = unit(c(17,5,5,5), "pt"))+#
ylab("Probability of transition") +# toward more Phragmites
xlab("Water depth (cm)")+
geom_point(size=0.8)+
geom_smooth(method='glm',method.args=list(family='binomial'),se=F, size=.5)+
scale_linetype_manual(values = c("dashed", "dashed", "solid", "dashed"))
b1 <- glm(NtoTPandTtoP ~ Site*WaterDepthcm, family = binomial(link="logit"), data = NN_NT_NP_TT_TP, na.action=na.exclude)
drop1(b1,test="Chisq",.~.)
#I don't trust the BS data b/c only 4 of 81 plots had waterdepth>0
data.frame(NN_NT_NP_TT_TP_BS%>%filter(WaterDepthcm>0))
length(NN_NT_NP_TT_TP_BS$WaterDepthcm)
#Stats for all years
b1 <- glm(NtoTPandTtoP ~ WaterDepthcm, family = binomial(link="logit"), data = NN_NT_NP_TT_TP_BP, na.action=na.exclude)#14 1s out of 82
drop1(b1,test="Chisq",.~.)
sum(NN_NT_NP_TT_TP_BP$NtoTPandTtoP);length(NN_NT_NP_TT_TP_BP$NtoTPandTtoP)
b1 <- glm(NtoTPandTtoP ~ WaterDepthcm, family = binomial(link="logit"), data = NN_NT_NP_TT_TP_BS, na.action=na.exclude)#27 1s to 81
drop1(b1,test="Chisq",.~.)
sum(NN_NT_NP_TT_TP_BS$NtoTPandTtoP);length(NN_NT_NP_TT_TP_BS$NtoTPandTtoP)
b1 <- glm(NtoTPandTtoP ~ WaterDepthcm, family = binomial(link="logit"), data = NN_NT_NP_TT_TP_BB, na.action=na.exclude)#11 1s out of 98
drop1(b1,test="Chisq",.~.)
sum(NN_NT_NP_TT_TP_BB$NtoTPandTtoP);length(NN_NT_NP_TT_TP_BB$NtoTPandTtoP)
b1 <- glm(NtoTPandTtoP ~ WaterDepthcm, family = binomial(link="logit"), data = NN_NT_NP_TT_TP_PR, na.action=na.exclude)#7 1s out of 98
drop1(b1,test="Chisq",.~.)
sum(NN_NT_NP_TT_TP_PR$NtoTPandTtoP);length(NN_NT_NP_TT_TP_PR$NtoTPandTtoP)
##### Water depth - Using all years, going more native #####
watnat<-ggplot(data = PP_PN_PT_TT_TN, aes(x = WaterDepthcm, y = PtoNTandTtoN,color=Site,linetype=Site)) +
theme_classic()+
theme(line=element_line(linewidth =.3),axis.text=element_text(size=9),axis.title = element_text(size = 10),strip.background = element_rect(colour="white", fill="white"),strip.text.x = element_text(size=10,hjust=0,vjust = 1, margin=margin(l=0)),axis.line=element_line(),legend.position="none")+
theme(plot.margin = unit(c(17,5,5,5), "pt"))+#
ylab("") +# toward more native
#ylab("Probability of a transition") +# toward more native
xlab("Water depth (cm)")+
geom_point(size=0.8)+
geom_smooth(method='glm',method.args=list(family='binomial'),se=F,size=0.5)+
scale_linetype_manual(values = c("solid", "dashed", "blank", "dashed"))
b1 <- glm(PtoNTandTtoN ~ Site*WaterDepthcm, family = binomial(link="logit"), data = PP_PN_PT_TT_TN, na.action=na.exclude)
drop1(b1,test="Chisq",.~.)
#test
#without BS
PP_PN_PT_TT_TNnoBS<-PP_PN_PT_TT_TN%>%
filter(Site!="Bayou Sauvage")
b1 <- glm(PtoNTandTtoN ~ Site*WaterDepthcm, family = binomial(link="logit"), data = PP_PN_PT_TT_TNnoBS, na.action=na.exclude)
drop1(b1,test="Chisq",.~.)
#I don't trust the BS data b/c only 1 of 50 plots had waterdepth>0
data.frame(PP_PN_PT_TT_TN_BS%>%filter(WaterDepthcm>0))
length(PP_PN_PT_TT_TN_BS$WaterDepthcm)
#Stats on all years
b1 <- glm(PtoNTandTtoN ~ WaterDepthcm, family = binomial(link="logit"), data = PP_PN_PT_TT_TN_BP, na.action=na.exclude)#23 1s of 62
drop1(b1,test="Chisq",.~.)
sum(PP_PN_PT_TT_TN_BP$PtoNTandTtoN);length(PP_PN_PT_TT_TN_BP$PtoNTandTtoN)
b1 <- glm(PtoNTandTtoN ~ WaterDepthcm, family = binomial(link="logit"), data = PP_PN_PT_TT_TN_BS, na.action=na.exclude)# 16 1s of 50
drop1(b1,test="Chisq",.~.)
sum(PP_PN_PT_TT_TN_BS$PtoNTandTtoN);length(PP_PN_PT_TT_TN_BS$PtoNTandTtoN)
b1 <- glm(PtoNTandTtoN ~ WaterDepthcm, family = binomial(link="logit"), data = PP_PN_PT_TT_TN_BB, na.action=na.exclude)#18 1s of 50
drop1(b1,test="Chisq",.~.)
sum(PP_PN_PT_TT_TN_BB$PtoNTandTtoN);length(PP_PN_PT_TT_TN_BB$PtoNTandTtoN)
b1 <- glm(PtoNTandTtoN ~ WaterDepthcm, family = binomial(link="logit"), data = PP_PN_PT_TT_TN_PR, na.action=na.exclude)#22 of 50
drop1(b1,test="Chisq",.~.)
sum(PP_PN_PT_TT_TN_PR$PtoNTandTtoN);length(PP_PN_PT_TT_TN_PR$PtoNTandTtoN)
##### Four panel transition plot #####
salwatlegend<-
ggplot(data = PP_PN_PT_TT_TN, aes(x = WaterDepthcm, y = PtoNTandTtoN,fill=Site,color=Site)) +#color=Site
theme_classic()+
theme(line=element_line(linewidth =.3),axis.text=element_text(size=9),axis.title = element_text(size = 10),strip.background = element_rect(colour="white", fill="white"),strip.text.x = element_text(size=10,hjust=0,vjust = 1, margin=margin(l=0)),axis.line=element_line())+
theme(legend.text=element_text(size=8))+
theme(legend.spacing.x = unit(.30, 'cm'))+
#guides(fill = guide_legend(byrow = TRUE)) +
#theme(legend.key.spacing.y=unit(.1,'cm'))+
geom_point(size=0.8)+
geom_smooth(method='glm',method.args=list(family='binomial'),se=F,size=0.5)+
theme(legend.spacing.y = unit(-1.5, 'mm')) +
guides(fill = guide_legend(byrow = TRUE))
#to use the legend.spacing.y you need to you use "fill=Site" not color=Site in the iniitial aes, i have no idea why this matters
#need to mess with plot margins to get this nicer
pdf("/Users/farrer/Dropbox/EmilyComputerBackup/Documents/LAmarsh/Survey/Manuscripts/Temporal/Figs/FourPanelTransitionPlot.pdf",width=6,height=4)#width=12.4,height=3
legend <- cowplot::get_legend(salwatlegend)
plot_grid(salphrag,salnat,get_legend(salphrag),watphrag,watnat,legend,nrow = 2,ncol=3,labels=c("a) Toward Phragmites dominance","b) Toward native dominance","","c) Toward Phragmites dominance","d) Toward native dominance"),label_size=10,hjust=-.17,vjust=1.2,scale=1,label_fontface = "plain",rel_widths=c(1,1,.5))
dev.off()
#Trying a few sites (not bayou sauvage) with both water depth and salinity, things are similar except not sig at bayou sauvage
b1 <- glm(PtoNTandTtoN ~ Salinity15cmppt + WaterDepthcm, family = binomial(link="logit"), data = PP_PN_PT_TT_TN_BP, na.action=na.exclude)#
drop1(b1,test="Chisq",.~.)
b1 <- glm(PtoNTandTtoN ~ Salinity15cmppt + WaterDepthcm, family = binomial(link="logit"), data = PP_PN_PT_TT_TN_BS, na.action=na.exclude)#
drop1(b1,test="Chisq",.~.)
b1 <- glm(PtoNTandTtoN ~ Salinity15cmppt + WaterDepthcm, family = binomial(link="logit"), data = PP_PN_PT_TT_TN_PR, na.action=na.exclude)#
drop1(b1,test="Chisq",.~.)
b1 <- glm(PtoNTandTtoN ~ Salinity15cmppt + WaterDepthcm, family = binomial(link="logit"), data = PP_PN_PT_TT_TN_BB, na.action=na.exclude)#
drop1(b1,test="Chisq",.~.)
##### Using 2020-2023 only, since we don't have good plot level water depth 2017-2019 #####
NN_NT_NP_TT_TP2<-NN_NT_NP_TT_TP%>%
filter(Year>2019)
#Toward more phrag
ggplot(data = NN_NT_NP_TT_TP2, aes(x = WaterDepthcm, y = NtoTPandTtoP,color=Site,linetype=Site)) +
ylab("Probability of a transition toward more Phragmites") +
xlab("Water depth (cm)")+
geom_point()+
geom_smooth(method='glm',method.args=list(family='binomial'),se=F)+
scale_linetype_manual(values = c("dashed", "solid", "dashed", "dashed"))
b1 <- glm(NtoTPandTtoP ~ Site*WaterDepthcm, family = binomial(link="logit"), data = NN_NT_NP_TT_TP2, na.action=na.exclude)
drop1(b1,test="Chisq",.~.)
#I don't trust the BS data b/c only 4 of 55 plots had waterdepth>0
data.frame(NN_NT_NP_TT_TP2_BS%>%filter(WaterDepthcm>0))
length(NN_NT_NP_TT_TP2_BS$WaterDepthcm)
#Stats for 2020-2023
NN_NT_NP_TT_TP2_BP<-NN_NT_NP_TT_TP2%>%filter(Site=="Barataria")
NN_NT_NP_TT_TP2_BS<-NN_NT_NP_TT_TP2%>%filter(Site=="Bayou Sauvage")
NN_NT_NP_TT_TP2_BB<-NN_NT_NP_TT_TP2%>%filter(Site=="Big Branch")
NN_NT_NP_TT_TP2_PR<-NN_NT_NP_TT_TP2%>%filter(Site=="Pearl River")
b1 <- glm(NtoTPandTtoP ~ WaterDepthcm, family = binomial(link="cloglog"), data = NN_NT_NP_TT_TP2_BP, na.action=na.exclude)# 8 1s of 54
drop1(b1,test="Chisq",.~.)
sum(NN_NT_NP_TT_TP2_BP$NtoTPandTtoP);length(NN_NT_NP_TT_TP2_BP$NtoTPandTtoP)
b1 <- glm(NtoTPandTtoP ~ WaterDepthcm, family = binomial(link="cloglog"), data = NN_NT_NP_TT_TP2_BS, na.action=na.exclude)#22 1s of 55
drop1(b1,test="Chisq",.~.)
sum(NN_NT_NP_TT_TP2_BS$NtoTPandTtoP);length(NN_NT_NP_TT_TP2_BS$NtoTPandTtoP)
b1 <- glm(NtoTPandTtoP ~ WaterDepthcm, family = binomial(link="cloglog"), data = NN_NT_NP_TT_TP2_BB, na.action=na.exclude)# 9 1s of 71
drop1(b1,test="Chisq",.~.)
sum(NN_NT_NP_TT_TP2_BB$NtoTPandTtoP);length(NN_NT_NP_TT_TP2_BB$NtoTPandTtoP)
b1 <- glm(NtoTPandTtoP ~ WaterDepthcm, family = binomial(link="cloglog"), data = NN_NT_NP_TT_TP2_PR, na.action=na.exclude)# 1 1s of 70
drop1(b1,test="Chisq",.~.)
sum(NN_NT_NP_TT_TP2_PR$NtoTPandTtoP);length(NN_NT_NP_TT_TP2_PR$NtoTPandTtoP)
#Towards less phrag
#2020-2023 only
PP_PN_PT_TT_TN2<-PP_PN_PT_TT_TN%>%
filter(Year>2019)
ggplot(data = PP_PN_PT_TT_TN2, aes(x = WaterDepthcm, y = PtoNTandTtoN,color=Site,linetype=Site)) +
ylab("Probability of a transition toward more native") +
xlab("Water depth (cm)")+
geom_point()+
geom_smooth(method='glm',method.args=list(family='binomial'),se=F)+
scale_linetype_manual(values = c("solid", "dashed", "dashed", "dashed"))
b1 <- glm(PtoNTandTtoN ~ Site*WaterDepthcm, family = binomial(link="logit"), data = PP_PN_PT_TT_TN2, na.action=na.exclude)
drop1(b1,test="Chisq",.~.)
#I don't trust the BS data b/c only 1 plots had waterdepth>0
data.frame(PP_PN_PT_TT_TN2_BS%>%filter(WaterDepthcm>0))
data.frame(PP_PN_PT_TT_TN_BP%>%filter(WaterDepthcm>15))
#Stats on 2020-2023
PP_PN_PT_TT_TN2_BP<-PP_PN_PT_TT_TN2%>%filter(Site=="Barataria")
PP_PN_PT_TT_TN2_BS<-PP_PN_PT_TT_TN2%>%filter(Site=="Bayou Sauvage")
PP_PN_PT_TT_TN2_BB<-PP_PN_PT_TT_TN2%>%filter(Site=="Big Branch")
PP_PN_PT_TT_TN2_PR<-PP_PN_PT_TT_TN2%>%filter(Site=="Pearl River")
b1 <- glm(PtoNTandTtoN ~ WaterDepthcm, family = binomial(link="cloglog"), data = PP_PN_PT_TT_TN2_BP, na.action=na.exclude)#
drop1(b1,test="Chisq",.~.)
b1 <- glm(PtoNTandTtoN ~ WaterDepthcm, family = binomial(link="cloglog"), data = PP_PN_PT_TT_TN2_BS, na.action=na.exclude)#
drop1(b1,test="Chisq",.~.)
b1 <- glm(PtoNTandTtoN ~ WaterDepthcm, family = binomial(link="cloglog"), data = PP_PN_PT_TT_TN2_BB, na.action=na.exclude)#
drop1(b1,test="Chisq",.~.)
b1 <- glm(PtoNTandTtoN ~ WaterDepthcm, family = binomial(link="cloglog"), data = PP_PN_PT_TT_TN2_PR, na.action=na.exclude)#
drop1(b1,test="Chisq",.~.)
##### Contingency tables #####
## For each site, 2X2 contingency table of
## Blah this doesn't work b/c the T stasis is in there twice, and you'd add them together to get the second column sum which doesn't make sense
# to pair stasis
# N,T
# T,P
tr2BP<-tr2%>%filter(Site=="Barataria")
data.frame(tr2BP)
tbl<-table(tr2BP$State1,tr2BP$State2);tbl
table(tr2BP$State1,tr2BP$NtoTPandTtoP)
chisq.test(tbl)
#Note about contingency tables: using a chi sq test assumes that you have a lot of data. the chi 2 test in general relies on an approximation. if you have n<5, you could use a continuity correction which is an approximation to the fisher's exact test. But really you should use a fishers exact test in the first place. fisher's exact test is computationally intensive but any computer can now do it.
#https://stats.stackexchange.com/questions/362517/when-to-switch-off-the-continuity-correction-in-chisq-test-function
#however on further exploration, the fisher exact tests also makes some assumptions, for example that that row and column totals are fixed. also you can't do it on a 2 x 1 table.
#https://www.graphpad.com/guides/prism/latest/statistics/stat_chi-square_or_fishers_test.htm
#so for now I will keep it as is.
#alternatively I could use "simulate.p.value" but I think that this makes the same assumptions of the fisher exact test (in terms of row and column marginals)
#alternatively I could use a g.test but despite what Claudia's paper says, it sill suffers when samples size <5
###### Persistence of Phrag within a site ######
# Stasis to other
# one site
#
tr2BP<-tr2%>%
filter(Site=="Barataria")%>%
filter(State1=="P")
#data.frame(tr2BP)
tbl<-table(tr2BP$Site,tr2BP$PtoNT);tbl
chisq.test(tbl, correct = FALSE)
chisq.test(tbl,correct=F,simulate.p.value = T)
g.test(tbl) #library(AMR)
##BS is persistent***
tr2BS<-tr2%>%
filter(Site=="Bayou Sauvage")%>%
filter(State1=="P")
#data.frame(tr2BS)
tbl<-table(tr2BS$Site,tr2BS$PtoNT);tbl
chisq.test(tbl, correct = FALSE)
assocstats(tbl)
tr2BB<-tr2%>%
filter(Site=="Big Branch")%>%
filter(State1=="P")
#data.frame(tr2BB)
tbl<-table(tr2BB$Site,tr2BB$PtoNT);tbl
chisq.test(tbl, correct = FALSE)
tr2PR<-tr2%>%
filter(Site=="Pearl River")%>%
filter(State1=="P")
#data.frame(tr2PR)
tbl<-table(tr2PR$Site,tr2PR$PtoNT);tbl
chisq.test(tbl, correct = FALSE)
#prop.test(tbl,correct=F) I'm not exactly sure why we wouldn't use this, the results are similar but not exactly that of the chisq test. but from the help file on chisq.test, it is valid to do a 1x2 chi squared test so I will stick with that.
###### Persistence of Transition within a site ######
#note 0 is changing, 1 is staying the same
tr2BP<-tr2%>%
filter(Site=="Barataria")%>%
filter(State1=="T")
#data.frame(tr2BP)
tbl<-table(tr2BP$Site,tr2BP$TT);tbl
chisq.test(tbl, correct = FALSE)
tr2BS<-tr2%>%
filter(Site=="Bayou Sauvage")%>%
filter(State1=="T")
#data.frame(tr2BS)
tbl<-table(tr2BS$Site,tr2BS$TT);tbl
chisq.test(tbl, correct = FALSE)
#BB T is nearly significant p=0.05551