R/addShadedBackground.R

Defines functions addShadedBackground

Documented in addShadedBackground

#' @export
#' @title Add Shaded Background to a Plot
#' @param param vector of data to be represented
#' @param timeAxis vector of times of the same length as param
#' @param breaks set of breaks used to assign colors
#' @param col color for vertical lines
#' @param maxOpacity maximum opacity
#' @param lwd line width
#' @description Adds vertical lines to an existing plot using any variable that shares the same
#' length as the time axis of the current plot. Line widths corresponds to magnitude of values.

########################################################################################
#
# This function adds shading to an existing plot using any variable that shares the same
# length as the time axis of the current plot.
#
########################################################################################

addShadedBackground <- function(param,
                                timeAxis,
                                breaks=stats::quantile(param, na.rm = TRUE),
                                col = 'blue',
                                maxOpacity = 0.2,
                                lwd = 1) {

  assignedBin <- .bincode(param, breaks, include.lowest = TRUE)
  colors <- c()
  for (i in seq_along(breaks)-1) {
    opacity <- maxOpacity*(i-1)/(length(breaks)-1)
    colors[i] <- adjustcolor(col,opacity)
  }

  abline(v = timeAxis, col = colors[assignedBin], lwd = lwd)

}

Try the PWFSLSmoke package in your browser

Any scripts or data that you put into this service are public.

PWFSLSmoke documentation built on July 8, 2020, 7:19 p.m.