R/fPlotDailyMeans_forYear.R

Defines functions fPlotDailyMeans_forYear

Documented in fPlotDailyMeans_forYear

#' This function will create 365-day time series plot of a flux/meteorological variable 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 y character, specify the name of the data column containing variable of interest
#' @param yr numeric, year of interest for current plot
#' @param span numeric, set the span for time series smoothing




fPlotDailyMeans_forYear <- function(dat, x = "fracyr_null", y, yr, span) {


  # browser()
  dat <- subset(dat, process_year == yr)

  VARIABLE_Selected <- y
  VARIABLE_Meta <- fLookUp_variable_info(VARIABLE_Selected)

  VARIABLE_Name <- VARIABLE_Meta$Name

  VARIABLE_Units <- VARIABLE_Meta$Units

  VARIABLE_Title <- VARIABLE_Meta$Title

  yrange <- range(dat[[y]], na.rm = TRUE)
  ymax <- yrange[2] + (yrange[2] - yrange[1])/10 # Add a little padding to the max value
  ylim <- c(yrange[1], ymax)


  xmin <- min(dat[[x]])
  xmax <- max(dat[[x]])

  xlim <- c(xmin, xmax)

  # Specify breaks for the x-axis
  DoYBreaks <- fracyr_to_DoYBreaks(interval = 100, xmin = xlim[1], xmax = xlim[2], padding = FALSE )

  p <- ggplot() +
    geom_point(data = dat, aes(x = .data[[x]], y = .data[[y]]), alpha = 0.1) +
    geom_smooth(data = dat, aes(x = .data[[x]], y = .data[[y]]),
                method = "loess", formula = y ~ x, span = span, se = FALSE, color = "black", size = 0.5, alpha = 0.5) +
    scale_y_continuous(name= VARIABLE_Units, limits = ylim) +
    scale_x_continuous(name = "DoY", breaks = DoYBreaks$fracyrBreaks,
                       labels = DoYBreaks$DoYBreaks,
                       limits = c(DoYBreaks$lwr[1], DoYBreaks$upr[1])) +


    geom_line(alpha=0.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(),
          legend.position = "bottom",
          legend.title = element_blank(),
          panel.grid.major = element_line(linetype = "dotted", color = "gray")) +
    ggtitle(VARIABLE_Title)

  return(p)


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