R/fPlotGPPsat_forYear.R

Defines functions fPlotGPPsat_forYear

Documented in fPlotGPPsat_forYear

#' This function will create 365-day time series plot of GPPsat with SOS/EOS overlaid for a given year
#'
#' @export
#' @title Plot 365-day time series of GPPsat (with SOS/EOS) for a single year.
#' @param dat dataframe, a specific dataframe format containing GPPsat data
#' @param x character, specify the name of the column containing null fractional years
#' @param critdat dataframe, a specific dataframe format containing a list of critical dates - created in function 'FS_fExtractAllCriticalDates'
#' @param yr numeric, year of interest for current plot



fPlotGPPsat_forYear <- function(dat, x = "fracyr_null", critdat, yr) {


  dat <- subset(dat, process_year == yr)
  critdat <- subset(critdat, process_year == yr)

  # Specify labels and ranges for your plots
  ylab <- expression(atop("GPPsat (NEE method, fixed 'b')",paste("(µmol ", m^-2," ", s^-1, ")")))
  ytitle <- "GPPsat (NEE method, fixed 'b')"
  yrange <- range(dat$GPPsat_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(dat$fracyr_null)
  xmax <- max(dat$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 = 100, xmin = xmin, xmax = xmax, padding = FALSE )

  plot_base <- ggplot() +
    geom_hline(yintercept = 0, color = "gray") +
    geom_point(data = dat, aes(x = .data[[x]], y = NEEsat_fixed_NEE), alpha = 0.1) +
    geom_line(data = dat, aes(x = fracyr_null, y = GPPsat_pred), size = 0.5, alpha = 0.5) +
    geom_point(data = critdat, aes(x = .data[[x]], y = GPP_pred, fill = CritThreshold), shape = 21, color = "black", size = 3) +
    geom_text(data = critdat, aes(x = .data[[x]], y = GPP_pred, label = DoY),
              nudge_x = 0.15, vjust = 0, size = 3) +
    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])) +
    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]) )) +
    ggtitle(ytitle)  +
    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=14, 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 = "none",
          panel.grid.major = element_line(linetype = "dotted", color = "gray")) +
    guides(fill = guide_legend(nrow = 1))




  return(plot_base)


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