From 975e79c3dcb0202ee8b318e7e72d37945c8823fe Mon Sep 17 00:00:00 2001 From: sbreitbart-NOAA Date: Thu, 26 Sep 2024 09:49:11 -0400 Subject: [PATCH 1/5] added t.series as module name to filter by --- R/plot_spawning_biomass.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/plot_spawning_biomass.R b/R/plot_spawning_biomass.R index 2acc7c4..5a31eb3 100644 --- a/R/plot_spawning_biomass.R +++ b/R/plot_spawning_biomass.R @@ -60,7 +60,7 @@ plot_spawning_biomass <- function(dat, output <- read.csv(dat) sb <- output |> dplyr::filter(label == "spawning_biomass", - module_name == "DERIVED_QUANTITIES" | module_name == "") |> # SS3 and BAM target module names + 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)){ From f37a47b15f4fdb6f45568b582d37a03fa2543128 Mon Sep 17 00:00:00 2001 From: sbreitbart-NOAA Date: Thu, 26 Sep 2024 12:16:57 -0400 Subject: [PATCH 2/5] created function that generates a total biomass time series plot --- R/plot_biomass.R | 121 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 R/plot_biomass.R diff --git a/R/plot_biomass.R b/R/plot_biomass.R new file mode 100644 index 0000000..16ed5a4 --- /dev/null +++ b/R/plot_biomass.R @@ -0,0 +1,121 @@ +#' Plot Total Biomass +#' +#' @template dat +#' @template model +#' @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) +} From 060525c4083f350e2dd7b42092c155382341cc3f Mon Sep 17 00:00:00 2001 From: Sam Schiano <125507018+Schiano-NOAA@users.noreply.github.com> Date: Fri, 27 Sep 2024 13:32:36 -0400 Subject: [PATCH 3/5] Update R/plot_biomass.R Change from outdated template call to inhertiedParams --- R/plot_biomass.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/plot_biomass.R b/R/plot_biomass.R index 16ed5a4..fd9231e 100644 --- a/R/plot_biomass.R +++ b/R/plot_biomass.R @@ -1,6 +1,6 @@ #' Plot Total Biomass #' -#' @template dat +#' @inheritParamsplot_recruitment #' @template model #' @param show_warnings Option to suppress warnings #' @param units units for biomass From ec07783b61f203a76ae3cf596aabcf66001a9539 Mon Sep 17 00:00:00 2001 From: Sam Schiano <125507018+Schiano-NOAA@users.noreply.github.com> Date: Fri, 27 Sep 2024 13:32:48 -0400 Subject: [PATCH 4/5] Update R/plot_biomass.R Remove template --- R/plot_biomass.R | 1 - 1 file changed, 1 deletion(-) diff --git a/R/plot_biomass.R b/R/plot_biomass.R index fd9231e..a6f8ef0 100644 --- a/R/plot_biomass.R +++ b/R/plot_biomass.R @@ -1,7 +1,6 @@ #' Plot Total Biomass #' #' @inheritParamsplot_recruitment -#' @template model #' @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 From e3669256dd4cc3f7500f6d2cdd1748b147356601 Mon Sep 17 00:00:00 2001 From: Schiano-NOAA Date: Fri, 27 Sep 2024 13:47:08 -0400 Subject: [PATCH 5/5] Fix issues with failing ghactions --- DESCRIPTION | 2 +- NAMESPACE | 1 + R/plot_recruitment.R | 3 +-- R/plot_spawning_biomass.R | 3 +-- man/plot_total_biomass.Rd | 43 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 47 insertions(+), 5 deletions(-) create mode 100644 man/plot_total_biomass.Rd diff --git a/DESCRIPTION b/DESCRIPTION index 72c89a2..796562e 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -29,5 +29,5 @@ Imports: stringr, tidyr, utils - Remotes: +Remotes: nmfs-fish-tools/nmfspalette diff --git a/NAMESPACE b/NAMESPACE index 898454a..54f19dd 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -3,4 +3,5 @@ export(add_theme) export(plot_recruitment) export(plot_spawning_biomass) +export(plot_total_biomass) export(table_indices) diff --git a/R/plot_recruitment.R b/R/plot_recruitment.R index 8e960ef..4350a1a 100644 --- a/R/plot_recruitment.R +++ b/R/plot_recruitment.R @@ -1,7 +1,6 @@ #' Plot Recruitment #' -#' @template dat -#' @template model +#' @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/R/plot_spawning_biomass.R b/R/plot_spawning_biomass.R index 5a31eb3..fe3c796 100644 --- a/R/plot_spawning_biomass.R +++ b/R/plot_spawning_biomass.R @@ -1,7 +1,6 @@ #' Plot Spawning Biomass #' -#' @template dat -#' @template model +#' @inheritParams plot_recruitment #' @param show_warnings Option to suppress warnings #' @param units If units are not available in the output file or are not the #' default of metric tons, then state the units of spawning biomass applicable 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 +}