diff --git a/NAMESPACE b/NAMESPACE index ca472f0..b1846f7 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -4,4 +4,5 @@ export(add_theme) export(plot_landings) export(plot_recruitment) export(plot_spawning_biomass) +export(plot_total_biomass) export(table_indices) diff --git a/R/plot_biomass.R b/R/plot_biomass.R new file mode 100644 index 0000000..a6f8ef0 --- /dev/null +++ b/R/plot_biomass.R @@ -0,0 +1,120 @@ +#' Plot Total Biomass +#' +#' @inheritParamsplot_recruitment +#' @param show_warnings Option to suppress warnings +#' @param units units for biomass +#' @param scaled TRUE/FALSE; indicate whether the output values for biomass and recruitment are scaled +#' @param scale_amount indicate the exact amount of scale (i.e. 1000) +#' @param ref_line choose with reference point to plot a reference line and use +#' in relative totb calculations +#' @param end_year input the end year of the stock assessment data (not including +#' projections). This parameter will be deprecated once the output converter is fully developed. +#' @param relative Plot relative total biomass. Ref line indicates which reference point to use +#' +#' @return Plot total biomass from a stock assessment model as found in a NOAA +#' stock assessment report. Units of total biomass can either be manually added +#' or will be extracted from the provided file if possible. In later releases, model will not +#' @export +#' +plot_total_biomass <- function(dat, + model = "standard", + show_warnings = FALSE, + units = NULL, + scaled = FALSE, + scale_amount = 1000, + ref_line = c("target", "MSY", "msy", "unfished"), + end_year = NULL, + relative = FALSE +){ + + if(length(ref_line)>1){ + ref_line = "target" + } else { + ref_line <- match.arg(ref_line, several.ok = FALSE) + } + + # check units + if(!is.null(units)){ + bu <- units + } else { + bu <- "metric tons" + } + + if(model == "standard"){ + output <- read.csv(dat) + totb <- output |> + dplyr::filter(label == "biomass", + module_name == "DERIVED_QUANTITIES" | module_name == "t.series") |> # SS3 and BAM target module names + dplyr::mutate(estimate = as.numeric(estimate), + year = as.numeric(year)) + if (is.null(end_year)){ + endyr <- max(totb$year) + } + # Select value for reference line and label + # update the target option later + if (any(grepl("target", output$label))) { + ref_line_val <- as.numeric(output[grep("(?=.*biomass)(?=.*target)", output$label, perl = TRUE),]$estimate) + ref_line_label <- "target" + if (scaled) { + ann_add <- ggplot2::annotate("text", x = endyr + 0.05, y=ref_line_val, label = bquote(B[target])) # this might need to change + } else { + ann_add <- ggplot2::annotate("text", x = endyr + 0.05, y=ref_line_val/1000, label = bquote(B[target])) + } + } else if (ref_line == "MSY" | ref_line == "msy") { + ref_line_val <- as.numeric(output[grep("(^biomass_msy)", output$label, perl = TRUE),]$estimate) + ref_line_label <- "MSY" + if (scaled) { + ann_add <- ggplot2::annotate("text", x = endyr + 0.05, y=ref_line_val, label = bquote(B[ref_line])) + } else { + ann_add <- ggplot2::annotate("text", x = endyr + 0.05, y=ref_line_val/1000, label = bquote(B[MSY])) + } + } else if (ref_line == "unfished") { + ref_line_val <- as.numeric(output[grep("(^biomass_unfished)", output$label, perl = TRUE),]$estimate) + ref_line_label <- "unfished" + if (scaled) { + ann_add <- ggplot2::annotate("text", x = endyr + 0.05, y=ref_line_val, label = bquote(B[unfished])) + } else { + ann_add <- ggplot2::annotate("text", x = endyr + 0.05, y=ref_line_val/1000, label = bquote(B[unfished])) + } + } + # Choose number of breaks for x-axis + x_n_breaks <- round(length(subset(totb, year<=endyr)$year)/10) + if (x_n_breaks <= 5) { + x_n_breaks <- round(length(subset(totb, year<=endyr)$year)/5) + } + if (relative) { + # plot relative TOTB + plt <- ggplot2::ggplot(data = subset(totb, year<=endyr)) + + ggplot2::geom_line(ggplot2::aes(x = year, y = estimate/ref_line_val), linewidth = 1) + + # ggplot2::geom_ribbon(ggplot2::aes(x = year, ymin = (value/ref_line_val - stddev/ref_line_val), ymax = (value/ref_line_val + stddev/ref_line_val)), colour = "grey", alpha = 0.3) + + ggplot2::geom_hline(yintercept = ref_line_val/ref_line_val, linetype = 2) + + ggplot2::labs(x = "Year", + y = paste("Biomass (", bu, ")", sep = "")) + + ggplot2::scale_x_continuous(n.breaks = x_n_breaks, + guide = ggplot2::guide_axis(minor.ticks = TRUE)) + } else { + if(scaled){ + plt <- ggplot2::ggplot(data = totb) + + ggplot2::geom_line(ggplot2::aes(x = year, y = estimate), linewidth = 1) + + ggplot2::geom_hline(yintercept = ref_line_val, linetype = 2) + + ggplot2::labs(x = "Year", + y = paste("Biomass (", bu, ")", sep = "")) + + ggplot2::scale_x_continuous(n.breaks = x_n_breaks, + guide = ggplot2::guide_axis(minor.ticks = TRUE)) + plt <- plt + ann_add + } else { + plt <- ggplot2::ggplot(data = totb) + + ggplot2::geom_line(ggplot2::aes(x = year, y = estimate/1000), linewidth = 1) + + # ggplot2::geom_ribbon(ggplot2::aes(x = year, ymin = (value/1000 - stddev/1000), ymax = (value/1000 + stddev/1000)), colour = "grey", alpha = 0.3) + + ggplot2::geom_hline(yintercept = ref_line_val/1000, linetype = 2) + + ggplot2::labs(x = "Year", + y = paste("Biomass (", bu, ")", sep = "")) + + ggplot2::scale_x_continuous(n.breaks = x_n_breaks, + guide = ggplot2::guide_axis(minor.ticks = TRUE)) + plt <- plt + ann_add + } + } + plt_fin <- add_theme(plt) + } + return(plt_fin) +} diff --git a/R/plot_recruitment.R b/R/plot_recruitment.R index 7cdedfa..a8aeedf 100644 --- a/R/plot_recruitment.R +++ b/R/plot_recruitment.R @@ -1,10 +1,6 @@ #' Plot Recruitment #' -#' @param dat Stock assessment output file containing estimates parameters and -#' other associated metrics. -#' @param model Acryonym of the stock assessment model that produced the output file. -#' Currently, only stock synthesis (SS3) and Beaufort Assessment Model (BAM) -#' are options for plotting spawning biomass. +#' @inheritParams plot_recruitment #' @param params Print/export the parameters of the stock recruitment function? #' @param params_only Only export the stock recruitment function or both the parameters and the plot(s)? #' @param units If units are not available in the output file, in metric tons, diff --git a/man/plot_total_biomass.Rd b/man/plot_total_biomass.Rd new file mode 100644 index 0000000..b8cc348 --- /dev/null +++ b/man/plot_total_biomass.Rd @@ -0,0 +1,43 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/plot_biomass.R +\name{plot_total_biomass} +\alias{plot_total_biomass} +\title{Plot Total Biomass} +\usage{ +plot_total_biomass( + dat, + model = "standard", + show_warnings = FALSE, + units = NULL, + scaled = FALSE, + scale_amount = 1000, + ref_line = c("target", "MSY", "msy", "unfished"), + end_year = NULL, + relative = FALSE +) +} +\arguments{ +\item{show_warnings}{Option to suppress warnings} + +\item{units}{units for biomass} + +\item{scaled}{TRUE/FALSE; indicate whether the output values for biomass and recruitment are scaled} + +\item{scale_amount}{indicate the exact amount of scale (i.e. 1000)} + +\item{ref_line}{choose with reference point to plot a reference line and use +in relative totb calculations} + +\item{end_year}{input the end year of the stock assessment data (not including +projections). This parameter will be deprecated once the output converter is fully developed.} + +\item{relative}{Plot relative total biomass. Ref line indicates which reference point to use} +} +\value{ +Plot total biomass from a stock assessment model as found in a NOAA +stock assessment report. Units of total biomass can either be manually added +or will be extracted from the provided file if possible. In later releases, model will not +} +\description{ +Plot Total Biomass +}