R/PlotCallVolumeByDate.R

PlotCallVolumeByDate <- 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.
  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)), ]
    
  #aggregate call volume by date
  volData <- aggregate(dF$dayOfWeek, by = list(dF$assignedDate), length)
  names(volData) <- c("assignedDate", "totalCalls")
  volData <- volData[order(volData$assignedDate), ]
  
  #Create sequence of dates to 'fill in gaps
  tmpDate <- data.frame(seq(min(dF$assignedDate), max(dF$assignedDate), 1))
  names(tmpDate) <- "assignedDate"
  
  volData <- merge(tmpDate, volData, by = "assignedDate", all = T)
  volData$totalCalls[is.na(volData$totalCalls)] <- 0
  
  ##details<< This functions converts dates that are not represented in the data
  ## frame to \code{0} values.  However, \code{0} value dates are only 'filled in;'
  ## that is, the function only fills in dates.  It does not extend the date field.
  ## For example, if the data frame has dates \code{2014-01-01}, \code{2014-01-03}, 
  ## and \code{2014-01-05}, this function fills in the date gaps.
  
  ylabel <- "Number of calls/day"  
  xlabel <- "Date"
  
  ##details<< Plots are generated by the function \code{\link{ggplot}}
  g <- ggplot(volData, aes(x = assignedDate, y = (totalCalls)))
  
  p1 <- g + geom_line(colour = "navyblue", lwd = 1.01) +
      scale_x_date(limits = c(as.Date(beginDate), as.Date(endDate))) +     
      labs(ylab(ylabel)) + labs(xlab(xlabel)) + labs(ggtitle("Resolution time"))
  
  return(p1) 

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