Repository navigation
Plot retrospectives #509
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Plot retrospectives #509
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 6821495
fix: :bug: make sure input is a list of models
JZahner1 0a45764
fix: make sure columns are same type across model platforms
JZahner1 ae061be
tests: update expected dimensions not that "model" column is always a…
JZahner1 9ede8eb
tests: add test for convert_output on multiple models.
JZahner1 b3f7f96
refactor: removed list processing from filter_data
JZahner1 fa2277e
test: add tests for multimodel data processing
JZahner1 af39895
refactor: remove dplyr deprecation warnings from conout_ss3
JZahner1 27178cc
test: add test for plot_abundance_at_age with multiple models
JZahner1 7d89219
refactor: change reference_line helper functions to work with multipl…
JZahner1 fd4e0cb
tests: add tests for multiple reference lines
JZahner1 636e749
tests: use custom directories for wiring RDA files for export-rda tests
JZahner1 51e33be
Merge branch 'nmfs-ost:main' into feat-multi-model-processing
JZahner1 e1d6d0d
feat: Add handling for specifying "models" and "fleet_names" paramete…
JZahner1 719a111
docs: Update roxygen to reflect changes to parameters for multiple mo…
JZahner1 c0f24d3
docs: Update roxygen to reflect changes to parameters for multiple mo…
JZahner1 75f5e25
Merge branch 'feat-multi-model-processing' of https://github.com/JZah…
JZahner1 c3cfea6
fix: Swicth erroneous %>% to |> format
JZahner1 9506928
feat: :sparkles: plot_retrospective peels of any quantity
JZahner1 32d1cb3
docs: :memo: add roxygen for plot_retrospective
JZahner1 9da41ce
refactor: :art: cleanup edge case around data column for plot_recruit…
JZahner1 0ed29b4
fix: stop from overwriting existing captions/alt-text from base plott…
JZahner1 5ae9ee5
Merge remote-tracking branch 'upstream/main' into feat-multi-model-pr…
JZahner1 8c9ba5e
Merge remote-tracking branch 'upstream/main' into feat-multi-model-pr…
JZahner1 31d3971
mend
JZahner1 3d85770
Merge remote-tracking branch 'origin/feat-multi-model-processing' int…
JZahner1 de899ac
test: plot_retrospective test
JZahner1 37271a8
Merge remote-tracking branch 'upstream/main' into plot_retrospectives
JZahner1 e27f3ec
tests: fix retrospective test
JZahner1 f012a48
feat: add support for displaying externally calculated value of Mohn'…
JZahner1 acd3363
feat: allow saving retrospective plots
JZahner1 249a371
test: remove retrospectives from `save_all_plots`
JZahner1 674ae2c
Merge branch 'main' into plot_retrospectives
JZahner1 db12fa2
docs: updated roxygen for plot_retrospective
JZahner1 9503858
fix: add extra checks to verify inputs are correct sizes
JZahner1 39b3f45
chore: export plot_retrospective to NAMESPACE
JZahner1 ce970d8
chore: fix spelling
JZahner1 ca6da73
fix: handle edge case where dat only has one model
JZahner1 4ef772b
fix: automatically convert positive end_years to negative for consist…
JZahner1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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( | ||
| 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) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -85,6 +85,8 @@ lendbase | |
| Lifecycle | ||
| loess | ||
| McAllister | ||
| Mohn's | ||
| mohn's | ||
| mis | ||
| Modelling | ||
| MSY | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.