-
Notifications
You must be signed in to change notification settings - Fork 0
/
Understory.R
1547 lines (1282 loc) · 58.7 KB
/
Understory.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
##===================== TITLE =========================
##...................................................##
##...US National Park Service........................##
##...Pacific Island Inventory & Monitoring Network...##
##...................................................##
##...J. Gross & L. Moore 06/09/2021..................##
##...................................................##
##...Briefing report.................................##
##...FTPC - Focal Terrestrial Plant Communities......##
##...................................................##
##....... Understory Cover Stats and Graphs..........##
##...................................................##
#'* NOTE - Before running this script for first time: *
#'* Export FTPC data from database using script 01_FTPC_DB_YYYYMMDD *
#'* All PACN vegetation R scrips are located on I drive: *
#'* 'I:\vital_signs\05_focal_terr_plant_communities\Documents\R_scripts' *
#.-----------------------------------------------------
# Packages ----
#.......................................................
library(gridExtra)
library(ggrepel)
library(tidytext)
library(lubridate)
library(here)
library(tidyverse)
#.-----------------------------------------------------
# Custom Functions ----
#.......................................................
site.filter <- function(x, UC = "All", C = "All", SF = "All"){
y <- x %>%
mutate(S_Year = year(Start_Date)) %>%
# Create column for Sample Cycle
mutate(S_Cycle = case_when(S_Year <= 2014 ~ "1",
S_Year >= 2015 & S_Year < 2020 ~ "2",
S_Year > 2020 ~ "3")) %>%
# Remove QA Plots
filter(QA_Plot == FALSE) %>%
# Apply site filters
{if(C!="All") filter(.,Community == C) else .} %>%
{if(SF!="All") filter(.,Sampling_Frame == SF) else .} %>%
{if(UC!="All") filter(.,Unit_Code == UC) else .}
return(y)
}
add.stats <- function(.data, .summary_var, ...){
# dataset needs to have columns: "Unit_Code, Sampling_Frame, Strata"
# column is the dataset$column that shows the change between two cycles
.summary_var <- enquo(.summary_var)
#.group_vars <- enquos(...)
.data %>%
dplyr::group_by(...) %>%
dplyr::summarise(NPLOTS = n(),
MEAN_CHG = round(mean(!!.summary_var),3),
MED_CHG = round(median(!!.summary_var),3),
MIN_CHG = round(min(!!.summary_var),3),
MAX_CHG = round(max(!!.summary_var),3),
SD_CHG = sd(!!.summary_var),
ERR_CHG = qt(0.975,df=NPLOTS-1)*(SD_CHG/sqrt(NPLOTS)),
L_CHG = MEAN_CHG - ERR_CHG,
R_CHG = MEAN_CHG + ERR_CHG,
MEAN_1 = round(mean(`1`),3),
MED_1 = round(median(`1`),3),
MIN_1 = round(min(`1`),3),
MAX_1 = round(max(`1`),3),
SD_1 = sd(`1`),
ERR_1 = qt(0.975,df=NPLOTS-1)*(SD_1/sqrt(NPLOTS)),
L_1 = MEAN_1-ERR_1,
R_1 = MEAN_1+ERR_1,
MEAN_2 = round(mean(`2`),3),
MED_2 = round(median(`2`),3),
MIN_2 = round(min(`2`),3),
MAX_2 = round(max(`2`),3),
SD_2 = sd(`2`),
ERR_2 = qt(0.975,df=NPLOTS-1)*(SD_2/sqrt(NPLOTS)),
L_2 = MEAN_2-ERR_2,
R_2 = MEAN_2+ERR_2,) %>%
tidyr::pivot_longer(
# period (.) means match any character
# asterisk (*) means match zero or more times
# use () to distinguish groups
# therefore the following breaks apart two words separated by underscore (_)
cols = MEAN_CHG:R_2,
names_to = c(".value", "S_Cycle"),
names_pattern = "(.*)_(.*)") %>%
dplyr::arrange(S_Cycle)
}
#.-----------------------------------------------------
# Loading Data ----
#.......................................................
list.files()
list.files("data/FTPC_Export")
# Select last folder in FTPC_Export
DB_download <- tail(list.files("data/FTPC_Export"), n=1)
print(DB_download)
# Read tables
Cover_High <- read_csv(here("data/FTPC_Export", DB_download, "Species_coverage_High.csv"))
Cover_Low <- read_csv(here("data/FTPC_Export", DB_download, "Species_coverage_Low.csv"))
Event <- read_csv(here("data/FTPC_Export", DB_download, "Event.csv"))
# Add folders for tables (tbls) and figures (figs), if not already created.
figs <- here("tbls")
tbls <- here("figs")
dir.create(tbls, showWarnings = FALSE)
dir.create(figs, showWarnings = FALSE)
#.-----------------------------------------------------
# Apply Site Filter Function ----
#.......................................................
# User selected subsets
levels(as.factor(Event$Unit_Code))
uc <- "HALE"
levels(as.factor(Event$Community))
c <- "All"
levels(as.factor(Event$Sampling_Frame))
sf <- "Subalpine Shrubland"
Event_filter <- site.filter(Event, uc, c, sf) %>%
select(S_Year, S_Cycle, Unit_Code, Sampling_Frame,
Plot_Type, Plot_Number, Plot_ID, Event_ID)
Cover_High_filter <- site.filter(Cover_High, uc, c, sf)
Cover_Low_filter <- site.filter(Cover_Low, uc, c, sf)
#.-----------------------------------------------------
# Paired Plots ----
#.......................................................
# display total number of fixed and rotational plots in subset
table(Event_filter$Plot_Type)
# Plot totals Fixed and Rotational separate
plot_totals <- Event_filter %>%
group_by(S_Cycle, S_Year, Unit_Code, Sampling_Frame, Plot_Type) %>%
summarise(plot_ss = n(), .groups = 'drop')
#plot_ss = plot sample size
plot_totals
# Plot totals Fixed and Rotationals combined
plot_totals_all <- Event_filter %>%
group_by(S_Cycle, S_Year, Unit_Code, Sampling_Frame) %>%
summarise(plot_ss = n(), .groups = 'drop')
# plot_ss = plot sample size
plot_totals_all
# Which plots are paired matches between years
fixed_plots <- Event_filter %>%
filter(Plot_Type=="Fixed") %>%
select(-S_Year,-Plot_Type,-Event_ID) %>%
mutate(Sampled = TRUE) %>%
complete(S_Cycle, # Complete a data frame with missing combinations of factors
# nesting = find only the combinations that occur in the selected factors
nesting(Unit_Code, Sampling_Frame, Plot_Number, Plot_ID),
fill = list(Sampled = FALSE))
# If fixed plots without 2 pairs (non-paired) exist display here
fixed_plots[fixed_plots$Sampled==FALSE,]
# Fixed plots sampled during Cycle 1 and Cycle 2
plots_f_match <- fixed_plots %>%
pivot_wider(names_from = S_Cycle, values_from = Sampled) %>%
filter(`1` == TRUE & `2` == TRUE)
plots_f_match
paired_plots <- nrow(plots_f_match)
paste("Number of Paired Plots =", paired_plots)
#.-----------------------------------------------------
# Fixed Plot Subset ----
#.......................................................
Cov_Fixed_Low <- plots_f_match %>%
left_join(Cover_Low_filter,
by = c("Unit_Code", "Sampling_Frame",
"Plot_Number", "Plot_ID"))
Cov_Fixed_High <- plots_f_match %>%
left_join(Cover_High_filter,
by = c("Unit_Code", "Sampling_Frame",
"Plot_Number", "Plot_ID"))
Cover_Fixed <- bind_rows(UNDERSTORY2 = Cov_Fixed_High,
UNDERSTORY1 = Cov_Fixed_Low, .id = "Strata") %>%
mutate(Strata = as.factor(Strata)) %>%
mutate(Strata = fct_rev(Strata)) %>%
mutate(Plot_Number = as.factor(Plot_Number)) %>%
mutate(S_Cycle = as.factor(S_Cycle)) %>%
filter(is.na(Dead) | Dead == FALSE) %>%
select(S_Cycle, S_Year, Unit_Code, Sampling_Frame, Plot_Type, Plot_Number,
Transect, Point, Strata, Nativity, Life_form, Code, Name,
Center_X_Coord, Center_Y_Coord, UTM_Zone, Datum)
#.-----------------------------------------------------
# Leaflet ----
#.......................................................
# Cover currently only has UTM coordinates so have to convert to lat long-----
library(leaflet)
library(terra)
points <- Cover_Fixed %>%
select(Plot_Number, Center_X_Coord, Center_Y_Coord) %>%
rename(x = Center_X_Coord) %>%
rename(y = Center_Y_Coord) %>%
distinct()
points.matrix <- points %>%
select(-Plot_Number)
points.matrix <- as.matrix(points.matrix)
v <- vect(points.matrix, crs="+proj=utm +zone=04 +datum=NAD83 +units=m")
y <- project(v, "+proj=longlat +datum=WGS84")
plots.xy <- geom(y)[, c("x", "y")]
plots.xy <- as_tibble(plots.xy) %>%
bind_cols(points) %>%
rename(lng = x...1) %>%
rename(lat = y...2)
#------------------------------------------------------------------------
leaflet(plots.xy) %>%
addProviderTiles(providers$OpenTopoMap) %>%
addCircleMarkers(
radius = 8,
color = "navy",
stroke = FALSE, fillOpacity = 0.4,
label = plots.xy$Plot_Number,
labelOptions = labelOptions(noHide = T,
direction = "center",
textOnly = T,
style = list("color" = "white"))
)
#.......................................................
# Lump Spp & Update Spp Info----
#.......................................................
Cover <- Cover_Fixed
#'* If IDs are uncertain for some species lump by "Code" here: *
# Check Codes/Species
table(Cover_Fixed$Code)
table(Cover_Fixed$Name)
# Get unique names
name_code <- Cover %>%
group_by(Nativity, Name, Code, Life_form) %>%
summarize(n = n())
#Cover <- Cover %>%
# Lifeforms:
#mutate(Life_form=replace(Life_form, Code=="CIBSP.", "Tree Fern")) %>%
# # Species:
# # Epipremnum pinnatum is accepted name ITIS, Bishop, and is introduced/naturalized (cultivar Aureum which originated in the Solomon Islands? (Wagner))
# mutate(Code=replace(Code, Code=="EPISP.", "EPIPIN")) %>%
# mutate(Name=replace(Name, Name=="Epipremnum sp.", "Epipremnum pinnatum")) %>%
# # The majority of Mucuna sp. is likely 'M. ginantea', but other spp. are possible.
# mutate(Code=replace(Code, Code=="MUCSPP", "MUCGIG2")) %>%
# mutate(Name=replace(Name, Name=="Mucuna gigantea", "Mucuna sp.")) %>%
# # The majority of Ipomoea sp. is likely 'I. violacea', but other spp. are possible.
# mutate(Code=replace(Code, Code=="IPOVIO", "IPOSP.")) %>%
# mutate(Name=replace(Name, Name=="Ipomoea violacea", "Ipomoea sp.")) %>%
# # The majority of Ficus sp. is likely 'F. prolixa' & 'F.tinctoria, but other spp. are possible.
# mutate(Code=replace(Code, Code=="FICPRO", "FICSP.")) %>%
# mutate(Name=replace(Name, Name=="Ficus prolixa", "Ficus sp.")) %>%
# mutate(Code=replace(Code, Code=="FICTIN1", "FICSP.")) %>%
# mutate(Name=replace(Name, Name=="Ficus tinctoria", "Ficus sp.")) %>%
# # The majority of Nephrolepis sp. is likely 'N. hirsutula', but other spp. are possible.
# mutate(Code=replace(Code, Code=="NEPHIR", "NEPSP.")) %>%
# mutate(Name=replace(Name, Name=="Nephrolepis hirsutula", "Nephrolepis sp."))
Cover <- Cover %>%
# Change Code column: "FESRUB" to "POAPRA" (Cycle 1 only)
mutate(Code = case_when(
Code == "FESRUB" & S_Cycle == 1 & Plot_Number == 3 ~ "POAPRA",
TRUE ~ Code)) %>%
# Change Name column: "Festuca rubra" to "Poa pratensis" (Cycle 1 only)
mutate(Name = case_when(
Name == "Festuca rubra" & S_Cycle == 1 & Plot_Number == 3 ~ "Poa pratensis",
TRUE ~ Name))
# mutate(Code=replace(Code, Code=="SADSOU", "SADSP.")) %>%
# mutate(Name=replace(Name, Name=="Sadleria souleyetiana", "Sadleria sp.")) %>%
# mutate(Code=replace(Code, Code=="SADCYA", "SADSP.")) %>%
# mutate(Name=replace(Name, Name=="Sadleria cyatheoides", "Sadleria sp.")) %>%
# mutate(Code=replace(Code, Code=="SADPAL", "SADSP.")) %>%
# mutate(Name=replace(Name, Name=="Sadleria pallida", "Sadleria sp."))
#
# Cover <- Cover %>%
# mutate(Code=replace(Code, Code=="STECAL", "STESP.")) %>%
# mutate(Name=replace(Name, Name=="Stenogyne calaminthoides", "Stenogyne sp."))
#
# Cover <- Cover %>%
# mutate(Life_form=replace(Life_form, Code=="MELSP.", "Tree")) %>%
# mutate(Code=replace(Code, Code=="MELSP.", "MELCLU")) %>%
# mutate(Name=replace(Name, Name=="Melicope sp.", "Melicope clusiifolia"))
#
# Cover <- Cover %>%
# mutate(Code=replace(Code, Code=="ASPSPH", "ASPSP.2")) %>%
# mutate(Name=replace(Name, Name=="Asplenium sphenotomum", "Asplenium sp."))
# Check if Lumped Correctly
name_code_lump <- Cover %>%
group_by(Nativity, Name, Code, Life_form) %>%
summarize(n = n())
name_code_lump %>%
group_by(Name) %>%
filter(n()>1)
# If same species being lumped occurs at same point, lump records
# together into one hit, so not to double count.
Cover <- Cover %>%
distinct(S_Cycle, Unit_Code, Sampling_Frame, Plot_Number,
Transect, Point, Strata, Nativity, Code, .keep_all = TRUE)
#.......................................................
# Remove Nativity Unknown?----
#.......................................................
#'*If appropriate, remove records that show Nativity == Unknown*
table(Cover$Nativity)
# Display "Unknown" Nativity species
Cover %>%
filter(Nativity == "Unknown") %>%
group_by(S_Cycle, Unit_Code, Sampling_Frame, Code, Name) %>%
summarise(n = n())
# Remove if needed...
# Cover <- Cover %>%
# filter(Nativity != "Unknown")
# If Nativity is wrong update here:
Cover <- Cover %>%
# #Non-natives:
mutate(Nativity=replace(Nativity, Code=="Unk_Grass1", "Non-Native")) #%>%
# mutate(Nativity=replace(Nativity, Name=="Epipremnum pinnatum", "Non-Native")) %>%
# mutate(Nativity=replace(Nativity, Name=="Stachytarpheta sp.", "Non-Native")) %>%
# #Natives:
# mutate(Nativity=replace(Nativity, Name=="Hernandia sp.", "Native")) %>%
# mutate(Nativity=replace(Nativity, Name=="Ipomoea sp.", "Native")) %>%
# mutate(Nativity=replace(Nativity, Name=="Nephrolepis sp.", "Native")) %>%
# mutate(Nativity=replace(Nativity, Name=="Thelypteris sp.", "Native")) %>%
# mutate(Nativity=replace(Nativity, Name=="Mucuna sp.", "Native")) %>%
# mutate(Nativity=replace(Nativity, Name=="Ficus sp.", "Native"))
# Lifeform updates:
Cover %>%
group_by(Life_form, Nativity) %>%
summarize(n = n())
Cover %>%
filter(is.na(Life_form)) %>%
group_by(Name, Code) %>%
summarize(n = n())
Cover <- Cover %>%
mutate(Life_form=replace(Life_form, Code=="PINPAT", "Tree")) %>%
mutate(Life_form=replace(Life_form, Code=="RYTSEM", "Grass")) %>%
mutate(Life_form=replace(Life_form, Code=="SANHAL", "Tree"))
# lf.update <- read_csv(here("data", "AMME_lifeform_update.csv")) %>%
# mutate(LF_update = Life_form) %>%
# select(-Name, -n, -Life_form)
# Cover <- Cover %>%
# left_join(lf.update, by = "Code") %>%
# mutate(Life_form = case_when(is.na(Life_form) ~ LF_update,
# TRUE ~ Life_form))
Cover %>%
group_by(Life_form, Nativity) %>%
summarize(n = n())
# NA updates:
Cover %>%
filter(is.na(Nativity))
Cover <- Cover %>%
drop_na(Transect)
Cover %>%
filter(is.na(Nativity))
# "Cover" ----
# Dataset is ready for analysis
#.-----------------------------------------------------
# ***** Sections ***** ----
#......................................................
# Sections start with most general (total cover) and proceed
# to most specific (species x plot)
# ""Cover" dataframe is the main dataset used at beginning of
# each following sections:
# 1) Total % cover (Tot_Cov)
# 2) Nativity Total % cover (Nat_Cov)
# 3) Nativity Richness (Nat_Rich)
# 4) Species % Cover (Spp_Cov)
# 5) Species Presence (Spp_Pres)
#.-----------------------------------------------------
# 1) Total % cover ----
#.......................................................
# Total can be greater Than 100%
Tot_Cov <- Cover %>%
group_by(S_Cycle, Unit_Code, Sampling_Frame, Plot_Number,
Transect, Point, Strata) %>%
# Count hits at each cover point:
summarise(Hits_All_Sp = n(), .groups = 'drop') %>%
# But don't count record if entire plot had no hits: (e.g. transect is NA )
mutate(Hits_All_Sp = replace(Hits_All_Sp, is.na(Transect), 0)) %>%
# group by plot (i.e. remove Transect and Point from grouping variable)
group_by(S_Cycle, Unit_Code, Sampling_Frame, Plot_Number, Strata) %>%
#Total hits at each point for each strata for entire plot
# (can be > 300 points or >100%)
summarise(tot_pct_cov = (sum(Hits_All_Sp)) / 300 * 100, .groups = 'drop')
#........DENSITY PLOT ----
means <- Tot_Cov %>%
group_by(S_Cycle, Unit_Code, Sampling_Frame, Strata) %>%
summarise(mean = mean(tot_pct_cov), .groups = 'drop') %>%
mutate(mean = round(mean,1))
ggplot(Tot_Cov, aes(x = tot_pct_cov, fill = S_Cycle)) +
geom_density(alpha = 0.4) +
geom_vline(data = means, aes(xintercept = mean, color= S_Cycle)) +
geom_text(data = means, aes(x = mean, label =mean),
y = 0.01, angle = 90, vjust = -0.2, size = 3) +
scale_fill_brewer(palette="Accent") +
scale_color_brewer(palette="Accent") +
facet_grid(rows = vars(Strata), cols = vars(Sampling_Frame)) +
xlim(0, max(Tot_Cov$tot_pct_cov) + 20)
#........BAR COV/PLOT# ----
Tot_Cov %>%
ggplot(aes(x = reorder_within(Plot_Number, desc(tot_pct_cov), Strata), y = tot_pct_cov, fill = S_Cycle)) +
geom_col(position = position_dodge()) +
scale_fill_brewer(palette="Accent") +
facet_wrap(vars(Strata), dir = "v", scales = "free_x") +
ylab("Total % Cover") + xlab("Plot Number") +
scale_x_reordered()
# Change ----
Tot_Cov_Chg <- Tot_Cov %>%
ungroup() %>%
#group_by("Unit_Code", "Sampling_Frame","Plot_Number","Nativity") %>%
complete(S_Cycle, # Complete a data frame with missing combinations of factors
# nesting = find only the combinations that occur in the selected factors
nesting(Unit_Code, Sampling_Frame, Plot_Number, Strata),
fill = list(tot_pct_cov = 0)) %>%
pivot_wider(names_from = S_Cycle, values_from = tot_pct_cov) %>%
mutate(tot_pct_cov_chg = round(`2` - `1`, 2))
#........BAR CHG/PLOT# ----
Tot_Cov_Chg %>%
mutate(direction = case_when(tot_pct_cov_chg > 0 ~ "Pos",
tot_pct_cov_chg < 0 ~ "Neg" )) %>%
ggplot(aes(x = reorder_within(Plot_Number, desc(tot_pct_cov_chg), Strata), y = tot_pct_cov_chg, fill = direction)) +
geom_col(position = position_dodge()) +
scale_fill_manual(values = c("#CC0000", "#009900")) +
#scale_fill_brewer(palette="Accent") +
#facet_grid(rows = vars(Strata), cols = vars(Sampling_Frame)) +
facet_wrap(vars(Strata), dir = "v", scales = "free_x") +
scale_x_reordered() +
xlab("Plot Number") + ylab("Change in Total % Cover") +
theme(legend.position = "none")
# Calculate range for count_ha_chg so that it can be plotted correctly in Jitter plot.
Tot_Cov_Chg_range <- Tot_Cov_Chg %>%
group_by(Sampling_Frame, Strata) %>%
summarize(y_range = max(abs(tot_pct_cov_chg))) %>%
ungroup()
# Add range column to Chg dataset
Tot_Cov_Chg <- Tot_Cov_Chg %>%
inner_join(Tot_Cov_Chg_range)
#........PAIRED PLOT ----
Tot_Cov %>%
#mutate(Status = fct_rev(Status)) %>%
ggplot(aes(x=S_Cycle, y=tot_pct_cov, group=Plot_Number)) +
geom_line(size=1, alpha=0.5, position=position_dodge(width=0.2)) +
geom_point(position=position_dodge(width=0.2)) +
xlab('Sample Cycle') +
ylab('Total % Cover') +
#scale_fill_brewer(palette="Accent") +
#scale_color_brewer(palette="Accent") +
#theme_bw() +
facet_grid(rows = vars(Strata))
#........JITTER PLOT ----
# Total Cover Change jitter plot
Tot_Cov_Chg %>%
ggplot(aes(x =Sampling_Frame, y = tot_pct_cov_chg)) +
geom_jitter(width = 0.05) +
geom_hline(yintercept=0, linetype = "dashed", color = "gray", size = 1) +
stat_summary(fun = mean, geom = "point", shape = 95, size = 8, color = "red") +
labs(x = "", y = "Change in % Cover") +
facet_wrap(vars(Strata), dir = "v")
# Summary Stats ----
# Use custom function at top of script to add stats to dataset
Tot_Cov_Stats <- add.stats(
.data = Tot_Cov_Chg,
.summary_var = tot_pct_cov_chg,
Unit_Code, Sampling_Frame, Strata)
# ....... BAR TOTAL MEANS ----
Tot_Cov_Stats %>%
#filter(S_Cycle != "CHG") %>%
ggplot(aes(x = S_Cycle, y = MEAN, fill = S_Cycle)) +
geom_bar(stat="identity", position = position_dodge()) +
geom_errorbar(aes(ymin=L, ymax=R), width=.2,
position=position_dodge(.9)) +
labs(y = "Total % Cover", x = "Sample Cycle") +
scale_fill_brewer(palette="Accent") +
facet_grid(rows = vars(Strata), cols = vars(Sampling_Frame))
#........JITTER PLOT ----
# Total Cover Change jitter plot
p2 <- Tot_Cov_Chg %>%
ggplot(aes(x =Sampling_Frame, y = tot_pct_cov_chg, label = Plot_Number)) +
geom_blank(aes(y = y_range)) +
geom_blank(aes(y = -y_range)) +
geom_hline(yintercept=0, linetype = "dashed", color = "gray", size = 1) +
geom_jitter(width = 0.05) +
stat_summary(fun = median, geom = "point", shape = 95, size = 8, color = "red") +
labs(y = "Change (% Cover)") +
facet_wrap(vars(Strata), nrow = 1, scales = "free") +
# geom_text_repel(force=1, point.padding=unit(1,'lines'),
# hjust=1, size = 3,
# direction='x',
# nudge_y=0.1,
# segment.size=0.7) +
theme(axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank())
p2
#........STRIP CHRT PAIR -----
p1 <- Tot_Cov %>%
#select(-count_pp) %>%
ungroup() %>%
#mutate(S_Year = as.factor(S_Year)) %>%
complete(nesting(S_Cycle), # Complete a data frame with missing combinations of factors
# nesting = find only the combinations that occur in the selected factors
Sampling_Frame, nesting(Strata), Plot_Number,
fill = list(tot_pct_cov = 0)) %>%
ggplot(aes(x=S_Cycle, y=tot_pct_cov, group=Plot_Number)) +
geom_line(size=1, alpha=0.5, position=position_dodge(width=0.2)) +
geom_point(position=position_dodge(width=0.2)) +
xlab('') +
ylab('% Cover') +
facet_wrap(vars(Strata), scales = "free", nrow = 1)
p1
#........STRIP/JITTER MULTI -----
grid.arrange(p1, p2, nrow = 2, top = "Total Cover"
#heights = c(2, 1.5)
)
# ....... Bar: SF compare % cover ----
#' *notes*
# Compare total percent cover of plots to other sampling frames.
#' *Note: subset options may need adjusted to add other sampling frames to graph*
# Tot_Cov_Stats %>%
# filter(S_Cycle == '2') %>%
# mutate(highlight = case_when(Unit_Code == "AMME" ~ 'AMME',
# Unit_Code != "AMME" ~ 'Other')) %>%
# ggplot(aes(x = reorder_within(Sampling_Frame, -MEAN, Strata),
# y = MEAN,
# fill = highlight)) +
# geom_bar(stat="identity") + #, position = position_dodge()
# geom_errorbar(aes(ymin=L, ymax=R), width=.2) + #,position=position_dodge(.9)
# labs(x = "Sampling Frame", y = "% Cover") +
# scale_x_reordered() +
# theme(axis.text.x=element_text(angle = 90, hjust = 1, vjust = 0.33)) +
# guides(fill=FALSE) +
# scale_fill_manual(values = c("#FF3300", "#736F6E")) +
# facet_wrap( ~Strata, scales = 'free_x', dir = "v")
#.-----------------------------------------------------
# 2) Nativity Total % cover ----
#.......................................................
# Can Total Greater Than 100%
# Calculate Total Percent Cover for Native vs. Non-native
Nat_Cov <- Cover %>%
group_by(S_Cycle, Unit_Code, Sampling_Frame, Plot_Number,
Transect, Point, Strata, Nativity) %>%
# Count hits at each cover point:
summarise(Hits_All_Nat = n(), .groups = 'drop') %>%
# But don't count record if entire plot had no hits: (e.g. transect is NA )
mutate(Hits_All_Nat = replace(Hits_All_Nat, is.na(Transect), 0)) %>%
# group by plot (i.e. remove Transect and Point from grouping variable)
group_by(S_Cycle, Unit_Code, Sampling_Frame, Plot_Number, Strata, Nativity) %>%
#Total hits at each point for each strata for entire plot
# (can be > 300 points or >100%)
summarise(tot_pct_cov = (sum(Hits_All_Nat)) / 300 * 100, .groups = 'drop')
# ........ BAR COV/PLOT----
Nat_Cov %>%
ggplot(aes(x = reorder_within(Plot_Number, -tot_pct_cov, within = Nativity),
y = tot_pct_cov, fill = S_Cycle)) +
geom_bar(stat="identity", position = position_dodge()) +
scale_fill_brewer(palette="Accent") +
facet_grid(rows = vars(Strata), cols = vars(Nativity), scales = "free") +
#facet_wrap(vars(Strata), dir = "v", scales = "free_x") +
scale_x_reordered() +
xlab("Plot Number")
# ...Change ----
# Calculate Change in Total Percent Cover for Native & Non-Native Frequency
Nat_Cov_Chg <- Nat_Cov %>%
ungroup() %>%
#group_by("Unit_Code", "Sampling_Frame","Plot_Number","Nativity") %>%
complete(S_Cycle, Unit_Code, Sampling_Frame, Plot_Number, Strata, Nativity,
fill = list(tot_pct_cov = 0)) %>%
pivot_wider(names_from = S_Cycle, values_from = tot_pct_cov) %>%
mutate(tot_pct_cov_chg = round(`2` - `1`, 2))
# ........ BAR CHG/PLOT----
Nat_Cov_Chg %>%
mutate(Plot_Number = reorder_within(Plot_Number, -tot_pct_cov_chg,
list(Strata, Nativity))) %>%
ggplot(aes(x = Plot_Number, y = tot_pct_cov_chg, fill = Nativity)) +
geom_col(position = position_dodge()) +
facet_wrap(~ Strata + Nativity, scales = "free_x") +
scale_fill_brewer(palette="Dark2") +
scale_x_reordered() +
xlab("Plot Number") + ylab("Change in % Cover")
#........QUAD NAT COVER----
plt <- max(c(abs(max(Nat_Cov_Chg$tot_pct_cov_chg)),
abs(min(Nat_Cov_Chg$tot_pct_cov_chg))))
Nat_Cov_Chg %>%
filter(Strata == "UNDERSTORY1") %>%
select(-`1`, -`2`) %>%
pivot_wider(names_from = Nativity, values_from = tot_pct_cov_chg) %>%
ggplot(aes(x = Native, y = `Non-Native`, label = Plot_Number)) +
annotate("rect", xmin = 0, xmax = Inf, ymin = 0, ymax = -Inf, fill= "#1B9E77", alpha = .25) +
annotate("rect", xmin = 0, xmax = -Inf, ymin = Inf, ymax = 0, fill= "#D95F02", alpha = .25) +
geom_point(color = "black", shape = 19, size = 5) +
geom_text(vjust = 0.4, color = "white", size = 3,
fontface = "bold", show.legend = FALSE) +
#geom_text_repel() +
geom_vline(xintercept = 0) +
geom_hline(yintercept = 0) +
xlim(min(-plt),max(plt)) +
ylim(max(plt), min(-plt)) +
facet_wrap(vars(Strata), dir = "v") +
ylab("Change in Non-Native Cover") +
xlab("Change in Native Cover")
# ...Summary Stats ----
# Use custom function at top of script to add stats to dataset
Nat_Cov_Stats <- add.stats(
.data = Nat_Cov_Chg,
.summary_var = tot_pct_cov_chg,
Unit_Code, Sampling_Frame, Strata, Nativity)
#........BAR YEARLY MEANS----
Nat_Cov_Stats %>%
filter(S_Cycle != "CHG") %>%
ggplot(aes(x = S_Cycle, y = MEAN, fill = Nativity)) +
geom_col(position = position_dodge()) +
geom_errorbar(aes(ymin=L, ymax=R), width=.2,
position=position_dodge(.9)) +
labs(y = "% Cover") +
facet_wrap(vars(Strata, Nativity), scales = "free_x") +
scale_fill_brewer(palette="Dark2") +
xlab("Sample Cycle") +
theme(legend.title = element_blank())
#........BAR CHG----
Nat_Cov_Stats %>%
filter(S_Cycle == "CHG") %>%
#filter(Strata == "UNDERSTORY1") %>%
ggplot(aes(x = S_Cycle, y = MEAN, fill = Nativity)) +
geom_col(position = position_dodge()) +
geom_errorbar(aes(ymin=L, ymax=R), width=.2,
position=position_dodge(.9)) +
labs(y = "Change in Total % Cover") +
facet_wrap(vars(fct_rev(Strata))) +
scale_fill_brewer(palette="Dark2") +
theme(axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
legend.title = element_blank())
#ggsave(here("figs", "bar_mean_cov_chg_nativity.png"))
#........JITTER PLOT ----
# Jitter plot change (stand alone)
Nat_Cov_Chg %>%
ggplot(aes(x =Sampling_Frame, y = tot_pct_cov_chg)) +
geom_jitter(width = 0.05) +
geom_hline(yintercept=0, linetype = "dashed", color = "gray", size = 1) +
stat_summary(fun = mean, geom = "point", shape = 95, size = 8, color = "red") +
facet_grid(vars(Strata), vars(Nativity))
# Calculate range for nat__cov_chg so that zeros will line up.
Nat_Cov_Chg_range <- Nat_Cov_Chg %>%
group_by(Sampling_Frame, Nativity, Strata) %>%
summarize(y_range = max(abs(tot_pct_cov_chg))) %>%
ungroup()
# Add range column to Chg dataset
Nat_Cov_Chg <- Nat_Cov_Chg %>%
inner_join(Nat_Cov_Chg_range)
# Jitter plot change (for multichart)
Nat_Cov_Chg_jitter <- Nat_Cov_Chg %>%
ggplot(aes(x =Sampling_Frame, y = tot_pct_cov_chg, label = Plot_Number)) +
geom_blank(aes(y = y_range)) +
geom_blank(aes(y = -y_range)) +
geom_hline(yintercept=0.0, linetype = "dashed", color = "gray", size = 1) +
geom_jitter(width = 0.05) +
stat_summary(fun = median, geom = "point", shape = 95, size = 8, color = "red") +
labs(y = "Change (% Cover)") +
facet_wrap(vars(Strata, Nativity), nrow = 1, scales = "free") +
# geom_text_repel(force=1, point.padding=unit(1,'lines'),
# hjust=1, size = 3,
# direction='x',
# nudge_y=0.1,
# segment.size=0.7) +
theme(axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank())
Nat_Cov_Chg_jitter
#........STRIP CHRT PAIR -----
Nat_Cov_strip <- Nat_Cov %>%
#mutate(Status = fct_rev(Status)) %>%
ggplot(aes(x=S_Cycle, y=tot_pct_cov, group=Plot_Number)) +
geom_line(size=1, alpha=0.5, position=position_dodge(width=0.2)) +
geom_point(position=position_dodge(width=0.2)) +
xlab('Sample Cycle') +
ylab('Total % Cover') +
#scale_fill_brewer(palette="Accent") +
#scale_color_brewer(palette="Accent") +
#theme_bw() +
facet_wrap(vars(Strata, Nativity), nrow = 1, scales = "free")
#facet_grid(cols = vars(Nativity), rows = vars(Strata))
Nat_Cov_strip
#........STRIP/JITTER MULTI -----
grid.arrange(Nat_Cov_strip, Nat_Cov_Chg_jitter, nrow = 2, top = "Nativity Cover"
#heights = c(2, 1.5)
)
#.-----------------------------------------------------
# 3) Nativity Richness ----
#.......................................................
Nat_Rich <- Cover %>%
group_by(S_Cycle, Unit_Code, Sampling_Frame, Plot_Number, Strata, Nativity) %>%
summarise(richness = n_distinct(Code))
#........STRIP CHRT PAIR -----
Nat_Rich %>%
#mutate(Status = fct_rev(Status)) %>%
ggplot(aes(x=S_Cycle, y=richness, group=Plot_Number)) +
geom_line(size=1, alpha=0.5, position=position_dodge(width=0.2)) +
geom_point(position=position_dodge(width=0.2)) +
xlab('Sample Cycle') +
ylab('Understory Richness') +
#scale_fill_brewer(palette="Accent") +
#scale_color_brewer(palette="Accent") +
#theme_bw() +
facet_grid(cols = vars(Nativity), rows = vars(Strata))
# ...Change ----
# Calculate Change in Total Percent Cover for Native & Non-Native Frequency
Nat_Rich_Chg <- Nat_Rich %>%
ungroup() %>%
#group_by("Unit_Code", "Sampling_Frame","Plot_Number","Nativity") %>%
complete(S_Cycle, Unit_Code, Sampling_Frame, Plot_Number, Strata, Nativity,
fill = list(richness = 0)) %>%
pivot_wider(names_from = S_Cycle, values_from = richness) %>%
mutate(richness_chg = round(`2` - `1`, 2))
# ........STRIP CHRT CHG----
Nat_Rich_Chg %>%
ggplot(aes(x = Sampling_Frame, y=richness_chg)) +
geom_jitter(width = 0.05) +
ylab('Change in richness') +
theme(axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank()) +
stat_summary(fun=mean, geom="point", shape=95,
size=8, color="red") +
geom_hline(yintercept=0, linetype="dashed", color = "gray", size = 1) +
facet_grid(cols = vars(Nativity), rows = vars(Strata))
#........QUAD NAT RICH----
plt.r <- max(c(abs(max(Nat_Rich_Chg$richness_chg)),
abs(min(Nat_Rich_Chg$richness_chg))))
Nat_Rich_Chg %>%
select(-`1`, -`2`) %>%
pivot_wider(names_from = Nativity, values_from = richness_chg) %>%
ggplot(aes(x = Native, y = `Non-Native`)) +
annotate("rect", xmin = 0, xmax = Inf, ymin = 0, ymax = -Inf, fill= "green", alpha = .25) +
annotate("rect", xmin = 0, xmax = -Inf, ymin = Inf, ymax = 0, fill= "red", alpha = .25) +
geom_point() +
geom_vline(xintercept = 0) +
geom_hline(yintercept = 0) +
xlim(min(-plt.r),max(plt.r)) +
ylim(max(plt.r), min(-plt.r)) +
facet_wrap(vars(Strata), dir = "v") +
ylab("Change in Non-Native Richness") +
xlab("Change in Native Richness")
# ...Summary Stats ----
# Use custom function at top of script to add stats to dataset
Nat_Rich_Stats <- add.stats(
.data = Nat_Rich_Chg,
.summary_var = richness_chg,
Unit_Code, Sampling_Frame, Strata, Nativity)
#........BAR YEARLY MEANS----
Nat_Rich_Stats %>%
ggplot(aes(x = S_Cycle, y = MEAN, fill = Nativity)) +
geom_col(position = position_dodge()) +
geom_errorbar(aes(ymin=L, ymax=R), width=.2,
position=position_dodge(.9)) +
labs(y = "richness") +
facet_wrap(vars(Strata, Nativity), scales = "free_x") +
scale_fill_brewer(palette="Dark2")
#........BAR CHG----
nrs <- Nat_Rich_Stats %>%
filter(S_Cycle == "CHG") %>%
filter(Nativity != "Unknown") %>%
ggplot(aes(x = S_Cycle, y = MEAN, fill = Nativity)) +
geom_col(position = position_dodge()) +
geom_errorbar(aes(ymin=L, ymax=R), width=.2,
position=position_dodge(.9)) +
labs(y = "Change in richness") +
facet_wrap(vars(fct_rev(Strata))) +
scale_fill_brewer(palette="Dark2") +
theme(axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
legend.position = "none")
nrs
#ggsave(here("figs", "bar_mean_nat_rich_chg.png"))
#grid.arrange(ncs, nrs, nrow = 1)
#.-----------------------------------------------------
# 4) Lifeform Total % cover ----
#.......................................................
#'* Optional Filter *
Forms_Cov_Filter <- Cover %>%
# Take 'Nativity' and 'Life_form' to make plural category (ex. 'Native Shrubs')
mutate(NLF = paste0(Nativity, " ", Life_form, "s")) #%>%
# filter(Plot_Number %in% c(2,4,6,9)) %>%
# filter(Nativity != "Unknown")
#drop_na(Name)
# Calculate Total Percent Cover for Native vs. Non-native Lifeforms
Forms_Cov <- Forms_Cov_Filter %>%
group_by(S_Cycle, Unit_Code, Sampling_Frame, Plot_Number,
Transect, Point, Strata, Nativity, Life_form, NLF) %>%
# Count hits at each cover point:
summarise(Hits_All_Forms = n(), .groups = 'drop') %>%
# But don't count record if entire plot had no hits: (e.g. transect is NA )
mutate(Hits_All_Forms = replace(Hits_All_Forms, is.na(Transect), 0)) %>%
# group by plot (i.e. remove Transect and Point from grouping variable)
group_by(S_Cycle, Unit_Code, Sampling_Frame, Plot_Number, Strata, Nativity,
Life_form, NLF) %>%
#Total hits at each point for each strata for entire plot
# (can be > 300 points or >100%)
summarise(tot_pct_cov = (sum(Hits_All_Forms)) / 300 * 100, .groups = 'drop')
# Calculate lifeform Cover (same as lines above) - but combine Strata 1 & 2
Forms_Cov_1a2 <- Forms_Cov_Filter %>%
group_by(S_Cycle, Unit_Code, Sampling_Frame, Plot_Number,
Transect, Point, Nativity, Life_form, NLF) %>%
summarise(Hits_All_Forms = n_distinct(n()), .groups = "drop") %>% #remove dbl counts
# group by plot (i.e. remove Transect and Point from grouping variable)
group_by(S_Cycle, Unit_Code, Sampling_Frame, Plot_Number,
Nativity, Life_form, NLF) %>%
#Total hits at each point for each strata for entire plot
# (cannot be greater than 100%)
summarise(tot_pct_cov = (sum(Hits_All_Forms)) / 300 * 100, .groups = 'drop')
# ........ BAR COV/PLOT----
Forms_Cov %>%
ggplot(aes(x = reorder_within(Plot_Number, -tot_pct_cov, within = NLF),
y = tot_pct_cov, fill = S_Cycle)) +
geom_bar(stat="identity", position = position_dodge()) +
scale_fill_brewer(palette="Accent") +
facet_grid(rows = vars(Strata), cols = vars(NLF), scales = "free") +
#facet_wrap(vars(Strata), dir = "v", scales = "free_x") +
scale_x_reordered() +
xlab("Plot Number")
# ...Change ----
# Calculate Change in Total Percent Cover for Native & Non-Native Frequency
Forms_Cov_Complete <- Forms_Cov %>%
ungroup() %>%
#group_by("Unit_Code", "Sampling_Frame","Plot_Number","Nativity") %>%
complete(S_Cycle, nesting(Unit_Code, Sampling_Frame, Plot_Number, Strata,
Nativity, Life_form, NLF),
fill = list(tot_pct_cov = 0))
Forms_Cov_Chg <- Forms_Cov_Complete %>%
pivot_wider(names_from = S_Cycle, values_from = tot_pct_cov) %>%
mutate(tot_pct_cov_chg = round(`2` - `1`, 2))
# Calculate Change (Strata 1&2 Combined)
Forms_Cov_Complete_1a2 <- Forms_Cov_1a2 %>%
ungroup() %>%
#group_by("Unit_Code", "Sampling_Frame","Plot_Number","Nativity") %>%
complete(S_Cycle, nesting(Unit_Code, Sampling_Frame, Plot_Number,
Nativity, Life_form, NLF),
fill = list(tot_pct_cov = 0))
Forms_Cov_Chg_1a2 <- Forms_Cov_Complete_1a2 %>%
pivot_wider(names_from = S_Cycle, values_from = tot_pct_cov) %>%
mutate(tot_pct_cov_chg = round(`2` - `1`, 2))
# ........ BAR CHG/PLOT----
Forms_Cov_Chg %>%
mutate(Plot_Number = reorder_within(Plot_Number, -tot_pct_cov_chg,