R/PlotResolutionTimeByDate.R

PlotResolutionTimeByDate <- structure(
function # Create a plot of the resolution time by date

  ##############################################################################
  # File:  PlotResolutionTimeByDate.R
  ##author<<  Steven H. Ranney
  ## Contact: \email{Steven.Ranney@gmail.com}
  # Created: 12/30/14  
  # Last Edited: 12/30/14 by SHR
  ##description<< This function plots the resolution time (in hours) 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.
  resTime = "first", ##<< A character vector that indicates which resolution time
                    ## to plot.  Other values include \code{"full"}.
  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$first_resolution_time_in_minutes_within_business_hours[dF$first_resolution_time_in_minutes_within_business_hours == "nan.0"] <- NA
  dF$full_resolution_time_in_minutes_within_business_hours[dF$full_resolution_time_in_minutes_within_business_hours == "nan.0"] <- NA
 
  if(resTime == "first") dF$value <- as.numeric(dF$first_resolution_time_in_minutes_within_business_hours)
  if(resTime == "full") dF$value <- as.numeric(dF$full_resolution_time_in_minutes_within_business_hours)
 
  #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), ]
  
  if(length(which(is.na(dF$value))) == nrow(dF)){
    stop(print("Data frame has no data to display"))
    } else {
    
  ##details<< Resolution time in all sample dataFrames is \code{factor}; this function
  ## converts apparently numeric values to \code{as.numeric}.
  
  ##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 resolution time 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 <- NA
  if(resTime == "first") ylabel <- "First resolution (hours)"
  if(resTime == "full") ylabel <- "Full resolution (hours)"
  
  xlabel <- "Date"
  
  ##details<< Plots are generated by the function \code{\link{ggplot}}
  g <- ggplot(tmp, aes(x = assignedDate, y = (value/60), 
                       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.