From 74aa50c3b47d2e9b7fb148a2a832f871ffda1dde Mon Sep 17 00:00:00 2001 From: Schiano-NOAA Date: Fri, 15 Nov 2024 11:26:53 -0500 Subject: [PATCH] fix(bugs in fxns): minor adjustments to rec devs, SR, and SB plots --- R/plot_recruitment_deviations.R | 23 ++++++++++++++++++++-- R/plot_spawn_recruitment.R | 34 ++++++++++++++++----------------- R/plot_spawning_biomass.R | 9 ++++++++- 3 files changed, 46 insertions(+), 20 deletions(-) diff --git a/R/plot_recruitment_deviations.R b/R/plot_recruitment_deviations.R index 975c77b..fb30582 100644 --- a/R/plot_recruitment_deviations.R +++ b/R/plot_recruitment_deviations.R @@ -18,7 +18,7 @@ plot_recruitment_deviations <- function ( } else { end_year <- end_year } - stryr <- min(as.numeric(dat$year), na.rm = TRUE) + start_year <- min(as.numeric(dat$year), na.rm = TRUE) rec_devs <- dat |> dplyr::filter(label == "recruitment_deviations" | label == "log_recruitment_deviations", @@ -39,9 +39,22 @@ plot_recruitment_deviations <- function ( stop("No recruitment deviations found in data.") } + # change plot breaks + x_n_breaks <- round(length(rec_devs[["year"]]) / 10) + if (x_n_breaks <= 5) { + x_n_breaks <- round(length(rec_devs[["year"]]) / 5) + } else if (x_n_breaks > 10) { + x_n_breaks <- round(length(rec_devs[["year"]]) / 15) + } + # Plot plt <- ggplot2::ggplot(data = rec_devs) + - ggplot2::geom_point(ggplot2::aes(x = year, y = estimate), shape = 1, size = 2.5) + + ggplot2::geom_point( + ggplot2::aes( + x = year, + y = estimate), + shape = 1, + size = 2.5) + # ggplot2::geom_pointrange( # ggplot2::aes( # x = year, @@ -59,6 +72,12 @@ plot_recruitment_deviations <- function ( ggplot2::labs( x = "Year", y = "Recruitment Deviations" + ) + + ggplot2::scale_x_continuous( + n.breaks = x_n_breaks, + guide = ggplot2::guide_axis( + minor.ticks = TRUE ) + ) suppressWarnings(add_theme(plt)) } diff --git a/R/plot_spawn_recruitment.R b/R/plot_spawn_recruitment.R index 1d50de6..215b55b 100644 --- a/R/plot_spawn_recruitment.R +++ b/R/plot_spawn_recruitment.R @@ -13,16 +13,9 @@ plot_spawn_recruitment <- function( recruitment_label = "metric tons", end_year = NULL ) { - if (is.null(end_year)){ - endyr <- max(rec$year) - } else { - endyr <- end_year - } - stryr <- min(rec$year) - - # Extract spawning biomass - sb <- output |> - dplyr::filter(label == "spawning_biomass", + # Extract recruitment + rec <- dat |> + dplyr::filter(label == "recruitment", module_name == "TIME_SERIES" | module_name == "t.series", !is.na(year), is.na(fleet) | length(unique(fleet)) <= 1, @@ -33,12 +26,19 @@ plot_spawn_recruitment <- function( ) |> # SS3 and BAM target module names dplyr::mutate(estimate = as.numeric(estimate), year = as.numeric(year)) |> - dplyr::rename(spawning_biomass = estimate) |> + dplyr::rename(recruitment = estimate) |> dplyr::select(-c(module_name, label)) - # Extract recruitment - rec <- dat |> - dplyr::filter(label == "recruitment", + if (is.null(end_year)){ + endyr <- max(rec$year) + } else { + endyr <- end_year + } + stryr <- min(rec$year) + + # Extract spawning biomass + sb <- dat |> + dplyr::filter(label == "spawning_biomass", module_name == "TIME_SERIES" | module_name == "t.series", !is.na(year), is.na(fleet) | length(unique(fleet)) <= 1, @@ -49,7 +49,7 @@ plot_spawn_recruitment <- function( ) |> # SS3 and BAM target module names dplyr::mutate(estimate = as.numeric(estimate), year = as.numeric(year)) |> - dplyr::rename(recruitment = estimate) |> + dplyr::rename(spawning_biomass = estimate) |> dplyr::select(-c(module_name, label)) # merge DF @@ -57,9 +57,9 @@ plot_spawn_recruitment <- function( # Plot plt <- ggplot2::ggplot(data = sr) + - ggplot2::geom_line(ggplot2::aes(x = spawning_biomass, y = recruitment/1000), linewidth = 1) + + ggplot2::geom_point(ggplot2::aes(x = spawning_biomass, y = recruitment/1000)) + ggplot2::labs(x = glue::glue("Spawning Biomass ({spawning_biomass_label})"), y = glue::glue("Recruitment ({recruitment_label})")) + ggplot2::theme(legend.position = "none") - suppressWarnings(add_theme(sr_plt)) + suppressWarnings(add_theme(plt)) } diff --git a/R/plot_spawning_biomass.R b/R/plot_spawning_biomass.R index 1b3f74c..026a98f 100644 --- a/R/plot_spawning_biomass.R +++ b/R/plot_spawning_biomass.R @@ -21,7 +21,7 @@ plot_spawning_biomass <- function( dat, unit_label = "metric ton", scale_amount = 1, - ref_line = c("target", "unfished"), + ref_line = c("target", "unfished", "msy"), end_year = NULL, relative = FALSE, n_projected_years = 10 @@ -45,6 +45,8 @@ plot_spawning_biomass <- function( stopifnot(any(end_year >= all_years)) # Select value for reference line and label + # TODO: add case if ref_line not indicated or hard to find - find one of the + # options and set as ref_line ref_line_val <- as.numeric(dat[ grep( pattern = glue::glue("^spawning_biomass.*{tolower(ref_line)}"), @@ -57,6 +59,11 @@ plot_spawning_biomass <- function( "The resulting reference value of `spawning_biomass_{ref_line}` was not found in `dat[[\"label\"]]`." )) + } else if (length(ref_line_val > 1)) { + warning(glue::glue( + "More than one of the resulting reference value of `spawning_biomass_{ref_line}` was + not in `dat[[\"label\"]]`." + )) } sb <- dat |> dplyr::filter(