#' This function will create a composite plot of cumulative GPP/NEE over time, as well as the first derivative of cumulative GPP, for the purposes of determining the GPP-free period.
#'
#' @export
#' @title Plot time series of cumulative GPP/NEE
#' @param dat dataframe, a specific dataframe format created in function 'FS_fExtractAllCriticalDates' that includes cumulative daily GPP, cumulative NEE, 1st derivative of cumu GPP
#' @param critdat dataframe, a specific dataframe format containing a list of critical dates - created in function 'FS_fExtractAllCriticalDates'
#' @param site.info list, metadata for current site
#' @param years_of_record numeric, vector of unique individual years for current site
fPlotCumuGPPbyYear <- function(dat, critdat, site.info, years_of_record) {
# Initialize an empty list
plot.list <- list()
for (yr in years_of_record) {
tryCatch(
expr = {
Sys.sleep(0.1)
annotations <- data.frame(
xpos = c(-Inf),
ypos = c(Inf),
annotateText = yr,
hjustvar = -0.1,
vjustvar = 1) #<- adjust
df <- subset(dat, process_year == yr)
df.crit <- subset(critdat, process_year == yr)
# # Define x-axis breaks
DoYInterval <- 60
DoYBreaks_Cumu <- df %>%
select(Year, DoY, cumu.DoY_raw) %>%
filter(DoY %in% c(1, head(seq(DoYInterval,366,DoYInterval),-1)))
YearBreaks_Cumu <- DoYBreaks_Cumu %>%
subset(., DoY == 1)
colors <- c("ECW" = "#e7d4e8", "SNW" = "#762a83")
colors <- colors[order(factor(names(colors), levels = c("ECW", "SNW")))]
if (sum(!is.na(df$deriv.GPP_raw_NA)) == 0) {
message("")
message(" * ERROR on process year: ", yr)
message(" NO data found!")
message(" (skipping to next process year)")
message("")
next
}
p1 <- ggplot(df, aes(x = cumu.DoY_raw)) +
geom_point(alpha=0.4, aes( y= deriv.GPP_raw_NA), color= "#1a9850") +
geom_line(aes(y = deriv.GPP_smooth), color="#1a9850", size = 1) +
geom_vline(xintercept=YearBreaks_Cumu$cumu.DoY_raw) +
scale_x_continuous(name = "DoY", breaks = DoYBreaks_Cumu$cumu.DoY_raw,
labels = DoYBreaks_Cumu$DoY) +
scale_y_continuous(name = "deriv (cumulative GPP)") +
theme_time() +
annotate("segment", x=-Inf, xend=Inf, y=-Inf, yend=-Inf, size=1.5, color = "black") +
annotate("segment", x=-Inf, xend=-Inf, y=-Inf, yend=Inf, size=1.5, color = "black") +
theme(plot.title = element_text(color="black", hjust = 0.5, size=12, face="bold"),
plot.subtitle = element_text(color="black", hjust = 0.5, size=12),
strip.background = element_blank(),
strip.text = element_text(size = 12, face = "bold"),
axis.line = element_blank(),
legend.position = "bottom",
legend.title = element_blank(),
panel.grid.major = element_line(linetype = "dotted", color = "gray"))
deriv.plot <- p1 +
geom_point(data = df.crit, aes(x = cumu.DoY_raw, y = deriv.GPP_smooth, fill = CritThreshold), shape = 21, color= "black", size = 5, stroke = 1) +
geom_text(data = df.crit, aes(x = cumu.DoY_raw, y = deriv.GPP_smooth, label = DoY), nudge_x = c(-20,20), vjust = -3, fontface = "bold", size = 4) +
scale_fill_manual(name = "Critical Date", values = colors,
breaks = c("ECW", "SNW")) +
theme(legend.position = "none")
## Cumulative NEE/GPP plot
# Value used to transform the data
coeff <- 10
cumu.GPP.plot <- ggplot(data = df, aes(x=cumu.DoY_raw)) +
geom_line( aes(y=cumu.NEE_raw_NA), size=1, color="black") +
geom_line( aes(y=cumu.GPP_raw_NA / coeff), size=1, color="#1a9850") +
geom_point(data = df.crit, aes(x = cumu.DoY_raw, y = cumu.GPP_raw_NA/ coeff, fill = CritThreshold), shape = 21, color= "black", size = 5, stroke = 1) +
geom_vline(xintercept=YearBreaks_Cumu$cumu.DoY_raw) +
scale_y_continuous(
# Features of the first axis
name = expression(atop("cumulative NEE",paste("(g C ", m^-2,")"))),
# Add a second axis and specify its features
sec.axis = sec_axis(~.*coeff, name=expression(atop("cumulative GPP",paste("(g C ", m^-2,")"))))
) +
scale_fill_manual(name = "Critical Date", values = colors,
breaks = c("ECW", "SNW")) +
scale_x_continuous(name = "DoY", breaks = DoYBreaks_Cumu$cumu.DoY_raw,
labels = DoYBreaks_Cumu$DoY) +
geom_text(data = df.crit, aes(x = cumu.DoY_raw, y = cumu.GPP_raw_NA/ coeff, label = DoY), nudge_x = 0.15, vjust = 3, fontface = "bold", size = 4) +
theme_time() +
annotate("segment", x=-Inf, xend=Inf, y=-Inf, yend=-Inf, size=1.5, color = "black") +
annotate("segment", x=-Inf, xend=-Inf, y=-Inf, yend=Inf, size=1.5, color = "black") +
theme(plot.title = element_text(color="black", hjust = 0.5, size=12, face="bold"),
plot.subtitle = element_text(color="black", hjust = 0.5, size=12),
strip.background = element_blank(),
strip.text = element_text(size = 12, face = "bold"),
axis.line = element_blank(),
axis.title.y.right = element_text(size = 14, angle = 90, color="#1a9850"),
axis.text.y.right = element_text(size = 12, color ="#1a9850"),
legend.position = "none",
panel.grid.major = element_line(linetype = "dotted", color = "gray"))
cumu.GPP.plot <- cumu.GPP.plot +
annotate("text", x = YearBreaks_Cumu$cumu.DoY_raw, y = Inf,
label=YearBreaks_Cumu$Year, hjust = -0.5, vjust = 1, fontface = "bold", size = 6)
cumu.GPP.composite.plot <- cumu.GPP.plot + deriv.plot + plot_layout(ncol =1)
cumu.GPP.composite.plot <- wrap_elements(cumu.GPP.composite.plot)
cumu.GPP.composite.plot.final <- fAddSiteMeta2Plot(cumu.GPP.composite.plot, site = site.info$site,
db = site.info$db,
longname = site.info$info,
country = site.info$country,
lat = site.info$lat,
long = site.info$long,
MAT = site.info$MAT,
MAP = site.info$MAP,
IGBP = site.info$IGBP)
# browser()
plot.list[[as.character(yr)]] <- cumu.GPP.composite.plot.final
},
# If loop fails for a particular year,
error = function(e){
message("")
message(" * ERROR on process year: ", yr)
message(" (NAs will be assigned to data during missing years)")
message("")
print(e)
}
)
}
return(plot.list)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.