-
Notifications
You must be signed in to change notification settings - Fork 0
/
Council_EC Flux_ Analysis_Github.Rmd
2099 lines (1580 loc) · 77.9 KB
/
Council_EC Flux_ Analysis_Github.Rmd
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: "Council EC Flux Data Analysis"
output: html_document
date: "2024-12-02"
---
## based on Dani's Churchill CO2 Analysis code on github
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
# add root directory here for github
```
Notes:
```{r, include=FALSE}
rm(list = ls())
library(data.table)
library(ggplot2)
library(cowplot)
library(openair)
library(plotrix)
library(signal)
library(svMisc)
library(zoo)
library(stringr)
library(plyr)
library(viridis)
library(lubridate)
library(tidyverse)
library(gridExtra)
library(plotly)
library(RColorBrewer)
library(pracma)
library(dplyr)
Sys.setenv(TZ='UTC')
```
# Set working directory and then Load data
```{r}
#original half-hourly dataframe
df = fread('C:/Users/kkent/Documents/Council Data/Council BASE gapfilling/council_gapfilled_clean_2017_2023_for analysis.csv',na.strings = c('-9999','NA','NaN','NAN','-7999'))
#needs continuous day variable
df_avg = fread('C:/Users/kkent/Documents/Council Data/Council BASE gapfilling/council_AVG_gapfilled_clean_2017_2023_for analysis.csv',na.strings = c('-9999','NA','NaN','NAN','-7999'))
#needs continuous day variable
df_monthly_avg = fread('C:/Users/kkent/Documents/Council Data/Council BASE gapfilling/council_monthly_AVG_gapfilled_clean_2017_2023_for analysis.csv',na.strings = c('-9999','NA','NaN','NAN','-7999'))
```
```{r}
#quick look at the different temp profiles (all at 15 cm depth, diff locations)
sum(is.na(df$TS_1_1_1)) #60277 NA's in temp by margin pond
sum(is.na(df$TS_2_1_1)) #48729 NA's in temp by lichen/berries
sum(is.na(df$TS_3_1_1)) #50445 NA's in temp by tussock
sum(is.na(df$TS_4_1_1)) #52796 NA's in temp by foot of EC tower
sum(is.na(df$TS_5_1_1)) #new temp in latest upload with 2023 data, don't have info on what it is yet**
```
```{r}
#checking timestamp
ggplot(data = df,aes(TIMESTAMP_END,FC_F))+
geom_point()+
scale_x_datetime(limits = as.POSIXct(c('2021-09-28','2021-10-05')))+
scale_y_continuous(limits = c(-12,5))
```
#create a useable timestamp - not necessary here as it's already in the correct format from our BASE processing code
```{r}
#ignore this if df is already in correct format, as this might mess up the formatting here and cause issues with analysis
# df$TIMESTAMP_END = as.character(df$TIMESTAMP_END)
# df$TIMESTAMP_START = as.character(df$TIMESTAMP_START)
#
# df$TIMESTAMP_END = as.POSIXct(df$TIMESTAMP_END, tz="UTC", format = "%Y%m%d%H%M")
# df$TIMESTAMP_START = as.POSIXct(df$TIMESTAMP_START, tz="UTC", format = "%Y%m%d%H%M")
```
#Create yearly dataframes
#create TIMESTAMP variable that is = to TIMESTAMP_END
```{r}
df$TIMESTAMP = df$TIMESTAMP_END
year_df <- function(df, year) {
df %>%
filter(format(date, "%Y") == as.character(year)) %>%
mutate(DOY = yday(date))
}
```
#Creates data frames of each year containing the daily averages of each value- useful for focusing on one year at a time
```{r}
#create dataframes for each year for the data you have so you can look at annual trends
#map out the timestamp of the daily avg dataset
df_avg$date <- as.POSIXct(df_avg$date, format = "%Y-%m-%d") #tells R the order of the timestamp: yr-month-day
df_avg_2017 <- year_df(df_avg, 2017)
df_avg_2018 <- year_df(df_avg, 2018)
df_avg_2019 <- year_df(df_avg, 2019)
df_avg_2020 <- year_df(df_avg, 2020)
df_avg_2021 <- year_df(df_avg, 2021)
df_avg_2022 <- year_df(df_avg, 2022)
df_avg_2023 <- year_df(df_avg, 2023)
#map out the timestamp of the half-hourly dataset
df$date <- as.POSIXct(df$date, format = "%Y-%m-%d %H:%M:%OS") #yr month day hour min sec
df_2017 <- year_df(df, 2017)
df_2018 <- year_df(df, 2018)
df_2019 <- year_df(df, 2019)
df_2020 <- year_df(df, 2020)
df_2021 <- year_df(df, 2021)
df_2022 <- year_df(df, 2022)
df_2023 <- year_df(df, 2023)
#map out the timestamp of the monthly dataset
df_monthly_avg$date <- as.POSIXct(df_monthly_avg$date, format = "%Y-%m-%d") #yr month
df_monthly_2017 <- year_df(df_monthly_avg, 2017)
df_monthly_2018 <- year_df(df_monthly_avg, 2018)
df_monthly_2019 <- year_df(df_monthly_avg, 2019)
df_monthly_2020 <- year_df(df_monthly_avg, 2020)
df_monthly_2021 <- year_df(df_monthly_avg, 2021)
df_monthly_2022 <- year_df(df_monthly_avg, 2022)
df_monthly_2023 <- year_df(df_monthly_avg, 2023)
```
#ERA5 air temp vs site air temp
####Since I'll be using ERA5 air temp to supplement the missing temp gaps in the site data, need to check agreement
```{r}
summary(lm(df$TA ~ df$TA_ERA5)) #R2 = 0.96, slope = 1
Air_temp_test= ggplot(data = df,aes(TA,TA_ERA5))+theme_bw()+
geom_hline(yintercept = 0,lty=2)+
geom_vline(xintercept = 0,lty=2)+
geom_point(alpha=0.2)+
scale_fill_viridis_c()+
geom_abline(slope = 1,intercept = 0,col='red',lty=1)+
scale_x_continuous('Site TA (C)')+
scale_y_continuous('ERA5 TA (C)')+
annotate(geom = 'text',x = 6, y = -8,label=expression(R^2~"= 0.96"),size = 3)+
annotate(geom = 'text',x = 6,y = -15,label=expression(Slope~"= 1.0"),size = 3)+
theme(text = element_text(size = 8)) +
labs(title = "TA vs ERA5 TA")
Air_temp_test #great agreement, just like Kyle said
```
# Timeseries plot - just to check things out
Convert the data from umol or nmol C/m2/s to g C/m2/day
Checking Random Timeseries of GPP, ER (RECO), NEE, and CH4
```{r}
#AMERIFLUX data is in micro mol/m2/s for CO2 and nano mol/m2/s for CH4 --> so here we have conversions to make the units in gC/m2/day for our timeseries figures
# 12 = molar mass of C
#nmol C to g C = 10^-9 * 12 & umol C to g C = 10^-6 * 12
#using the gap-filled data for timeseries so there is continuous data, and for these we use the daily averages calculated before and multiply them by seconds -> min -> hours within a day and convert to g C
annual_timeseries_2019_plot <- ggplot(data = df_avg_2019, aes(x=date))+
theme_bw()+
geom_point(aes(y = -GPP_F*60*60*24*(1/1000000)*12, color = "GPP"))+
geom_point(aes(y = RECO_F*60*60*24*(1/1000000)*12, color = "Respiration")) +
geom_line(aes(y = FC_F*60*60*24*(1/1000000)*12, color = "NEE"))+ # turbulent flux of CO2
geom_line(aes(y = FCH4_F*60*60*24*(1/1000000000)*12*50, color = "CH4"))+ #multiply by 50 to help scale it so it shows up in the same figure as C, GPP, and Resp
geom_hline(yintercept=0, col="black")+
scale_y_continuous(expression('CO'[2]*' Flux (g C'~m^-2~d^-1*')'),
sec.axis = sec_axis(~ . / 50, name = expression('CH'[4]*' Flux (g C'~m^-2~d^-1*')')))+ #~.*1 or ~./ 50 notation in the code is used to scale the secondary axis
#when multiplying CH4 to scale it, make sure to do the opposite for when you scale the axis **--> example, if you multiply CH4 by 50 to get it visible on the same figure, divide the CH4 scale by 50 to take this scaling into account
scale_color_manual(name = " ",
values = c("GPP" = "navy",
"Respiration" = "turquoise3",
"NEE" = "salmon",
"CH4" = "mediumorchid"),
breaks = c("GPP", "Respiration", "NEE", "CH4"))+
labs(title = "GPP, Respiration, NEE, and Methane Fluxes: 2019", x = "Date") +
# vertical lines for specific events - 2019 - for AGU poster
geom_vline(xintercept = as.POSIXct("2019-05-24"))+ #end of winter, DOY 144*
geom_vline(xintercept = as.POSIXct("2019-09-02"))+ #end of growing season,DOY 245* based on looking thru data --> old: DOY 258 #9/15, visual assessment
geom_vline(xintercept = as.POSIXct("2019-10-11")) + #end of Fall Senescence, DOY 284*
# # Labels for each vertical line within figure
annotate("text", size = 3, x = as.POSIXct("2019-03-05"), y = -6, label = "Winter", color = "black") +
annotate("text", size = 3, x = as.POSIXct("2019-07-11"), y = -6, label = "Growing Season", color = "black") +
annotate("text", size = 3, x = as.POSIXct("2019-10-01"), y = -6, label = "Senescence", color = "black") +
annotate("text", size = 3, x = as.POSIXct("2019-12-16"), y = -6, label = "Winter", color = "black")
annual_timeseries_2019_plot
```
#Save timeseries plot
```{r}
ggsave(
filename = "annual_timeseries_2019_plot .png", # File name and extension
plot = annual_timeseries_2019_plot, # The plot to save (default is the last plot created)
width = 20, # Width in inches
height = 15, # Height in inches
dpi = 600, # Resolution in dots per inch
units = "cm" # Units for width and height (can be "in", "cm", or "mm")
)
#ggsave("my_plot.pdf", width = 8, height = 6, dpi = 300, units = "in")
```
# Gap Filling vs Site Data Plot
Shows gap in flux data and compares gap filled and non-gap filled data
```{r}
#Uses Half-Hourly data - units of umol/m^2/s
#have to tell R what format the timestamp is in so it can parse out the dates
df_2019$TIMESTAMP <- as.POSIXct(df_2019$TIMESTAMP, format="%Y-%m-%d %H:%M:%S")
#plot half-hourly FCO2 data with the gap-filled data to compare
ggplot(data = df_2019)+
geom_hline(yintercept = 0)+
geom_point(aes(TIMESTAMP,FC_F,col='RF Gapfilled'))+
geom_point(aes(TIMESTAMP,FC,col='Original'))+
scale_y_continuous(limits=c(-10,10))+
scale_x_datetime(limits = as.POSIXct(c('2019-01-01','2019-12-01')))+
labs(#y = "CO2 Flux (umol/m2/s) ",
y = expression(CO[2]~Flux~(umolCO[2]~m^-2~y^-1)),
x = "Time") +
scale_color_manual(values=c('black','red'))+
theme_bw()+
#geom_vline(xintercept = as.POSIXct("2019-07-05"))+
labs(title = "Random Forest Gapfill vs Site Data: 2019")
#plot half-hourly FCH4 data with the gap-filled data to compare
ggplot(data = df_2019)+
geom_hline(yintercept = 0)+
geom_point(aes(TIMESTAMP,FCH4_F,col='RF Gapfilled'))+
geom_point(aes(TIMESTAMP,FCH4,col='Original'))+
#scale_y_continuous(limits=c(-10,10))+
scale_x_datetime(limits = as.POSIXct(c('2019-01-01','2019-12-01')))+
labs(#y = "FCH4 Flux (nmol/m2/s)"
y = expression(CH[4]~Flux~(nmolCH[4]~m^-2~y^-1)),
x = "Time") +
scale_color_manual(values=c('black','red'))+
theme_bw()+
#geom_vline(xintercept = as.POSIXct("2019-07-05"))+
labs(title = "Random Forest Gapfill vs Site Data: 2019")
```
# Annual Net C Budget - converting units and checking sums
_no_NAs is used for other years of data where NAs are present in gapfilled data
```{r}
#checking the budgets with the sum function to double check the results of the bar plots in code chunk below
# Net CO2 Flux - multiply by 30 to represent per half hour --> can use this with "sum" function to double check the results of the plots for the yearly budgets
df_2017 <- df_2017 %>%
mutate(FC_F_no_NAs = ifelse(is.na(FC_F), 0, FC_F * 60 * 30* (1/1000000) * 12))
#sum of the flux
sum(df_2017$FC_F_no_NAs)
#Net CH4 flux --> to check results of bar plots with the sum function
df_2017 <- df_2017 %>%
mutate(FCH4_F_no_NAs = ifelse(is.na(FCH4_F), 0, FCH4_F*60*30*(1/1000000000)*12))
#sum - yearly budget
sum(df_2017$FCH4_F_no_NAs)
```
#### Net C Budgets: Annual bar plot
```{r}
#Creates numeric timestamp with the unit of minutes to integrate over half-hourly data
#this references the half hourly increments and takes the diff (so 30 min increments)
df_2019 <- df_2019 %>%
mutate(time_minutes = as.numeric(difftime(TIMESTAMP, min(TIMESTAMP), units = "mins")))
df_2019$TIMESTAMP
# Units start as micro-moles of CO2/(m^2/s), converted to Grams of C/m^2/minute, integrated over minutes to get Grams of C/m^2
#creating new column of net C budget in order to make the annual budget bar plots
#net CO2
df_2019 <- df_2019 %>%
mutate(FC_F_no_NAs = ifelse(is.na(FC_F), 0, FC_F * 60 * (1/1000000) * 12))
#Net CO2 flux in g/m2
net_CO2 <- trapz(df_2019$time_minutes, df_2019$FC_F_no_NAs)
# Units start as nano-moles of CH4/(m^2/s), converted to Grams of C/m^2/minute, integrated over minutes to get Grams of C/m^2
#Net CH4 Flux
df_2019 <- df_2019 %>%
mutate(FCH4_F_no_NAs = ifelse(is.na(FCH4_F), 0, FCH4_F*60*(1/1000000000)*12))
net_CH4 <- trapz(df_2019$time_minutes, df_2019$FCH4_F_no_NAs)
# Used IPCC Sixth Assessment Report (AR6) global warming potentials, 100 year time period - could use paper gwp* or delta equation for future analysis
net_CH4_CO2e <- net_CH4*27.2
sum = net_CO2+net_CH4
#Dataframe created to generate bar graph
net_wp_2019 <- data.frame(
Category = c("CO2", "CH4", "Total"),
Value = c(round(net_CO2, 2), round(net_CH4, 2), round(sum, 2)
))
#Plot here
annual_Cbudget_2019 <- ggplot(net_wp_2019, aes(x = Category, y = Value)) +
theme_bw()+
geom_bar(stat = "identity", position = "dodge", fill = "turquoise3") +
geom_hline(yintercept=0, colour = "black")+
labs(
x = "",
#y = expression(FCH4~(gCH[4]-C/m^2/s)),
y = expression(Net~Carbon~Flux~(gC~m^-2~y^-1))) +
geom_label(aes(label = Value), vjust = ifelse(net_wp_2019$Value >= 0, -0.7, 1.5), colour = "black", fill ="white")+
scale_y_continuous(
#breaks = seq(-5, 5, 1.5), # changing for 2019
# limits = c(-2, 5))+ #changing for year 2019
breaks = seq(-80, 20, 10), # Set limits for the primary axis - other years go up to -73
limits = c(-80, 20))+
labs(title = "Cumulative Annual Carbon Budget for 2019") +#for 2019
#labs(title = "2019")
theme(
axis.title.x = element_text(size = 16, face = "bold"), # Make x-axis title larger and bold
axis.title.y = element_text(size = 16, face = "bold"), # Make y-axis title larger and bold
axis.text.x = element_text(size = 14, face = "bold"), # Make x-axis text larger and bold
axis.text.y = element_text(size = 14, face = "bold") # Make y-axis text larger and bold
)# +
#coord_fixed(ratio = 0.6) #for making small 2019 fig
annual_Cbudget_2019
```
#save image to maintain dimensions
```{r}
ggsave(
filename = "Annual_Cbudget_2019.png", # File name and extension
plot = annual_Cbudget_2019 , # The plot to save (default is the last plot created)
width = 5, # Width in inches
height = 3, # Height in inches
dpi = 600, # Resolution in dots per inch
units = "cm" # Units for width and height (can be "in", "cm", or "mm")
)
#ggsave("my_plot.pdf", width = 8, height = 6, dpi = 300, units = "in")
```
#Daily avg sum for each month per year - Net C budget
```{r}
#If using the daily avg data from the timeAverage daily dataframe
#convert from umol CO2/m2/s to gCO2/m2/day
df_avg_2019 <- df_avg_2019 %>%
mutate(FC_F_no_NAs = ifelse(is.na(FC_F), 0, FC_F * 60 * 60 * 24 * (1/1000000) * 12))
#convert from nano-mol CH4/m2/s to gCH4/m2/day
df_avg_2019 <- df_avg_2019 %>%
mutate(FCH4_F_no_NAs = ifelse(is.na(FCH4_F), 0, FCH4_F * 60 * 60 * 24 * (1/1000000000) * 12))
# Ensure the 'date' column is in Date format if it isn't already
#df_avg_2018$date <- as.Date(df_avg_2018$date)
# Extract the year and month to create a grouping column
df_avg_2019$year_month <- format(df_avg_2019$date, "%Y-%m")
# Calculate the sum of carbon emissions (g C/m²/month) for each month - this is not working now...
monthly_CO2sum_2019 <- aggregate(FC_F_no_NAs ~ year_month, data = df_avg_2019, sum, na.rm = TRUE)
monthly_CH4sum_2019 <- aggregate(FCH4_F_no_NAs ~ year_month, data = df_avg_2019, sum, na.rm = TRUE)
# View the resulting dataframe with monthly sums
# monthly_CO2sum_2019
# monthly_CH4sum_2019
#To find monthly sum of CO2 and CH4 together
# Calculate the sum of carbon emissions (g C/m²/month) and methane emissions (g CH4/m²/month) for each month
monthly_Cflux_sum_2019 <- aggregate(cbind(FC_F_no_NAs, FCH4_F_no_NAs) ~ year_month, data = df_avg_2019, sum, na.rm = TRUE)
# View the resulting dataframe with monthly sums
monthly_Cflux_sum_2019
#save table of monthly C flux budgets per year
#write.csv(monthly_Cflux_sum_2019, file = "monthly_Cflux_sum_2019.csv", row.names = FALSE)
#double check the monthly sums
#extract month
df_avg_2019$month <- as.numeric(format(df_avg_2019$date, "%m"))
#filter out by month - October = 10
sum_october_2019 <- df_avg_2019[month == 10, .(monthly_sum = sum(FC_F_no_NAs, na.rm = TRUE))]
# Print the result
sum_october_2019
#sum - check sum to match yearly budget
sum(df_avg_2019$FC_F_no_NAs)
sum(df_2019$FC_F_no_NAs)
```
```{r}
#If using the HH data -- but these are redundant & should match the monthly averages from the daily data in code chunk above
#convert from umol CO2/m2/s to gCO2/m2/HH if not done so already - skip if already done
df_2019 <- df_2019 %>%
mutate(FC_F_no_NAs = ifelse(is.na(FC_F), 0, FC_F * 60 * 30 * (1/1000000) * 12))
#convert from nano-mol CH4/m2/s to gCH4/m2/HH if not done so already - skip if already done
df_2019 <- df_2019 %>%
mutate(FCH4_F_no_NAs = ifelse(is.na(FCH4_F), 0, FCH4_F * 60 * 30 * (1/1000000000) * 12))
# Ensure the 'date' column is in Date format if it isn't already
#df_avg_2019$date <- as.Date(df_avg_2019$date)
# Extract the year and month to create a grouping column
df_2019$year_month <- format(df_2019$date, "%Y-%m")
# Calculate the sum of carbon emissions (g C/m²/month) for each month
monthly_HHCO2sum_2019 <- aggregate(FC_F_no_NAs ~ year_month, data = df_avg_2019, sum, na.rm = TRUE)
monthly_HHCH4sum_2019 <- aggregate(FCH4_F_no_NAs ~ year_month, data = df_avg_2019, sum, na.rm = TRUE)
# View the resulting dataframe with monthly sums
monthly_HHCO2sum_2019
monthly_HHCH4sum_2019
#To find monthly sum of CO2 and CH4 together
# Calculate the sum of carbon emissions (g C/m²/month) and methane emissions (g CH4/m²/month) for each month
monthly_HHCflux_sum_2019 <- aggregate(cbind(FC_F_no_NAs, FCH4_F_no_NAs) ~ year_month, data = df_avg_2019, sum, na.rm = TRUE)
# View the resulting dataframe with monthly sums
monthly_HHCflux_sum_2019
#save table of monthly C flux budgets per year
#write.csv(monthly_HHCflux_sum_2019, file = "monthly_HH_Cflux_sum_2019.csv", row.names = FALSE)
```
#### Net C Budgets per month bar plot
```{r}
# Used IPCC Sixth Assessment Report (AR6) global warming potentials of CH4 compared to CO2, 100 year time period
# Extract the month name from the year_month column
monthly_Cflux_sum_2019$month_name <- format(as.Date(paste0(monthly_Cflux_sum_2019$year_month, "-01")), "%B")
#CH4 warming potential - optional
monthly_Cflux_sum_2019 <- monthly_Cflux_sum_2019 %>%
mutate(CH4_CO2e = FCH4_F_no_NAs*27.2)
#sum of CO2 and CH4 fluxes
monthly_Cflux_sum_2019 <- monthly_Cflux_sum_2019 %>%
mutate(total_Cflux = FC_F_no_NAs + FCH4_F_no_NAs)
#reshape the df into a long format so you can plot CO2, CH4, and the sum per month on the same figure
# Load necessary libraries
library(ggplot2)
library(tidyr)
library(dplyr)
# Reshape the data to a long format
monthly_Cflux_sum_2019_long <- monthly_Cflux_sum_2019 %>%
pivot_longer(cols = c(FC_F_no_NAs, FCH4_F_no_NAs, total_Cflux),
names_to = "Flux_Type",
values_to = "Flux_Sum")
# Create the bar graph
ggplot(monthly_Cflux_sum_2019_long, aes(x = factor(month_name, levels = month.name), y = Flux_Sum, fill = Flux_Type)) +
geom_bar(stat = "identity", position = "dodge") +
scale_fill_manual(values = c("FC_F_no_NAs" = "turquoise", "FCH4_F_no_NAs" = "salmon", "total_Cflux" = "grey")) +
labs(x = "Month", y = "C Flux (g/m²/month)",
title = "Monthly CO2 and CH4 Flux Sums for 2019",
fill = "C flux") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
#Methane is very small compared to CO2, so make a scaling factor and new fig with a secondary axis
# Scale the methane values (FCH4_F_no_NAs) by a factor, e.g., 100
scaling_factor <- 30
monthly_Cflux_sum_2019$FCH4_scaled <- monthly_Cflux_sum_2019$FCH4_F_no_NAs * scaling_factor
# Reshape the data to a long format
monthly_Cflux_sum_2019_long <- monthly_Cflux_sum_2019 %>%
select(-FCH4_F_no_NAs) %>% # Exclude the unwanted column
pivot_longer(cols = c(FC_F_no_NAs, total_Cflux, FCH4_scaled),
names_to = "Flux_Type",
values_to = "Flux_Sum")
# Create the bar graph with a scaled FCH4_F_N_no_NAs
monthlyC_2019_barplot <- ggplot(monthly_Cflux_sum_2019_long, aes(x = factor(month_name, levels = month.name), y = Flux_Sum, fill = Flux_Type)) +
geom_bar(stat = "identity", position = "dodge") +
scale_fill_manual(
values = c("FC_F_no_NAs" = "turquoise", "FCH4_scaled" = "salmon", "total_Cflux" = "grey"),
labels = c("FC_F_no_NAs" = "CO2 Flux",
"FCH4_scaled" = "CH4 Flux",
"total_Cflux" = "Total C Flux"))+
# Primary y-axis for CO2 and scaled methane on the secondary axis
scale_y_continuous(
#name = "CO2 Flux (g C/m²/month)",
name = expression(CO[2]~Flux~(gC~m^-2~month^-1)),
breaks = seq(-55, 30, 5), # Set limits for the primary axis
limits = c(-55, 30), # Explicitly set limits for the primary axis
sec.axis = sec_axis(~ . / scaling_factor, name = expression(Methane~Flux~(gC~m^-2~month^-1)),breaks = seq(-1, 1, 0.25))
) +
#y = expression(CH[4]~Flux~(nmolCH[4]~m^-2~y^-1)),
labs(x = "Month", y = "C Flux (g/m²/month)",
title = "Monthly C Fluxes for 2019",
#title = "Monthly CO2 and CH4 Flux Sums for 2019",
fill = "C flux") +
theme_bw() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
monthlyC_2019_barplot
```
#save image to maintain dimensions
```{r}
ggsave(
filename = "monthlyC_2019_barplot .png", # File name and extension
plot = monthlyC_2019_barplot , # The plot to save (default is the last plot created)
width = 20, # Width in inches
height = 15, # Height in inches
dpi = 600, # Resolution in dots per inch
units = "cm" # Units for width and height (can be "in", "cm", or "mm")
)
#ggsave("my_plot.pdf", width = 8, height = 6, dpi = 300, units = "in")
```
#Comparing gapfilled to non-gapfilled with boxplot
```{r}
# Create the boxplot from HH data plotting FC_no NAs and FC_F_no NAs to compare original to gap-filled data
#create a new d_long df so it can plot the FC and FC_F_no NA variables side by side
# Reshape the data to long format with columns for datatype (FC vs FC_F_no NAs)
df_2017long <- df_2017 %>%
pivot_longer(cols = c(FC_no_NAs, FC_F_no_NAs), names_to = "DataType", values_to = "CO2flux")
df_2017long2 <- df_2017 %>%
pivot_longer(cols = c(FCH4_no_NAs, FCH4_F_no_NAs), names_to = "DataType", values_to = "CH4emissions")
# Create the boxplot for CO2
ggplot(df_2017long, aes(x = as.factor(month), y = CO2flux, fill = DataType)) +
geom_boxplot(position = position_dodge(width = 0.8)) +
labs(x = "Month", y = "HH FC (g C"~m^-2~HH^-1*")",
title = "HH FC_no NAs vs FC_F_no NAs Carbon Emissions (2017)",
fill = "Data Type") +
theme_minimal()
# Create the boxplot for FCH4 vs FCH4_F no NAs --> won't work, won't do FCH4 no NAs ** review code
ggplot(df_2017long, aes(x = as.factor(month), y = CH4emissions, fill = DataType)) +
geom_boxplot(position = position_dodge(width = 0.8)) +
labs(x = "Month", y = "HH FCH4 (g C"~m^-2~HH^-1*")",
title = "HH FCH4_no NAs vs FCH4_F_no NAs Carbon Emissions (2017)",
fill = "Data Type") +
theme_minimal()
#boxplot for just FCH4_F_no NAs
ggplot(df_2017, aes(x = as.factor(month), y = FCH4_F_no_NAs)) +
geom_boxplot(position = position_dodge(width = 0.8)) +
labs(x = "Month", y = "HH FCH4_F_no NAs (g C"~m^-2~HH^-1*")",
title = "HH FCH4_no NAs vs FCH4_F_no NAs Carbon Emissions (2017)",
fill = "Data Type") +
theme_minimal()
```
#Boxplot of daily avg (df_avg) C per month for every year
```{r}
#Creating boxplot from daily avg -- only need to convert if you haven't yet already for the daily averages - if yes, skip down to extracting the month as a numeric value
#convert CO2 data from umolC/m2/s to gC/m2/day
#net CO2 flux - gapfilled
df_avg_2019 <- df_avg_2019 %>%
mutate(FC_F_no_NAs = ifelse(is.na(FC_F), 0, FC_F * 60 * 60 * 24* (1/1000000) * 12))
#net CO2 flux not gapfilled
df_avg_2019 <- df_avg_2019 %>%
mutate(FC_no_NAs = ifelse(is.na(FC), 0, FC * 60 * 60 * 24 * (1/1000000) * 12))
# CH4 Units start as nano-moles of CH4/(m^2/s), converted to gC/m^2/day
#Net CH4 Flux - gapfilled
df_avg_2019 <- df_avg_2019 %>%
mutate(FCH4_F_no_NAs = ifelse(is.na(FCH4_F), 0, FCH4_F * 60 * 60 * 24 * (1/1000000000) * 12))
#net CH4 flux not gapfilled
df_avg_2019 <- df_avg_2019 %>%
mutate(FCH4_no_NAs = ifelse(is.na(FCH4), 0, FCH4 * 60 * 60 * 24* (1/1000000000) * 12))
########################################################################################################################
# Extract the month as a numeric value
df_avg_2023$month <- format(df_avg_2023$date, "%m")
# Create the boxplot from gapfilled data - CO2
ggplot(df_avg_2023, aes(x = as.factor(month), y = FC_F_no_NAs)) +
geom_boxplot() +
labs(x = "Month", y = "FC_F_no NAs (g C"~m^-2~d^-1*")", title = "Avg daily FC_F Emissions per Month (2023)") +
theme_minimal()+
geom_hline(yintercept=0)
# Create the boxplot from gapfilled data - CH4
ggplot(df_avg_2023, aes(x = as.factor(month), y = FCH4_F_no_NAs)) +
geom_boxplot() +
labs(x = "Month", y = "FCH4_F_no NAs (g C"~m^-2~d^-1*")", title = "Avg daily CH4_F Emissions per Month (2023)") +
theme_minimal()+
geom_hline(yintercept=0)
#just checking sums here
# Filter the dataframe for October 2023 and calculate the sum of FC_F_no NAs
sum_october_2023 <- sum(df_avg_2023$FC_F_no_NAs[format(df_avg_2023$date, "%Y-%m") == "2023-10"], na.rm = TRUE)
# Print the result
sum_october_2023
#find avg daily emission of g C by month
avg_dec_2023 <- mean(df_avg_2023$FC_F_no_NAs[format(df_avg_2023$date, "%Y-%m") == "2023-12"], na.rm = TRUE)
avg_dec_2023
```
#Dailv avg boxplot of gapfilled vs not gapfilled
```{r}
# Create the boxplot plotting FC_no NAs and FC_F_no NAs to compare original to gap-filled data
#create a new d_long df so it can plot the FC and FC_F_no NA variables side by side
# Reshape the data to long format with columns for datatype (FC vs FC_F_no NAs)
df_avg_2017long <- df_avg_2017 %>%
pivot_longer(cols = c(FC_no_NAs, FC_F_no_NAs), names_to = "DataType", values_to = "CO2flux")
df_avg_2017long2 <- df_avg_2017 %>%
pivot_longer(cols = c(FCH4_no_NAs, FCH4_F_no_NAs), names_to = "DataType", values_to = "CH4emissions")
# Create the boxplot for CO2 - gapfilled vs not
ggplot(df_avg_2017long, aes(x = as.factor(month), y = CO2flux, fill = DataType)) +
geom_boxplot(position = position_dodge(width = 0.8)) +
labs(x = "Month", y = "HH FC (g C"~m^-2~d^-1*")",
title = "FC_no NAs vs FC_F_no NAs Carbon Emissions (2017)",
fill = "Data Type") +
theme_minimal()
# Create the boxplot for FCH4 vs FCH4_F no NAs --> won't work, won't do FCH4 no NAs ** review code
ggplot(df_avg_2017long, aes(x = as.factor(month), y = CH4emissions, fill = DataType)) +
geom_boxplot(position = position_dodge(width = 0.8)) +
labs(x = "Month", y = "HH FCH4 (g C"~m^-2~d^-1*")",
title = "HH FCH4_no NAs vs FCH4_F_no NAs Carbon Emissions (2017)",
fill = "Data Type") +
theme_minimal()
#boxplot for just FCH4_F_no NAs
ggplot(df_avg_2017, aes(x = as.factor(month), y = FCH4_F_no_NAs)) +
geom_boxplot(position = position_dodge(width = 0.8)) +
labs(x = "Month", y = "HH FCH4_F_no NAs (g C"~m^-2~d^-1*")",
title = "HH FCH4_no NAs vs FCH4_F_no NAs Carbon Emissions (2017)",
fill = "Data Type") +
theme_minimal()
```
# Seasonal Delineations - finding seasonal boundaries
Plots ratio of par in/out, soil heat flux data, snow depth, air temp to identify seasonal trends, use these plots to explore delineations and then populate the cleaned up version for each year in the figures in the next chunk
```{r}
#Explore seasonal delineations for each year here
#ggplot of PPFD ratio
#Photosynthetic photon flux density, incoming / outgoing --> if ratio is closer to 1, it indicates more reflectance from snow; if ratio is closer to 0, it's less reflectance and likely vegetation / no snow
ggplot(data = df_avg_2023) +
theme_bw() +
geom_line(aes(x = DOY, y = (PPFD_OUT*500) / PPFD_IN), color = 'pink') +
geom_vline(xintercept = 136)+ #end of winter - 3+ days of air temp above 0 & low PPFD ratio -- lag in ground temp likely is 0 curtain period
#geom_vline(xintercept = 162)+ #end of snowmelt --> Council does not seem to have snowmelt variable
geom_vline(xintercept = 248)+ #end of growing season --> FC_F < 0 for 3 days
geom_vline(xintercept = 286)+ #end of Fall Senescence / start of winter --> it's 272 for 3+ days of ground temp below 0; it's 268 -272 for 3+ days of air temperature <0
geom_hline(yintercept=0)+ #makes a black line at y=0 to indicate sink vs source
#scale_x_continuous(limits = c(0, 366))+ #whole year
scale_x_continuous(limits = c(100, 366))+
labs(title = "2023")
#PAR shows up after 100 DOY -- since we multiplied PPFD ratio by 500 to get it to show up on the scale, low around 200 = summer, and high ratio around 500 and up means snow
#ggplot of air temp, ground temp, and FC_F
ggplot(data = df_avg_2017) +
theme_bw() +
geom_line(aes(x = DOY, y = TS_3_1_1), color = 'red') + #ground temp at 15cm in "tussock"
geom_line(aes(x = DOY, y = TA), color = 'orange') + #air temp
geom_line(aes(x = DOY, y = (FC_F*15)), color = 'darkblue') + #CO2 flux
scale_y_continuous(name = expression('Biomet Vars & FC'), limits = c(-30,20)) +
geom_vline(xintercept = 102)+ #end of winter - 3+ days of air temp above 0 & low PPFD ratio -- lag in ground temp likely is 0 curtain period
#geom_vline(xintercept = 162)+ #end of snowmelt --> Council does not seem to have snowmelt variable
geom_vline(xintercept = 259)+ #end of growing season --> FC_F < 0 for 3 days
geom_vline(xintercept = 280)+ #end of Fall Senescence / start of winter --> 3+ days of ground temp below 0; it's 268 -272 for 3+ days of air temperature <0
geom_hline(yintercept=0)+ #makes a black line at y=0 to indicate sink vs source
#scale_x_continuous(limits = c(0, 366)) #whole year
scale_x_continuous(limits = c(100, 366)) +#zooming in on DOY ranges
#geom_line(aes(x = DOY, y = D_SNOW/2), color = 'purple') + #dataset doesn't seem to have snow
labs(title = "2017")
#these look wonky, may have to clean more to get this to look better**
```
# Cleaned up seasonal delineations - swap out years
```{r}
ggplot(data = df_avg_2023) +
theme_bw() +
# Map color inside aes() to include in the legend
geom_line(aes(x = DOY, y = TS_3_1_1, color = 'Ground Temp')) + # ground temp at 15cm in "tussock"
geom_line(aes(x = DOY, y = TA, color = 'Air Temp')) + # air temp
geom_line(aes(x = DOY, y = (FC_F*15), color = 'CO2 Flux * 15')) + # CO2 flux
#diff in air and soil temp above 0 could be snowmelt, compare to PAR data
# Y-axis label and limits
scale_y_continuous(name = expression('Biomet Vars & FC'), limits = c(-30, 20)) +
# Vertical and horizontal reference lines for each year 2017-2022
#2017
# geom_vline(xintercept = 127) + # end of winter - 3+ days of air temp above 0
# geom_vline(xintercept = 258) + # end of growing season - FC_F < 0 for 3 days
# geom_vline(xintercept = 289) + # end of Fall Senescence / start of winter --> 3+ days of air temperature < 0
#2018
# geom_vline(xintercept = 150) + # end of winter - 3+ days of air temp above 0 - p
# geom_vline(xintercept = 256) + # end of growing season - FC_F < 0 for 3 days
# geom_vline(xintercept = 299) + # end of Fall Senescence / start of winter --> 3+ days of air temperature < 0
#2021
# geom_vline(xintercept = 143) + # end of winter - 3+ days of air temp above 0 - p
# geom_vline(xintercept = 253) + # end of growing season - FC_F < 0 for 3 days
# geom_vline(xintercept = 284) + # end of Fall Senescence / start of winter --> 3+ days of air temperature < 0
# #2020
# geom_vline(xintercept = 130) + # end of winter - 3+ days of air temp above 0 - p
# geom_vline(xintercept = 249) + # end of growing season - FC_F < 0 for 3 days
# geom_vline(xintercept = 286) + # end of Fall Senescence / start of winter --> 3+ days of air temperature < 0
#2021
# geom_vline(xintercept = 143) + # end of winter - 3+ days of air temp above 0
# geom_vline(xintercept = 259) + # end of growing season - FC_F < 0 for 3 days
# geom_vline(xintercept = 288) + # end of Fall Senescence / start of winter --> 272 for 3+ days of air temperature < 0
# #2022
# geom_vline(xintercept = 137) + # end of winter - 3+ days of air temp above 0
# geom_vline(xintercept = 259) + # end of growing season - FC_F < 0 for 3 days
# geom_vline(xintercept = 280) + # end of Fall Senescence / start of winter --> 272 for 3+ days of air temperature < 0
# #2023
geom_vline(xintercept = 156) + # end of winter - 3+ days of air temp above 0
geom_vline(xintercept = 239) + # end of growing season - FC_F < 0 for 3 days
geom_vline(xintercept = 280) + # end of Fall Senescence / start of winter --> 272 for 3+ days of air temperature < 0
geom_hline(yintercept = 0) + # horizontal line at y = 0
# X-axis limits
scale_x_continuous(limits = c(100, 366)) + #(zoomed into specific time)
#scale_x_continuous(limits = c(0, 366)) #whole year
# Define manual color scale for legend
scale_color_manual(values = c('Ground Temp' = 'red',
'Air Temp' = 'orange',
'CO2 Flux * 15' = 'darkblue')) +
# Add labels and title
labs(color = "Variables")+ # This adds the title for the legend
labs(title = "2023")
```
####Seasonal Plots with temp and NEE for each year
Definition of seasons: 3+ days of FC_F<0 (negative) for growing season, 3+ days of mostly pos NEE for end of growing season, and 3+ days of air temperature <0 for winter. Gaps are senescence and spring (?) - no snowmelt data
####2017 Seasonal plot
```{r}
#2017
# colnames(df_avg_2017)[colnames(df_avg_2017) == "date"] <- "day"
#
# df_avg_2017$date <- as.POSIXct(df_avg_2017$date)
#
# df_avg_2017$date <- as.POSIXct(df_avg_2017$date, format = "%Y-%m-%d")
## Plot with legend-
ggplot(data = df_avg_2017) +
theme_bw() +
# horizontal line at y = 0
geom_hline(yintercept=0, colour = "black")+
#lines for soil and air temperature
geom_line(aes(x = date, y = TS_3_1_1/15, colour = "15cm Soil Temperature")) + #divided to scale
geom_line(aes(x = date, y = TA/15, colour = "Air Temperature")) +
geom_line(aes(x = date, y = TA_ERA5/15, colour = "ERA5 Air Temperature"), linetype = "dashed")+
# NEE (converting from umol/m2/s to gC/m2/day)
geom_point(aes(x = date, y = FC_F*60*60*24*(1/1000000)*12, colour = "NEE"))+
# NEE and temp axis scales
scale_y_continuous(
name = expression(NEE~(gCO[2]-C~m^-2~d^-1)), limits =c(-4,3),
sec.axis = sec_axis(~ . * 15, name = expression("Temperature ("*degree*"C)"))) +
# color legend for the lines
scale_color_manual(
name = "",
values = c("15cm Soil Temperature" = "darkblue", "Air Temperature" = "salmon", "NEE" = "turquoise3", "ERA5 Air Temperature" = "darkgreen"))+
# vertical lines for specific events
geom_vline(xintercept = as.POSIXct("2017-05-10"))+ #end of winter, DOY 126
#geom_vline(xintercept = as.POSIXct("2017-06-12"))+ #end of snowmelt, DOY 163
geom_vline(xintercept = as.POSIXct("2017-09-14"))+ #end of growing season, DOY 258
geom_vline(xintercept = as.POSIXct("2017-10-17")) + #end of Fall Senescence, DOY 289
# Labels for each vertical line within figure
annotate("text", size = 3, x = as.POSIXct("2017-02-05"), y = -4, label = "Winter", color = "black") +
annotate("text", size = 3, x = as.POSIXct("2017-07-06"), y = -4, label = "Growing Season", color = "black") +
annotate("text", size = 3, x = as.POSIXct("2017-10-10"), y = -4, label = "Senescence", color = "black") +
annotate("text", size = 3, x = as.POSIXct("2017-12-15"), y = -4, label = "Winter", color = "black") +
#Title for the plot
labs(title = "Seasonal Trends: 2017", x = "Date")
# anyDuplicated(names(df_avg_2017))
# duplicated(names(df_avg_2017))
# head(df_avg_2017)
#plot(df_avg_2017$NEE, df_avg_2017$date)
```
```{r}
# Definition of 2017 seasons into the daily average dataframe
df_avg_2017 <- df_avg_2017 %>%
mutate(
season = case_when(
(DOY >= 258 & DOY <= 290) ~ 'Fall Senescence',
(DOY >= 131 & DOY <= 257) ~ 'Growing Season',
(DOY >= 291 | DOY <= 130) ~ 'Winter',
TRUE ~ NA_character_
)
)
# # copied over for quick DOY reference for 2017
# geom_vline(xintercept = as.POSIXct("2017-05-10"))+ #end of winter, DOY 130*
# geom_vline(xintercept = as.POSIXct("2017-09-14"))+ #end of growing season, DOY 257*
# geom_vline(xintercept = as.POSIXct("2017-10-17")) + #end of Fall Senescence, DOY 290*
#add to HH df
df_2017 <- df_2017 %>%
mutate(
season = case_when(
(DOY >= 258 & DOY <= 290) ~ 'Fall Senescence',
(DOY >= 131 & DOY <= 257) ~ 'Growing Season',
(DOY >= 291 | DOY <= 130) ~ 'Winter',
TRUE ~ NA_character_
)
)
```
####2018 Seasonal Plot with NEE and temp
```{r}
#2018
# colnames(df_avg_2018)[colnames(df_avg_2018) == "date"] <- "day"
#
# df_avg_2018$day <- as.POSIXct(df_avg_2018$day)
## Plot with legend- use only for legend
ggplot(data = df_avg_2018) +
theme_bw() +
# horizontal line at y = 0
geom_hline(yintercept=0, colour = "black")+
#lines for soil and air temperature
geom_line(aes(x = date, y = TS_3_1_1/15, colour = "15cm Soil Temperature")) + #divided to scale
geom_line(aes(x = date, y = TA/15, colour = "Air Temperature")) +
geom_line(aes(x = date, y = TA_ERA5/15, colour = "ERA5 Air Temperature"), linetype = "dashed")+
# NEE (converting from umol/m2/s to gC/m2/day)
geom_point(aes(x = date, y = FC_F*60*60*24*(1/1000000)*12, colour = "NEE"))+
# NEE and temp axis scales
scale_y_continuous(
name = expression(NEE~(gCO[2]-C~m^-2~d^-1)), limits =c(-4,3),
sec.axis = sec_axis(~ . * 15, name = expression("Temperature ("*degree*"C)"))) +
# color legend for the lines
scale_color_manual(
name = "",
values = c("15cm Soil Temperature" = "darkblue", "Air Temperature" = "salmon", "NEE" = "turquoise3", "ERA5 Air Temperature" = "darkgreen"))+
# vertical lines for specific events
geom_vline(xintercept = as.POSIXct("2018-05-31"))+ #end of winter, DOY 151*
#geom_vline(xintercept = as.POSIXct("2018-06-12"))+ #end of snowmelt, DOY 163
geom_vline(xintercept = as.POSIXct("2018-09-13"))+ #end of growing season, DOY 256*
geom_vline(xintercept = as.POSIXct("2018-10-27")) + #end of Fall Senescence, DOY 300*
# Labels for each vertical line within figure
annotate("text", size = 3, x = as.POSIXct("2018-03-01"), y = -4, label = "Winter", color = "black") +
annotate("text", size = 3, x = as.POSIXct("2018-07-19"), y = -4, label = "Growing Season", color = "black") +
annotate("text", size = 3, x = as.POSIXct("2018-10-10"), y = -4, label = "Senescence", color = "black") +
annotate("text", size = 3, x = as.POSIXct("2018-12-15"), y = -4, label = "Winter", color = "black") +
#Title for the plot
labs(title = "Seasonal Trends: 2018", x = "Date")
```
```{r}
# Add seasonal delineations to the daily average dataframe
df_avg_2018 <- df_avg_2018 %>%
mutate(
season = case_when(
(DOY >= 257 & DOY <= 300) ~ 'Fall Senescence',
(DOY >= 152 & DOY <= 256) ~ 'Growing Season',
(DOY >= 301 | DOY <= 151) ~ 'Winter',
TRUE ~ NA_character_
)
)
# # copied here for reference
# geom_vline(xintercept = as.POSIXct("2018-05-31"))+ #end of winter, DOY 151*
# geom_vline(xintercept = as.POSIXct("2018-09-13"))+ #end of growing season, DOY 256*
# geom_vline(xintercept = as.POSIXct("2018-10-27")) + #end of Fall Senescence, DOY 300*
#add to HH df
df_2018 <- df_2018 %>%
mutate(
season = case_when(
(DOY >= 257 & DOY <= 300) ~ 'Fall Senescence',
(DOY >= 152 & DOY <= 256) ~ 'Growing Season',
(DOY >= 301 | DOY <= 151) ~ 'Winter',
TRUE ~ NA_character_
)
)
```
####2019 Seasonal Plot with NEE and temp
```{r}
#2019 --> didn't add ERA5 here as the temp data is nearly complete
# colnames(df_avg_2019)[colnames(df_avg_2019) == "date"] <- "day"
# df_avg_2019$day <- as.POSIXct(df_avg_2019$day)
## Plot with legend- use only for legend