R/qwblankPlot.R

Defines functions qwblankPlot

Documented in qwblankPlot

#'Blank sample timeseries
#'
#' Takes output data object from readNWISodbc and prints a boxplot of blank results
#' @param qw.data A qw.data list generated from readNWISodbc
#' @param plotparm A character vector of the parameter to plot.
#' @param site.selection A character vector of site IDs to plot
#' @param facet Character string of either "multisite" for plotting all sites on one plot or "Facet" for plotting sites on individual plots
#' @param scales Character string to define y axis on faceted plots. Options are "free","fixed","free_x", or "free_y"
#' @param printPlot Logical. Prints plot to graphics device if TRUE
#' @examples 
#' data("exampleData",package="WQReview")
#' qwblankPlot(qw.data = qw.data,
#'                        site.selection = "All",
#'                        plotparm = "00915",
#'                        printPlot = TRUE
#'                        )
#' @import ggplot2
#' @importFrom stringr str_wrap
#' @export

qwblankPlot <- function(qw.data,
                        plotparm,
                        site.selection = "All",
                        facet = "multisite",
                        scales="fixed",
                        printPlot = TRUE){
  
  dqi.colors <- c("#000000", "#E69F00")
  names(dqi.colors) <- c("DQI = R","DQI = I, S, or P")
  
  if(length(plotparm) > 1)
  {
    stop("Only one parmater can be supplied")
  }
  
  give.n <- function(x){
    return(c(y = median(x)*1.05, label = length(x))) 
    # experiment with the multiplier to find the perfect position
  }
  
  if(site.selection == "All")
  {
    plotData <- subset(qw.data$PlotTable,
                       PARM_CD==(plotparm) &
                         MEDIUM_CD %in% c("OAQ","OA"))
    
    
  }else {
    plotData <- subset(qw.data$PlotTable,SITE_NO %in% (site.selection) & 
                         PARM_CD==(plotparm) &
                         MEDIUM_CD %in% c("OAQ","OA"))
  }
  
  #plotData$status <- NA
  plotData$status[plotData$DQI_CD == "R"] <- "DQI = R"
  plotData$status[plotData$DQI_CD %in% c("I","S","P")] <- "DQI = I, S, or P"
  plotData <- plotData[plotData$REMARK_CD != "<",]
  
  p1 <- ggplot(data=plotData,aes(x=PARM_NM,y=RESULT_VA,color=status,group=status)) +
    geom_boxplot() + geom_point(size=3,position=position_dodge(width=0.75)) +
    ylab("Concentration") + xlab("Parameter") + ggtitle("Comparison of Blank Detections") +
    theme_bw() + theme(axis.text.x = element_text(angle = 90),
                       panel.grid.minor = element_line())
  p1 <- p1 + stat_summary(fun.data = give.n, 
                          geom = "text", 
                          fun.y = median,
                          color="black",
                          position=position_dodge(width=0.75))
  p1 <- p1 + scale_color_manual("DQI Status",values = dqi.colors)
  if ( facet == "Facet")
  {
          p1 <- p1 + facet_wrap(~ STATION_NM, nrow = 1, scales=scales) 
  } else{}
  
  if(nrow(plotData) == 0)
  {
    p1 <- ggplot() + 
      geom_text(aes(x=1,y=1,label="No detections")) + ggtitle("Comparison of Blank Detections") +
      theme_bw() +
      theme(axis.text.x = element_blank(),
            axis.text.y = element_blank())
  }
  
  if(printPlot)
  {
    print(p1)
  }else{}
  return(p1)
}
USGS-R/WQ-Review documentation built on Nov. 12, 2019, 9:51 a.m.