R/plot_year_comp.R

Defines functions plot_year_comp

Documented in plot_year_comp

#' plot_year_comp
#' 
#' plot the output of \code{parse_year_comp()}
#' 
#' @param df output of \code{parse_year_comp()}
#' @param type one of "median","smooth","smoothmedian"
#'        \itemize{
#'         \item median - plot the rolling median calculated by \code{parse_year_comp()}
#'         \item smooth - plot a loess smooth through the daily median values
#'         \item smoothmedian - plot a loess smooth through the rolling median values
#'        }
#' @param xlim x axis limits via \code{ggplot2::coord_cartesian()} 
#' @param vline_pos numeric vector. add vertical lines at the given day of year
#' @param xlab x axis label
#' @param strip_text_y override the strip text on the y axis
#' @param annotations logical, show plot subtitles? default true
#' @param alpha control the alpha of the underlying timeseries
#' @param ... 
#' 
#' @author W. S. Drysdale
#' 
#' @export
#' 

plot_year_comp = function(df,
                          type = c("median","smooth","smoothmedian")[1],
                          xlim = c(0,365),
                          vline_pos = NULL,
                          xlab = "Day of Year",
                          strip_text_y = NULL,
                          annotations = TRUE,
                          alpha = 0.2,
                          ...){
  
  if(!"name_parsed" %in% names(df))
    df = parse_spec(df)
  
  g = ggplot(df)+
    geom_line(aes(yd,value,
                  group = y, colour = y),
              size = 0.8,alpha = alpha)+
    ylab("")+
    scale_x_continuous(expand = c(0,0),name = xlab)+
    coord_cartesian(xlim=xlim)+
    AQvis_plotTheme()
  
  if(length(unique(df$y)) == 2)
    g = g+scale_colour_manual(values = RColorBrewer::brewer.pal(6,name = "Paired")[c(4,2)],name = "")
  
  
  if(!"code" %in% names(df)){
    g = g+facet_wrap(~name_parsed,
                     scales = "free_y",
                     labeller = "label_parsed",
                     ncol = 1,
                     strip.position = "left")
    
  } else {
    g = g+
      facet_grid(name_parsed~code,
                 scales = "free_y",
                 labeller = "label_parsed",
                 switch = "y")
  }
  
  if(!is.null(vline_pos)){
    vlineDat = data.frame(xintercept = vline_pos)
    
    g = g+geom_vline(data = vlineDat,aes(xintercept = xintercept),linetype = 2,colour = "grey50")
  }
  
  if(type == "median"){
    subtitle = paste0("Smooth line: rolling ",df$rolling_width[1]," day median")
    
    g = g + geom_line(aes(yd,value_median,
                          group = y,colour = y),
                      size = 0.8,
                      ...)
  }
  
  if(type == "smooth"){
    subtitle = paste0("Smooth line: LOESS generated by ggplot2's geom_smooth() applied to daily median values")
    
    g = g + geom_smooth(aes(yd,value,
                            group = y,colour = y),
                        size = 0.8, method = "loess",
                        ...)
    
  }
  
  if(type == "smoothmedian"){
    subtitle = paste0("Smooth line: LOESS generated by ggplot2's geom_smooth() applied to a rolling ",df$rolling_width[1], " day median")
    
    g = g + geom_smooth(aes(yd,value_median,
                            group = y,colour = y),
                        size = 0.8, method = "loess",
                        ...)
  }
  
  if(annotations){
    g = g+labs(subtitle = subtitle)
  }
  
  if(!is.null(strip_text_y))
    g = g+theme(strip.text.y = strip_text_y)
  
  # Return
  
  g
  
  
}
willdrysdale/AQVisR documentation built on July 24, 2022, 8:52 a.m.