R/plot_exploitation_rate.R

Defines functions plot_exploitation_rate

Documented in plot_exploitation_rate

#' Plot the exploitation rate (F0) by country and scenario
#'
#' @param ps A plot setup object as output by [setup_mse_plot_objects()]
#' @param ci A vector of length two of the lower and upper credible interval values.
#'
#' @return A [ggplot2::ggplot()] object
#'
#' @export
plot_exploitation_rate <- function(ps = NULL,
                                   ci = c(0.05, 0.95)){

  stopifnot(!is.null(ps))
  stopifnot(!is.null(ci))

  f0 <- ps$mse_quant$f0_quant
  f0$year <- as.numeric(f0$year)
  stopifnot("0.5" %in% names(f0))
  stopifnot(is.numeric(ci))
  stopifnot(all(ci %in% names(f0)))
  stopifnot(length(ci) == 2)

  f0_can <- f0 %>%
    filter(country == "Canada")
  f0_us <- f0 %>%
    filter(country == "US")

  ci <- as.character(ci) %>% map(~{sym(.x)})

  g <- ggplot(f0_can, aes(x = year, y = `0.5`)) +
    geom_line(color = "red") +
    geom_line(aes(y = f0_us$`0.5`), color = "blue") +
    theme_classic() +
    scale_y_continuous(name ="Exploitation rate") +
    facet_wrap(~scenario) +
    theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5)) +
    geom_ribbon(aes(ymin = !!ci[[1]], ymax = !!ci[[2]]),
                fill = alpha("red", alpha = 0.2),
                linetype = 0) +
    geom_ribbon(aes(ymin = f0_us[[ci[[1]]]], ymax = f0_us[[ci[[2]]]]),
                fill = alpha("blue", alpha = 0.2),
                linetype = 0)
  g
}
pacific-hake/pacifichakemse documentation built on June 11, 2024, 4:07 a.m.