R/plotTimeSeries.R

Defines functions plotTimeSeries

Documented in plotTimeSeries

#' @title Time Series Plot
#'
#' @description Plot window counts, trap estimates and STADEM estimates of total escapement through time.
#'
#' @author Kevin See
#'
#' @param stadem_mod jagsUI object returned from running \code{runSTADEMmodel}.
#' @param weeklyData weekly data compiled by \code{compileGRAdata}, with the name \code{weeklyData} in the resulting list.
#'
#' @import dplyr ggplot2
#' @return NULL
#' @export

plotTimeSeries = function(stadem_mod = NULL,
                          weeklyData = NULL) {

  stopifnot(!is.null(stadem_mod))
  stopifnot(!is.null(weeklyData))


  plot_df = stadem_mod$summary[grep('^X.all', rownames(stadem_mod$summary)),] %>%
    as_tibble(rownames = 'var') %>%
    mutate(week = as.integer(str_extract(var, "[0-9]+")),
           param = str_extract_all(var, "[:alpha:]+", simplify = T)[,3],
           param = ifelse(param == '', 'all', param)) %>%
    select(var, param, week, everything()) %>%
    left_join(weeklyData %>%
                rename(week = week_num)) %>%
    # adjust window count for night-time passage
    mutate(win_cnt_adj = win_cnt / (day_tags / tot_tags)) %>%
    mutate_at(vars(win_cnt_adj,
                   trap_est),
              list(~ if_else(. == Inf,
                             as.numeric(NA),
                             .))) %>%
    # mark trap estimate as NA if not considered valid
    mutate(trap_est_valid = if_else(trap_valid,
                                    trap_est,
                                    as.numeric(NA)))


  plot_df %>%
    filter(param == 'all') %>%
    ggplot(aes(x = Start_Date)) +
    geom_ribbon(aes(ymin = `2.5%`,
                    ymax = `97.5%`),
                alpha = 0.2) +
    geom_line(aes(y = win_cnt_adj,
                  color = 'Window (adj)')) +
    geom_point(aes(y = win_cnt_adj,
                   color = 'Window (adj)')) +
    geom_line(aes(y = win_cnt,
                  color = 'Window (raw)')) +
    geom_point(aes(y = win_cnt,
                   color = 'Window (raw)')) +
    geom_line(aes(y = trap_est_valid,
                  color = 'Trap')) +
    geom_point(aes(y = trap_est,
                   shape = trap_valid,
                   color = 'Trap')) +
    scale_shape_manual(values = c('TRUE' = 19,
                                  'FALSE' = 1),
                       name = 'Valid Est.') +
    geom_line(aes(y = `50%`,
                  color = 'Model')) +
    geom_point(aes(y = `50%`,
                   color = 'Model')) +
    scale_color_manual(values = c('Model' = 'black',
                                  'Window (raw)' = 'lightblue',
                                  'Window (adj)' = 'blue',
                                  'Trap' = 'red')) +
    theme(legend.position = 'bottom') +
    labs(x = 'Date',
         y = 'Estimate',
         color = 'Source',
         title = 'Total Escapement')

}
KevinSee/STADEM documentation built on June 20, 2021, 11:13 p.m.