From e912c0a6fa2603856bfbbfc1b8102fc2ed6facaa Mon Sep 17 00:00:00 2001 From: Sam Schiano <125507018+Schiano-NOAA@users.noreply.github.com> Date: Tue, 21 Jul 2026 15:50:50 -0400 Subject: [PATCH 1/2] add note for where editing will occur --- R/plot_spawning_biomass.R | 1 + 1 file changed, 1 insertion(+) diff --git a/R/plot_spawning_biomass.R b/R/plot_spawning_biomass.R index 33a67fca..486a5d09 100644 --- a/R/plot_spawning_biomass.R +++ b/R/plot_spawning_biomass.R @@ -228,6 +228,7 @@ plot_spawning_biomass <- function( ggplot2::geom_point(ggplot2::aes(x = min_year - 1, y = ref_point)) + # should I keep -1 or set as first year? theme_noaa() } else { + # add apply/purrr/or for loop for reference lines -- not just the first anymore final <- reference_line( plot = plt, dat = rp_dat, From 76065c7657ba4a2a22ca636c1f3708b7be9a0092 Mon Sep 17 00:00:00 2001 From: Sam Schiano <125507018+Schiano-NOAA@users.noreply.github.com> Date: Fri, 24 Jul 2026 13:42:50 -0400 Subject: [PATCH 2/2] initial creation of plot_discard and remove problem duplicate bam naming --- R/plot_discard.R | 147 +++++++++++++++++++++++++++++++ inst/resources/bam_var_names.csv | 4 - 2 files changed, 147 insertions(+), 4 deletions(-) create mode 100644 R/plot_discard.R diff --git a/R/plot_discard.R b/R/plot_discard.R new file mode 100644 index 00000000..4637e634 --- /dev/null +++ b/R/plot_discard.R @@ -0,0 +1,147 @@ +#' Plot discards +#' +#' Plot discards as a line graph. +#' +#' @inheritParams plot_spawning_biomass +#' +#' @returns A ggplot showing discards over time, usually by fleet. +#' +#' @details The input is from an assessment model output file +#' translated to a standardized output (\link[stockplotr]{convert_output}). +#' There are options to return a `ggplot2` object or export an .rda object +#' containing associated caption and alternative text for the figure. +#' +#' @note +#' All plotting functions automatically recognize indexing variables and will +#' use them in groupings and/or facetting. @seealso [process_data()]. +#' +#' @seealso [convert_output()], [plot_timeseries()], [calculate_reference_point()], [reference_line()], [filter_data()], [process_data()], [export_kqs()], [insert_kqs()], [create_rda()] +#' +#' @export +#' +#' @examples +#' plot_discard( +#' dat = example_data +#' +#' ) +plot_discard <- function( + dat, + group = NULL, + facet = NULL, + unit_label = "mt", + era = NULL, + lbs = FALSE, + scale_amount = 1, + interactive = TRUE, + module = NULL, + make_rda = FALSE, + figures_dir = getwd() +) { + # this assumes that the previous units were metric tons + if (lbs && unit_label %notin% c("lbs", "pounds", "lb")) { + cli::cli_alert_info("Unit label was not changed. Setting unit_label to 'lbs'.") + unit_label <- "lbs" + } + + discard_label <- label_magnitude( + label = "Discard", + unit_label = unit_label, + scale_amount = dplyr::if_else( + lbs, + ifelse(unit_label %in% c("mt", "mts", "metric tons", "metric ton"), 1000, 1) * scale_amount, + scale_amount + ), + legend = TRUE + ) + + # Filter data for discards + prepared_data <- filter_data( + dat = dat, + label_name = "^discard$|discard_weight", + geom = "line", + era = era, + group = group, + facet = facet, + module = module, + scale_amount = scale_amount, + interactive = interactive + ) + + # Process the data to remove unneccessary columns and information + p_dat <- process_data( + prepared_data, + group = group, + facet = facet, + lbs = lbs + ) + discards <- p_dat[[1]] + group <- p_dat[[2]] + facet <- p_dat[[3]] + + # make the plot + plt <- plot_timeseries( + discards, + ylab = discard_label, + group = group, + facet = if (length(facet) > 0) facet else NULL#, + # ... + ) + + ### Make RDA ---- + if (make_rda) { + if (relative) { + rel.ssb.min <- calc_kqs(returned_kq = "rel.ssb.min", + final = final) + + + rel.ssb.max <- calc_kqs(returned_kq = "rel.ssb.max", + final = final) + + # calculate & export key quantities + export_kqs(rel.ssb.min, rel.ssb.max) + + # Add key quantities to captions/alt text + insert_kqs(rel.ssb.min, rel.ssb.max) + } else { + ssb.min <- min(plot_data$estimate) |> round(digits = 3) + ssb.max <- max(plot_data$estimate) |> round(digits = 3) + + export_kqs(ssb.min, ssb.max) + insert_kqs(ssb.min, ssb.max) + } + + # Obtain relevant key quantities for captions/alt text + ssb.ref.pt <- as.character(ref_line) + ssb.units <- as.character(unit_label) + ssb.start.year <- min(plot_data$year) + ssb.end.year <- max(plot_data$year) + + # calculate & export key quantities + export_kqs( + ssb.ref.pt, + ssb.units, + ssb.start.year, + ssb.end.year + ) + + # Add key quantities to captions/alt text + insert_kqs( + ssb.ref.pt, + ssb.units, + ssb.start.year, + ssb.end.year + ) + + create_rda( + object = final, + topic_label = "discards", + fig_or_table = "figure", + dat = rp_dat, + dir = figures_dir, + scale_amount = scale_amount, + unit_label = unit_label + ) + } + # Output final plot + plt + theme_noaa() +} \ No newline at end of file diff --git a/inst/resources/bam_var_names.csv b/inst/resources/bam_var_names.csv index a7178d5e..e809d3f0 100644 --- a/inst/resources/bam_var_names.csv +++ b/inst/resources/bam_var_names.csv @@ -244,10 +244,6 @@ LD.pr.tseries,L.klb,landings_weight LD.pr.tseries,L.knum,landings_numbers LD.pr.tseries,D.klb,discard_weight LD.pr.tseries,D.knum,discard_numbers -LD.pr.tseries,l.klb,landings_weight -LD.pr.tseries,l.knum,landings_numbers -LD.pr.tseries,d.klb,discard_weight -LD.pr.tseries,d.knum,discard_numbers a.series,length,length a.series,length.cv,length_cv a.series,length.sd,length_sd