R/fPlotGPPsat_plus_VariablesOfInterest.R

Defines functions fPlotGPPsat_plus_VariablesOfInterest

Documented in fPlotGPPsat_plus_VariablesOfInterest

#' This function will create a faceted 365-day time series of GPPsat plus 15 other variables, with each year getting its own page.
#'
#' @export
#' @title Plot 365-day time series of GPPsat (with SOS/EOS) as a single facet among 15 other variables, with each year getting its own array of 16 plots.
#' @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
#' @param vars, names of 15 variable columns to include in plot
#' @param span numeric, set the span for time series smoothing (defaults to 0.075)




fPlotGPPsat_plus_VariablesOfInterest <- function(dat, critdat, site.info, years_of_record, vars, span) {

  # vars example:
  # Select 15 additional variables to facet with GPPsat
  # vars_of_interest <- c("k_LE",
  #                       "k_H",
  #                       "k_Rnet",
  #                       "SW_IN_NT_gf",
  #                       "k_SW_out",
  #                       "k_LW_in",
  #                       "k_LW_out",
  #                       "k_albedo",
  #                       "Tair_NT_gf",
  #                       "Tsoil_NT_gf",
  #                       "k_SWC",
  #                       "k_RH",
  #                       "VPD_NT_gf",
  #                       "k_ustar",
  #                       "PPFD_NT_gf")


  # Initialize an empty list
  plot.list <- list()

  pb <- txtProgressBar(min = 0, max = length(years_of_record), initial = 0, style = 3)
  k <- 1

  for (process_year in years_of_record) {

    k <- which(years_of_record %in% process_year)

    tryCatch(
      expr = {
        Sys.sleep(0.1)

        annotations <- data.frame(
          xpos = c(-Inf),
          ypos =  c(Inf),
          annotateText = process_year,
          hjustvar = -0.1,
          vjustvar = 1) #<- adjust




        plot1 <- fPlotGPPsat_forYear(dat,  x = "fracyr_null", critdat = critdat, yr = process_year) +
          geom_text(data= annotations, aes(x=xpos, y=ypos, hjust=hjustvar, vjust=vjustvar, label = annotateText), size = 10, color = "red")

        plot2 <- fPlotDailyMeans_forYear(dat = dat, y = vars[1], yr = process_year, span = span)
        plot3 <- fPlotDailyMeans_forYear(dat = dat, y = vars[2], yr = process_year, span = span)
        plot4 <- fPlotDailyMeans_forYear(dat = dat, y = vars[3], yr = process_year, span = span)
        plot5 <- fPlotDailyMeans_forYear(dat = dat, y = vars[4], yr = process_year, span = span)
        plot6 <- fPlotDailyMeans_forYear(dat = dat, y = vars[5], yr = process_year, span = span)
        plot7 <- fPlotDailyMeans_forYear(dat = dat, y = vars[6], yr = process_year, span = span)
        plot8 <- fPlotDailyMeans_forYear(dat = dat, y = vars[7], yr = process_year, span = span)
        plot9 <- fPlotDailyMeans_forYear(dat = dat, y = vars[8], yr = process_year, span = span)
        plot10 <- fPlotDailyMeans_forYear(dat = dat, y = vars[9], yr = process_year, span = span)
        plot11 <- fPlotDailyMeans_forYear(dat = dat, y = vars[10], yr = process_year, span = span)
        plot12 <- fPlotDailyMeans_forYear(dat = dat, y = vars[11], yr = process_year, span = span)
        plot13 <- fPlotDailyMeans_forYear(dat = dat, y = vars[12], yr = process_year, span = span)
        plot14 <- fPlotDailyMeans_forYear(dat = dat, y = vars[13], yr = process_year, span = span)
        plot15 <- fPlotDailyMeans_forYear(dat = dat, y = vars[14], yr = process_year, span = span)
        plot16 <- fPlotDailyMeans_forYear(dat = dat, y = vars[15], yr = process_year, span = span)

        # Create faceted plot of all subplots
        f.composite_multivar <- plot1 + plot2 + plot3 + plot4 + plot5 + plot6 + plot7 + plot8 + plot9 +
          plot10 + plot11 + plot12 + plot13 + plot14 + plot15 + plot16 + plot_layout(ncol=4)


        # Add a title to your plot that includes all of the site metadata
        plot_final <- fAddSiteMeta2Plot(wrap_elements(f.composite_multivar), 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)


        plot.list[[as.character(process_year)]]  <-  plot_final

        rm(plot1, plot2, plot3, plot4, plot5, plot6, plot7, plot8,
           plot9, plot10, plot11, plot12, plot13, plot14, plot15, plot16, f.composite_multivar)

      },
      # If loop fails for a particular year,
      error = function(e){
        message("")
        message("    * ERROR on process year: ", process_year)
        message("      (NAs will be assigned to data during missing years)")
        message("")
        print(e)
      }
    )

    # Step up progress bar
    setTxtProgressBar(pb,k)

  }

  return(plot.list)


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