From 018421ff29a851205364a2d90ce6e9dd4ee3bc18 Mon Sep 17 00:00:00 2001 From: Sam Schiano <125507018+Schiano-NOAA@users.noreply.github.com> Date: Wed, 8 Apr 2026 15:46:30 -0400 Subject: [PATCH 1/3] [Fix] plot_indices (#221) * initial commit of new obs v pred plot * adjust process data so it converts all indexed data to character to function even when input values are numeric per #212 * update plot indices to use new plot_obsvpred function and adjust based on needs * update documentation for package * adjust plot_indices where nfleet = 1 and update test to remove new file produced * add missing dependency --- R/plot_indices.R | 4 ++-- R/process_data.R | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/R/plot_indices.R b/R/plot_indices.R index bd034fef..6a9e5838 100644 --- a/R/plot_indices.R +++ b/R/plot_indices.R @@ -63,7 +63,7 @@ plot_indices <- function( prepared_data <- prepared_data |> dplyr::filter(fleet %in% focus) } - + processed_data <- process_data( dat = prepared_data, group = group, @@ -109,7 +109,7 @@ plot_indices <- function( facet_formula <- stats::reformulate(facet) plt <- plt + ggplot2::facet_wrap(facet_formula, scales = "free") } - + ### Make RDA ---- if (make_rda) { # Obtain relevant key quantities for captions/alt text diff --git a/R/process_data.R b/R/process_data.R index 46907a58..662c7f17 100644 --- a/R/process_data.R +++ b/R/process_data.R @@ -354,7 +354,7 @@ process_data <- function( group <- NULL } } - + # Ensure that index_variables -- group or facets are non-numeric to be plotted accurately data <- data |> dplyr::mutate( From ecd48611de8725d0beb7880866119ee95af77b3b Mon Sep 17 00:00:00 2001 From: sbreitbart-NOAA Date: Thu, 9 Apr 2026 17:15:35 -0400 Subject: [PATCH 2/3] Address https://github.com/nmfs-ost/stockplotr/issues/218 by scaling uncertainty intervals if relative = TRUE for B and SB plots --- R/plot_biomass.R | 5 ++++- R/plot_spawning_biomass.R | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/R/plot_biomass.R b/R/plot_biomass.R index 8740f4dd..e03a4807 100644 --- a/R/plot_biomass.R +++ b/R/plot_biomass.R @@ -121,7 +121,10 @@ plot_biomass <- function( } if (is.na(ref_line_val)) cli::cli_abort("Reference value not found. Cannot plot relative values.") prepared_data <- prepared_data |> - dplyr::mutate(estimate = estimate / ref_line_val) + dplyr::mutate(estimate = estimate / ref_line_val, + estimate_lower = estimate_lower / ref_line_val, + estimate_upper = estimate_upper / ref_line_val + ) } plt <- plot_timeseries( diff --git a/R/plot_spawning_biomass.R b/R/plot_spawning_biomass.R index a4715ba0..5ea46f48 100644 --- a/R/plot_spawning_biomass.R +++ b/R/plot_spawning_biomass.R @@ -188,7 +188,10 @@ plot_spawning_biomass <- function( } if (is.na(ref_line_val)) cli::cli_abort("Reference value not found. Cannot plot relative values.") plot_data <- plot_data |> - dplyr::mutate(estimate = estimate / ref_line_val) + dplyr::mutate(estimate = estimate / ref_line_val, + estimate_lower = estimate_lower / ref_line_val, + estimate_upper = estimate_upper / ref_line_val + ) } plt <- plot_timeseries( From dfbda7f10f798c4a23151316073c949675a8e059 Mon Sep 17 00:00:00 2001 From: Sam Schiano <125507018+Schiano-NOAA@users.noreply.github.com> Date: Thu, 16 Apr 2026 16:19:06 -0400 Subject: [PATCH 3/3] remove previous calcs of relative and add if statement instead --- R/plot_biomass.R | 81 ++++++++++++++++----------------------- R/plot_spawning_biomass.R | 73 ++++++++++++++--------------------- 2 files changed, 62 insertions(+), 92 deletions(-) diff --git a/R/plot_biomass.R b/R/plot_biomass.R index e03a4807..6d7cc096 100644 --- a/R/plot_biomass.R +++ b/R/plot_biomass.R @@ -75,56 +75,41 @@ plot_biomass <- function( } # Filter data for spawning biomass - prepared_data <- filter_data( - dat = dat, - label_name = "^biomass$", - geom = geom, - group = group, - facet = facet, - module = module, - scale_amount = scale_amount, - interactive = interactive - ) - - # check if all 3 are present and subset for one or two - if (length(unique(prepared_data$label)) > 1 & any(grepl("biomass$", unique(prepared_data$label)))) { - # cli::cli_alert_info("> 1 label name. Selecting total biomass only.") - prepared_data <- prepared_data |> - dplyr::filter( - grepl("biomass$", label) - ) - } - - # Process data for indexing/grouping - # TODO: check and add into process_data step to summarize when theres >1 label - processing <- process_data( - prepared_data, - group, - facet - ) - - # variable <- processing[[1]] - prepared_data <- processing[[1]] - group <- processing[[2]] - if (!is.null(processing[[3]])) facet <- processing[[3]] - - # Calculate estimate if relative if (relative) { - if (!is.null(names(ref_line))) { - ref_line_val <- ref_line[[1]] - # ref_line <- names(ref_line) - } else { - ref_line_val <- calculate_reference_point( - dat = rp_dat, - reference_name = glue::glue("^biomass_", ref_line) - ) / scale_amount + + } else { + prepared_data <- filter_data( + dat = dat, + label_name = "^biomass$", + geom = geom, + group = group, + facet = facet, + module = module, + scale_amount = scale_amount, + interactive = interactive + ) + + # check if all 3 are present and subset for one or two + if (length(unique(prepared_data$label)) > 1 & any(grepl("biomass$", unique(prepared_data$label)))) { + # cli::cli_alert_info("> 1 label name. Selecting total biomass only.") + prepared_data <- prepared_data |> + dplyr::filter( + grepl("biomass$", label) + ) } - if (is.na(ref_line_val)) cli::cli_abort("Reference value not found. Cannot plot relative values.") - prepared_data <- prepared_data |> - dplyr::mutate(estimate = estimate / ref_line_val, - estimate_lower = estimate_lower / ref_line_val, - estimate_upper = estimate_upper / ref_line_val - ) + + # Process data for indexing/grouping + # TODO: check and add into process_data step to summarize when theres >1 label + processing <- process_data( + prepared_data, + group, + facet + ) + + # variable <- processing[[1]] + prepared_data <- processing[[1]] + group <- processing[[2]] + if (!is.null(processing[[3]])) facet <- processing[[3]] } plt <- plot_timeseries( diff --git a/R/plot_spawning_biomass.R b/R/plot_spawning_biomass.R index 5ea46f48..904df894 100644 --- a/R/plot_spawning_biomass.R +++ b/R/plot_spawning_biomass.R @@ -146,52 +146,37 @@ plot_spawning_biomass <- function( } # Filter data for spawning biomass - prepared_data <- filter_data( - dat = dat, - label_name = "^spawning_biomass$", - geom = geom, - era = era, - group = group, - facet = facet, - module = module, - scale_amount = scale_amount, - interactive = interactive - ) - - # process the data for grouping - processing <- process_data( - dat = prepared_data, - group = group, - facet = facet, - method = "sum" - ) - # variable <- processing[[1]] - plot_data <- processing[[1]] - group <- processing[[2]] - if (!is.null(processing[[3]])) facet <- processing[[3]] - - # Override grouping variable when there is only NA's - if (!is.null(group)) { - if (group %notin% colnames(plot_data)) group <- NULL - } - - # Calculate estimate if relative if (relative) { - if (!is.null(names(ref_line))) { - ref_line_val <- ref_line[[1]] - # ref_line <- names(ref_line) - } else { - ref_line_val <- calculate_reference_point( - dat = rp_dat, - reference_name = glue::glue("spawning_biomass_", ref_line) - ) / scale_amount + + } else { + prepared_data <- filter_data( + dat = dat, + label_name = "^spawning_biomass$", + geom = geom, + era = era, + group = group, + facet = facet, + module = module, + scale_amount = scale_amount, + interactive = interactive + ) + + # process the data for grouping + processing <- process_data( + dat = prepared_data, + group = group, + facet = facet, + method = "sum" + ) + # variable <- processing[[1]] + plot_data <- processing[[1]] + group <- processing[[2]] + if (!is.null(processing[[3]])) facet <- processing[[3]] + + # Override grouping variable when there is only NA's + if (!is.null(group)) { + if (group %notin% colnames(plot_data)) group <- NULL } - if (is.na(ref_line_val)) cli::cli_abort("Reference value not found. Cannot plot relative values.") - plot_data <- plot_data |> - dplyr::mutate(estimate = estimate / ref_line_val, - estimate_lower = estimate_lower / ref_line_val, - estimate_upper = estimate_upper / ref_line_val - ) } plt <- plot_timeseries(