Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
70a30f1
feat: convert multiple models into standard stockplotr format
JZahner1 Nov 4, 2026
6821495
fix: :bug: make sure input is a list of models
JZahner1 Nov 4, 2026
0a45764
fix: make sure columns are same type across model platforms
JZahner1 Nov 4, 2026
ae061be
tests: update expected dimensions not that "model" column is always a…
JZahner1 Nov 4, 2026
9ede8eb
tests: add test for convert_output on multiple models.
JZahner1 Nov 4, 2026
b3f7f96
refactor: removed list processing from filter_data
JZahner1 Nov 5, 2026
fa2277e
test: add tests for multimodel data processing
JZahner1 Nov 5, 2026
af39895
refactor: remove dplyr deprecation warnings from conout_ss3
JZahner1 Nov 5, 2026
27178cc
test: add test for plot_abundance_at_age with multiple models
JZahner1 Nov 6, 2026
7d89219
refactor: change reference_line helper functions to work with multipl…
JZahner1 Nov 6, 2026
fd4e0cb
tests: add tests for multiple reference lines
JZahner1 Nov 6, 2026
636e749
tests: use custom directories for wiring RDA files for export-rda tests
JZahner1 Nov 6, 2026
51e33be
Merge branch 'nmfs-ost:main' into feat-multi-model-processing
JZahner1 Nov 6, 2026
e1d6d0d
feat: Add handling for specifying "models" and "fleet_names" paramete…
JZahner1 Nov 9, 2026
719a111
docs: Update roxygen to reflect changes to parameters for multiple mo…
JZahner1 Nov 9, 2026
c0f24d3
docs: Update roxygen to reflect changes to parameters for multiple mo…
JZahner1 Nov 9, 2026
75f5e25
Merge branch 'feat-multi-model-processing' of https://github.com/JZah…
JZahner1 Nov 9, 2026
c3cfea6
fix: Swicth erroneous %>% to |> format
JZahner1 Nov 9, 2026
9506928
feat: :sparkles: plot_retrospective peels of any quantity
JZahner1 Nov 12, 2026
32d1cb3
docs: :memo: add roxygen for plot_retrospective
JZahner1 Nov 12, 2026
9da41ce
refactor: :art: cleanup edge case around data column for plot_recruit…
JZahner1 Nov 12, 2026
0ed29b4
fix: stop from overwriting existing captions/alt-text from base plott…
JZahner1 Nov 12, 2026
5ae9ee5
Merge remote-tracking branch 'upstream/main' into feat-multi-model-pr…
JZahner1 Nov 12, 2026
8c9ba5e
Merge remote-tracking branch 'upstream/main' into feat-multi-model-pr…
JZahner1 Nov 13, 2026
31d3971
mend
JZahner1 Nov 13, 2026
3d85770
Merge remote-tracking branch 'origin/feat-multi-model-processing' int…
JZahner1 Nov 13, 2026
de899ac
test: plot_retrospective test
JZahner1 Nov 13, 2026
37271a8
Merge remote-tracking branch 'upstream/main' into plot_retrospectives
JZahner1 Nov 17, 2026
e27f3ec
tests: fix retrospective test
JZahner1 Nov 17, 2026
f012a48
feat: add support for displaying externally calculated value of Mohn'…
JZahner1 Nov 17, 2026
acd3363
feat: allow saving retrospective plots
JZahner1 Nov 17, 2026
249a371
test: remove retrospectives from `save_all_plots`
JZahner1 Nov 18, 2026
674ae2c
Merge branch 'main' into plot_retrospectives
JZahner1 Nov 18, 2026
db12fa2
docs: updated roxygen for plot_retrospective
JZahner1 Nov 18, 2026
9503858
fix: add extra checks to verify inputs are correct sizes
JZahner1 Nov 18, 2026
39b3f45
chore: export plot_retrospective to NAMESPACE
JZahner1 Nov 18, 2026
ce970d8
chore: fix spelling
JZahner1 Nov 18, 2026
ca6da73
fix: handle edge case where dat only has one model
JZahner1 Nov 18, 2026
4ef772b
fix: automatically convert positive end_years to negative for consist…
JZahner1 Nov 18, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export(plot_natural_mortality)
export(plot_obsvpred)
export(plot_recruitment)
export(plot_recruitment_deviations)
export(plot_retrospective)
export(plot_runs_test)
export(plot_selectivity)
export(plot_spawning_biomass)
Expand Down
230 changes: 230 additions & 0 deletions R/plot_retrospective.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
#' Plot retrospective peels
#'
#' Plot a given quantity across multiple models with differing
#' end years, so as to plot retrospective peels.
#'
#' @inheritParams plot_spawning_biomass
#'
#' @param end_years Integer. vector of what year, relative to the maximum
#' year present in `dat`, should be treated as the final year for each
#' model. Should be <= 0.
#'
#' Default: 0
#'
#' @param quantity String. Assessment quantity to plot (e.g., spawning biomass,
#' biomass, recruitment, etc.). Should correspond to an existing `plot_*` function.
#'
#' Default: "spawning_biomass"
#'
#' @param show_end_year_points Logical. Whether to add points at the terminal year
#' for each model peel.
#'
#' Default: TRUE
#'
#' @param mohns_rho Numeric. Externally calculated value(s) of Mohn's rho. Should be
#' either a single value or of length `end_years-1`. Value of NULL disables
#' reporting Mohn's rho statistic on plot. If vector, names of vector entries should
#' match with model names.
#'
#' Default: NULL
#'
#' @param group String. Single column that groups the data.
#'
#' Default: "model"
#'
#' @return A plot showing values of an assessment quantity across different
#' retrospective peels.
#'
#' @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, but will prioritize the defined
#' group or facet by the function arguments. @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_retrospective(
#' dat = ss3_simple,
#' end_years = 0:-5,
#' quantity = "spawning_biomass",
#' show_end_year_points=TRUE
#' )
#' plot_retrospective(
#' dat = ss3_simple,
#' end_years = 0:-5,
#' quantity = "spr",
#' show_end_year_points=TRUE,
#' interactive=FALSE,
#' module = "derived_quants"
#' )
#' plot_retrospective(
#' dat = ss3_simple,
#' end_years = 0:-5,
#' quantity = "spr",
#' show_end_year_points=TRUE,
#' mohns_rho = c(0.023, 0.01, 1.25, 0.845, 0.068),
#' interactive=FALSE,
#' module = "derived_quants"
#' )
plot_retrospective <- function(
Comment thread
JZahner1 marked this conversation as resolved.
Resolved
dat,
end_years=NULL,
quantity="spawning_biomass",
show_end_year_points=TRUE,
mohns_rho = NULL,
group="model",
make_rda=FALSE,
figures_dir=getwd(),
...
){

# plot_biomass, plot_spawning_biomass, plot_fishing_mortality, plot_recruitment, plot_spr
quantity <- match.arg(quantity, c("biomass", "spawning_biomass", "fishing_mortality", "recruitment", "spr"))

func_name <- paste0("plot_",quantity)
if(!(func_name %in% ls(getNamespace("stockplotr")))){
cli::cli_abort("`quantity` not valid. Should correspond to one of the available stockplotr plotting functions.")
}

n_models <- length(unique(dat$model))

if(is.null(end_years)) end_years <- rep(0, n_models)
if(length(end_years) != n_models && n_models > 1){
cli::cli_alert_info("Different number of end_years and available models.")
end_years <- c(end_years, rep(0, n_models-length(end_years)))
}

# Convert end_years to negatives for math
if(any(end_years > 0)){
end_years[which(end_years > 0)] <- end_years[which(end_years > 0)]*-1
}


if(n_models == 1)
cli::cli_alert_info("Only 1 model detected in `dat`. Function expects multiple models corresponding to unique retrospective peels.")

if(is.null(names(end_years))){
cli::cli_alert("`end_years` provided without names. names automatically applied.")
names(end_years) <- dat$model |> unique()
}

if(!all(is.null(mohns_rho)) && length(mohns_rho) != n_models-1){
cli::cli_alert_warning("Invalid number of mohn's rho values provided. Should be number of peels - 1. Mohn's rho values will not be reported.")
mohns_rho <- NULL
}

if(!all(is.null(mohns_rho)) && is.null(names(mohns_rho))){
cli::cli_alert("`mohns_rho` values provided without names. names automatically applied.")
names(mohns_rho) <- unique(dat$model)[-1]
}

retro_dat <- dat |>
dplyr::filter(!is.na(year), !is.na(estimate)) |>
dplyr::mutate(term_year=max(year)) |>
dplyr::rowwise() |>
dplyr::mutate(
max_yr = term_year+end_years[which(model==names(end_years))],
peel = abs(end_years[which(model==names(end_years))])
) |>
dplyr::ungroup() |>
dplyr::filter(year < max_yr-1) |>
dplyr::select(-term_year, -max_yr)


func_pars <- list(
dat = retro_dat,
group="peel",
model = "All", # turn this on to bypass interactive model selection and plot all available models
make_rda=FALSE, # don't want to overwrite files from non-retro plots
...
)

plt <- do.call(func_name, func_pars)
plot_data <- plt$layers[[2]]$data |>
dplyr::rename(
dplyr::any_of(c(estimate="predicted_recruitment"))
)

if(show_end_year_points){
# get data from the "geom_line" elements and identfiy terminal year estimate
term_dat <- plot_data |>
dplyr::slice_max(order_by=year, n=1, by=model)

plt <- plt+
ggplot2::geom_point(
data=term_dat,
ggplot2::aes(
x=.data[["year"]],
y=.data[["estimate"]],
color={
if (length(unique(.data[["model"]])) > 1) {
interaction(model, group_var)
} else {
group_var
}
}
),
size=3.5
)
}

if(!all(is.null(mohns_rho))){

mohns_df <- data.frame(
label = sprintf("%.3f", mohns_rho)
)
mohns_df$model = names(mohns_rho)
mohns_df$vjust = 0.5 + seq_along(mohns_rho-1)*1.5
mohns_df$label <- paste0(mohns_df$model, " Mohns Rho:\t", mohns_df$label)

plt <- plt + ggplot2::geom_text(
data = mohns_df,
ggplot2::aes(
x=Inf,
y=Inf,
label=label,
vjust=vjust
),
# family = "mono",
size=6,
hjust=1.1
)

}

plt <- plt +
ggplot2::scale_color_discrete(labels=unique(retro_dat$model))+
ggplot2::scale_fill_discrete(labels=unique(retro_dat$model))+
ggplot2::guides(
color=ggplot2::guide_legend("Peel"),
fill=ggplot2::guide_legend("Peel")
)


if(make_rda){

create_rda(
object = plt,
topic_label = glue::glue("retrospective_{quantity}"),
fig_or_table = "figure",
dat = NULL,
dir = figures_dir,
ref_line = ifelse(!is.null(names(ref_line)), names(ref_line), ref_line),
scale_amount = scale_amount
)

cli::cli_alert_danger("No captions, alt-text, or key quantities were stored at this time.")
}

plt

}

# plot_retrospective(dat)
7 changes: 1 addition & 6 deletions R/save_all_plots.R
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,7 @@ save_all_plots <- function(
# plottab_function_files <- all_R_files[grepl("plot_|table_", all_R_files)]
# # Remove table_afsc_tier function for now
# plottab_function_files <- plottab_function_files[!grepl("afsc", plottab_function_files)]
# Helpers, and the composition plots, which take composition data from
# prepare_composition_data() rather than the standard output
helper_functions <- c(
"plot_timeseries", "plot_aa", "plot_obsvpred", "plot_error", "table_afsc_tier",
"plot_composition"
)
helper_functions <- c("plot_timeseries", "plot_aa", "plot_obsvpred", "plot_error", "plot_retrospective", "table_afsc_tier", "plot_composition")

# plottab_functions <- sub("\\.[^\\.]*$", "", plottab_function_files)
all_functions <- ls(getNamespace("stockplotr"))
Expand Down
2 changes: 2 additions & 0 deletions inst/WORDLIST
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ lendbase
Lifecycle
loess
McAllister
Mohn's
mohn's
mis
Modelling
MSY
Expand Down
113 changes: 113 additions & 0 deletions man/plot_retrospective.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading