R/PlotReopensByDate.R

PlotReopensByDate <- structure(
function # Create a plot of the number of reopens by date

  ##############################################################################
  # File:  PlotReopensByDate.R
  ##author<<  Steven H. Ranney
  ## Contact: \email{Steven.Ranney@gmail.com}
  # Created: 01/06/15  
  # Last Edited: 01/06/15 by SHR
  ##description<< This function plots the number of reopens and produces a box 
  ## plot of those values as a function of \code{date} level. 
  #
  # TODO: add RData for example
  # TODO: add testing section
  ###############################################################################
  
  (dF, ##<< The dataFrame for which to plot resolution time by date level.
  beginDate = min(dF$assignedDate), ##<< A character that includes the beginning
                                        ## date of interest.  Value \emph{must} 
                                        ## be in the form \code{"yyyy-mm-dd"}, 
                                        ## e.g., "2014-01-01". The function will 
                                        ## default to the earliest date in the data frame.
  endDate = max(dF$assignedDate), ##<< A character that includes the end date 
                                    ## of interest.  Value \emph{must} be in the 
                                    ## form \code{"yyyy-mm-dd"}, e.g., "2014-01-01".
                                    ## The function will default to the latest
                                    ## date in the data frame.                                      
  ... ##<< Arguments to be passed to other functions.  Specifically, this can take 
      ## the form of identifying which \code{date} value is of interest.  For example
      ## the sample datasets provided all had either at least \code{assigned_at} 
      ## or \code{created_at} values.  The \code{\link{AssignDateAndDay}} can used
      ## either of these values as the date of interest. 
  ){

  dF <- AssignDateAndDay(dF, ...)

  #Eliminate rows where there is no date assigned
  dF <- dF[which(!is.na(dF$assignedDate)), ]
  
  #Handling 'nan.0' values
  dF$reopens[dF$reopens == "nan.0"] <- NA
  dF$reopens <- as.numeric(dF$reopens)
  dF$value <- dF$reopens
 
  #Create dataframe of just dates and resolution times
  tmp <- dF[, c("assignedDate", "value")]
  
  #Create sequence of dates to 'fill in gaps
  tmpDate <- data.frame(seq(min(tmp$assignedDate), max(tmp$assignedDate), 1))
  names(tmpDate) <- "assignedDate"
  
  tmp <- merge(tmp, tmpDate, by = "assignedDate", all = T)
  tmp <- tmp[order(tmp$assignedDate), ]
  tmp <- tmp[which(!is.na(tmp$value)), ]
  
  if(length(which(is.na(dF$value))) == nrow(dF)){
    stop(print("Data frame has no reopen data to display"))
    } else {
  
  ##details<< This functions converts values that are \code{nan.0} to \code{NA}.
  
  ##details<< Boxes in the plot that is produced from this function can help
  ## managers understand the number of reopens by date.  If the 
  ## notches in a box overlap the heavy bar (i.e., the median resolution time)
  ## in an adjacent box, then the resolution time for that priority level is \emph{not}
  ## significantly different than the resolution time in the adjacent plot.
  
  ##details<< The red dots on a plot indicate outliers in the data set.  In the 
  ## \code{\link{geom_boxplot}} documentation, outliers are defined as data points 
  ## fall outside of \emph{1.5 * IQR} where \emph{IQR} stands for the "Inter-Quartile
  ## Range" of the data.
 
  ylabel <- "Number of Reopens"  
  xlabel <- "Date"
  
  ##details<< Plots are generated by the function \code{\link{ggplot}}
  g <- ggplot(tmp, aes(x = assignedDate, y = (value), 
                       group = assignedDate))
  
  p1 <- g + geom_boxplot(mapping = NULL, data = tmp, stat = "boxplot", 
                    outlier.colour = "red", outlier.shape = 16, 
                    outlier.size = 2, notch = TRUE) + 
      scale_x_date(limits = c(as.Date(beginDate), as.Date(endDate))) +     
      labs(ylab(ylabel)) + labs(xlab(xlabel)) + labs(ggtitle("Resolution time"))
        
    
    } #end else

  return(p1)

})
stevenranney/ispiranteRanney documentation built on May 30, 2019, 4:46 p.m.