R/fPlotMultiyearGPPsat.R

Defines functions fPlotMultiyearGPPsat

Documented in fPlotMultiyearGPPsat

#' This function will create a time series plot of GPPsat from days 1-365, wtih each year (and SOS/EOS) overlaid.
#'
#' @export
#' @title Plot multiyear daily time series of GPPsat (with SOS/EOS)
#' @param GPP numeric, requires a vector of daily GPP for a given process_year
#' @param GPP.pred numeric, requires a vector of PREDICTED daily GPP for a given process_year
#' @param GPP.POSIX date, requires a vector of POSIX dates (YYYY-MM-DD) for each process_year
#' @param GPP.ProcYr numeric, requires a vector of process_years (used to facet our data by individual years)
#' @param crit.ID character, requires a vector of critical threshold labels (e.g. SOS10, SOS25) (vector must be of equal length as the number of individual critical dates)
#' @param crit.POSIX date, requires a vector of POSIX dates (YYYY-MM-DD) for each critical threshold
#' @param crit.ProcYr numeric, requires a vector of process_years (used to identify which subset of data the critical thresholds were estimated from)
#' @param crit.GPP numeric, requires a vector of GPPsat estimates for each critical threshold date
#' @param span numeric, set the span for time series smoothing (defaults to 0.075) (vector must be of equal length as the number of individual critical dates)




fPlotMultiyearGPPsat <- function(GPP, GPP.pred, GPP.POSIX, GPP.ProcYr, crit.ID, crit.POSIX, crit.ProcYr, crit.GPP, span) {

  GPP.df <- data.frame(GPP.ProcYr, GPP.POSIX, GPP, GPP.pred)
  names(GPP.df) <- c("process_year", "k_POSIXdate_plotting", "GPP", "GPP_pred")


  crit.df <- data.frame(crit.ProcYr, crit.POSIX, crit.ID, crit.GPP)
  names(crit.df) <- c("process_year", "k_POSIXdate_plotting", "CriticalThreshold", "Crit_pred")

  CritDates <- crit.df %>%
    left_join(GPP.df, by = c("process_year", "k_POSIXdate_plotting")) %>%
    mutate(fracyr = decimal_date(k_POSIXdate_plotting),
           fracyr_null = fPOSIX_to_fracyr_null(k_POSIXdate_plotting, process_year),
           DoY = yday(k_POSIXdate_plotting))


  PlottingData <- GPP.df %>%
    mutate(fracyr = decimal_date(k_POSIXdate_plotting),
           fracyr_null = fPOSIX_to_fracyr_null(k_POSIXdate_plotting, process_year),
           DoY = yday(k_POSIXdate_plotting))



  # Specify labels and ranges for your plots
  ylab <- expression(atop("GPPsat (NEE method, fixed 'b')",paste("(µmol ", m^-2," ", s^-1, ")")))
  yrange <- range(PlottingData$GPP_pred, na.rm = TRUE)
  ymax <- yrange[2] + (yrange[2] - yrange[1])/10

  # Set a limits to the yaxis range that's reasonable (if your data falls below this threshold, it won't be plotted)
  if (yrange[1] < -10) {
    yrange[1] <- -10
  }

  if (ymax > 40) {
    ymax <- 40
  }

  ylim <- c(yrange[1], ymax)

  xmin <- min(PlottingData$fracyr_null)
  xmax <- max(PlottingData$fracyr_null)


  colors <- c("SOS10" = "#1a9850", "SOS25" = "#91cf60", "SOS50" = "#d9ef8b", "Peak_GPPsat" = "white",
              "EOS10" = "#d73027", "EOS25" = "#fc8d59", "EOS50" = "#fee08b")

  colors <- colors[order(factor(names(colors), levels = c("SOS10", "SOS25", "SOS50", "Peak_GPPsat", "EOS50", "EOS25", "EOS10")))]


  # Specify breaks for the x-axis
  DoYBreaks <- fracyr_to_DoYBreaks(interval = 60, xmin = xmin , xmax = xmax, padding = TRUE )

  plot1_multiyear <- ggplot() +
    geom_vline(xintercept = DoYBreaks$fracyrBreaks, linetype = "dotted", alpha = 0.3, color = "black") +
    geom_vline(aes(xintercept = c(1,2)), linetype = "solid", alpha = 0.3) +
    geom_hline(aes(yintercept = 0), linetype = "dashed", alpha = 0.3) +
    geom_point(data = PlottingData, aes(x = fracyr_null, y = GPP, color = factor(process_year)), alpha = 0.1) +
    geom_line(data = PlottingData, aes(x = fracyr_null, y = GPP_pred, color = factor(process_year)), size = 0.5, alpha = 0.5) +
    scale_y_continuous(name= ylab, limits = ylim) +
    scale_x_continuous(name = "DoY", breaks = DoYBreaks$fracyrBreaks, labels = DoYBreaks$DoYBreaks, limits = c(DoYBreaks$lwr[1], DoYBreaks$upr[1])) +
    geom_point(data = CritDates, aes(x = fracyr_null, y = GPP_pred, fill = CriticalThreshold), shape = 21, color = "black", size = 3) +
    scale_fill_manual(name = "Critical Date", values = colors,
                      breaks = c("SOS10", "SOS25", "SOS50","Peak_GPPsat", "EOS50", "EOS25", "EOS10"),
                      labels = c(expression(SOS[10]), expression(SOS[25]), expression(SOS[50]),
                                 expression(maxGPP["sat"]), expression(EOS[50]), expression(EOS[25]), expression(EOS[10]) )) +
    scale_color_discrete(name = "Process Year") +
    geom_text(aes(x = 1.03, y = ymax), label = "Process\nYear", hjust = 0, vjust = 1, fontface = "bold") +
    geom_text(aes(x = DoYBreaks$lwr[1], y = ymax), label = "Lag\nYear", hjust = 0, vjust = 1, fontface = "bold") +
    geom_text(aes(x = 2.03, y = ymax), label = "Latter\nYear", hjust = 0, vjust = 1, fontface = "bold") +
    ggtitle(paste0("Multiyear GPPsat by site"))  +
    theme_time() +
    theme(plot.title = element_text(color="black", hjust = 0.5, size=14, face="bold"),
          plot.subtitle = element_text(color="black", hjust = 0.5, size=12))





  return(plot1_multiyear)


}
ksmiff33/FluxSynthU documentation built on Dec. 15, 2020, 10:29 p.m.