diff --git a/R/plot_abundance_at_age.R b/R/plot_abundance_at_age.R index 8c8d0e9f..22f7e7b7 100644 --- a/R/plot_abundance_at_age.R +++ b/R/plot_abundance_at_age.R @@ -11,7 +11,7 @@ #' #' @param unit_label String. Abundance units #' -#' Default: "fish" +#' Default: NULL #' #' @param scale_amount Number. A number describing how much to scale down the abundance at #' age. Please choose a value ranging from 1-1,000,000,000 (one billion) in orders @@ -199,5 +199,10 @@ plot_abundance_at_age <- function( dat ) } + + if (exists("selected_module")) { + rm(selected_module, envir = .GlobalEnv) + } + plot } diff --git a/R/plot_biomass.R b/R/plot_biomass.R index 6595b8a4..ac8570bc 100644 --- a/R/plot_biomass.R +++ b/R/plot_biomass.R @@ -3,7 +3,7 @@ #' @inheritParams plot_spawning_biomass #' @param unit_label String. Biomass units #' -#' Default: "mt" +#' Default: NULL #' @param ref_line String. Type of reference point to #' compare biomass to. The default `"msy"` looks for #' `"biomass_msy"` in the `"label"` column of `dat`. The actual @@ -337,10 +337,10 @@ plot_biomass <- function( ) } - # remove global variable "selected_module" - if (exists("selected_module", envir = .GlobalEnv)) rm("selected_module", envir = .GlobalEnv) + if (exists("selected_module")) { + rm(selected_module, envir = .GlobalEnv) + } - # Output final plot final } diff --git a/R/plot_biomass_at_age.R b/R/plot_biomass_at_age.R index 58e3dbd6..1b9cd130 100644 --- a/R/plot_biomass_at_age.R +++ b/R/plot_biomass_at_age.R @@ -165,5 +165,10 @@ plot_biomass_at_age <- function( dat ) } + + if (exists("selected_module")) { + rm(selected_module, envir = .GlobalEnv) + } + plot } diff --git a/R/plot_catch_at_age.R b/R/plot_catch_at_age.R index 11374f6e..4917c45d 100644 --- a/R/plot_catch_at_age.R +++ b/R/plot_catch_at_age.R @@ -3,7 +3,7 @@ #' @inheritParams plot_abundance_at_age #' @param unit_label String. Units of catch used to label the axis #' -#' Default: "mt" +#' Default: NULL #' #' @param scale_amount Number. A number describing how much to scale down the catch at #' age. Please choose a value ranging from 1-1,000,000,000 (one billion) in orders @@ -183,9 +183,14 @@ plot_catch_at_age <- function( # get name of function and remove "plot_" from it topic_label = gsub("plot_", "", utils::tail(as.character(sys.call()[[1]]), n = 1)), fig_or_table = "figure", - dir=figures_dir, + dir = figures_dir, dat ) } + + if (exists("selected_module")) { + rm(selected_module, envir = .GlobalEnv) + } + plot } diff --git a/R/plot_discard.R b/R/plot_discard.R index 796cbe6c..689bd45b 100644 --- a/R/plot_discard.R +++ b/R/plot_discard.R @@ -162,6 +162,10 @@ plot_discard <- function( scale_amount = scale_amount ) } - # Output final plot + + if (exists("selected_module")) { + rm(selected_module, envir = .GlobalEnv) + } + final } diff --git a/R/plot_fishing_mortality.R b/R/plot_fishing_mortality.R index dbe6fd7b..dfd5b141 100644 --- a/R/plot_fishing_mortality.R +++ b/R/plot_fishing_mortality.R @@ -203,10 +203,9 @@ plot_fishing_mortality <- function( ) } - # remove global variable "selected_module" - if (exists("selected_module", envir = .GlobalEnv)) rm("selected_module", envir = .GlobalEnv) + if (exists("selected_module")) { + rm(selected_module, envir = .GlobalEnv) + } - # Output final plot - final - + final } diff --git a/R/plot_index.R b/R/plot_index.R index a9c7c81a..2c3ad84a 100644 --- a/R/plot_index.R +++ b/R/plot_index.R @@ -8,7 +8,7 @@ #' Options: May include, but is not limited to: "fleet", "year", "area", NULL #' @param unit_label String. Index of abundance/CPUE units #' -#' Default: "" +#' Default: NULL #' @param focus Character vector. Column name or names used to filter data. #' (i.e. select names of fleets to zoom into the plot) #' @@ -173,6 +173,10 @@ plot_index <- function( dir = figures_dir ) } - # Output final plot + + if (exists("selected_module")) { + rm(selected_module, envir = .GlobalEnv) + } + plt } diff --git a/R/plot_landings.R b/R/plot_landings.R index 0ab3a74c..efd1b7bf 100644 --- a/R/plot_landings.R +++ b/R/plot_landings.R @@ -60,10 +60,32 @@ plot_landings <- function( # filter NA from year # dplyr::filter(!is.na(year)) + # Process data + processed_data <- process_data( + dat = prepared_data, + group = group, + facet = facet, + lbs = lbs + ) + landings_data <- processed_data[[1]] + group <- processed_data[[2]] + facet <- processed_data[[3]] + + # Check if there is >1 label + if (length(landings_data$label) > 1) { + landings_data <- landings_data |> + # always select the first label if TRUE + dplyr::filter(label == unique(label)[1]) + } + # extract unit if unit_label is null and set lbs to F by default lbs <- FALSE if (is.null(unit_label)) { - unit_label <- unique(prepared_data$unit) + labels <- unique(landings_data$label) + unit_label <- prepared_data |> + dplyr::filter(label %in% labels) |> + dplyr::pull(unit) |> + unique() if (is.na(unit_label)) { cli::cli_alert_warning("No unit label found in the data. Please provide a unit label using the 'unit_label' argument.") unit_label <- "" @@ -79,33 +101,15 @@ plot_landings <- function( scale_amount = dplyr::if_else(lbs, 1000 * scale_amount, scale_amount), legend = FALSE ) - - # Process data - processed_data <- process_data( - dat = prepared_data, - group = group, - facet = facet, - lbs = lbs - ) - prepared_data <- processed_data[[1]] - group <- processed_data[[2]] - facet <- processed_data[[3]] - - # Check if there is >1 label - if (length(prepared_data$label) > 1) { - prepared_data <- prepared_data |> - # always select the first label if TRUE - dplyr::filter(label == unique(label)[1]) - } # Override grouping variable when there is only NA's if (!is.null(group)) { - if (group %notin% colnames(prepared_data)) group <- NULL + if (group %notin% colnames(landings_data)) group <- NULL } # inital base plot plt <- plot_timeseries( - dat = prepared_data, + dat = landings_data, y = "estimate", geom = geom, ylab = landings_label, @@ -114,17 +118,17 @@ plot_landings <- function( ... ) + theme_noaa() - if (length(unique(prepared_data$group_var)) == 1) { + if (length(unique(landings_data$group_var)) == 1) { plt <- plt + ggplot2::theme(legend.position = "none") } ### Make RDA ---- if (make_rda) { # Obtain relevant key quantities for captions/alt text - landings.start.year <- min(prepared_data$year) - landings.end.year <- max(prepared_data$year) - landings.min <- min(prepared_data$estimate) |> round(digits = 3) - landings.max <- max(prepared_data$estimate) |> round(digits = 3) + landings.start.year <- min(landings_data$year) + landings.end.year <- max(landings_data$year) + landings.min <- min(landings_data$estimate) |> round(digits = 3) + landings.max <- max(landings_data$estimate) |> round(digits = 3) landings.units <- unit_label # calculate & export key quantities @@ -157,6 +161,10 @@ plot_landings <- function( scale_amount = scale_amount ) } - # Output final plot + + if (exists("selected_module")) { + rm(selected_module, envir = .GlobalEnv) + } + plt } diff --git a/R/plot_natural_mortality.R b/R/plot_natural_mortality.R index 5fb1af55..e3c70b03 100644 --- a/R/plot_natural_mortality.R +++ b/R/plot_natural_mortality.R @@ -142,6 +142,10 @@ plot_natural_mortality <- function( dir = figures_dir ) } - # Output final plot + + if (exists("selected_module")) { + rm(selected_module, envir = .GlobalEnv) + } + final } diff --git a/R/plot_spawning_biomass.R b/R/plot_spawning_biomass.R index f5c21dfc..91b3cb80 100644 --- a/R/plot_spawning_biomass.R +++ b/R/plot_spawning_biomass.R @@ -141,7 +141,6 @@ plot_spawning_biomass <- function( ) { check_data(dat) - # TODO: start deprecation procedure for argument lbs if (relative & scale_amount > 1) { cli::cli_alert_warning("Scale amount is not applicable when relative = TRUE. Resetting scale_amount to 1.") scale_amount <- 1 diff --git a/man/plot_abundance_at_age.Rd b/man/plot_abundance_at_age.Rd index 62bef3a3..3dd4ff31 100644 --- a/man/plot_abundance_at_age.Rd +++ b/man/plot_abundance_at_age.Rd @@ -29,7 +29,7 @@ NULL} \item{unit_label}{String. Abundance units -Default: "fish"} +Default: NULL} \item{scale_amount}{Number. A number describing how much to scale down the abundance at age. Please choose a value ranging from 1-1,000,000,000 (one billion) in orders diff --git a/man/plot_biomass.Rd b/man/plot_biomass.Rd index c358192b..e0881fa3 100644 --- a/man/plot_biomass.Rd +++ b/man/plot_biomass.Rd @@ -65,7 +65,7 @@ Options: "early", "time", "fore" (forecast), or NULL (all data)} \item{unit_label}{String. Biomass units -Default: "mt"} +Default: NULL} \item{module}{Character vector. (Optional) Module name found in \code{dat$module_name}. If selecting >1 module, place them in a vector like c("module1", "module2"). diff --git a/man/plot_biomass_at_age.Rd b/man/plot_biomass_at_age.Rd index 54fce339..66e19b6d 100644 --- a/man/plot_biomass_at_age.Rd +++ b/man/plot_biomass_at_age.Rd @@ -29,7 +29,7 @@ NULL} \item{unit_label}{String. Abundance units -Default: "fish"} +Default: NULL} \item{scale_amount}{Number. A number describing how much to scale down the biomass at age. For example, scale_amount = 100 would scale down a value diff --git a/man/plot_catch_at_age.Rd b/man/plot_catch_at_age.Rd index 296e36aa..8044bc76 100644 --- a/man/plot_catch_at_age.Rd +++ b/man/plot_catch_at_age.Rd @@ -36,7 +36,7 @@ Options: "early", "time", "fore" (forecast), or NULL (all data)} \item{unit_label}{String. Units of catch used to label the axis -Default: "mt"} +Default: NULL} \item{scale_amount}{Number. A number describing how much to scale down the catch at age. Please choose a value ranging from 1-1,000,000,000 (one billion) in orders diff --git a/man/plot_index.Rd b/man/plot_index.Rd index 12a0666c..0eb9591c 100644 --- a/man/plot_index.Rd +++ b/man/plot_index.Rd @@ -26,7 +26,7 @@ in \code{ref_line} is used to plot a reference line or calculate relative spawni \item{unit_label}{String. Index of abundance/CPUE units -Default: ""} +Default: NULL} \item{group}{String. Single column that groups the data. diff --git a/man/table_total_catch.Rd b/man/table_total_catch.Rd index 25f6c5ce..5026642f 100644 --- a/man/table_total_catch.Rd +++ b/man/table_total_catch.Rd @@ -6,7 +6,7 @@ \usage{ table_total_catch( dat, - unit_label = "mt", + unit_label = NULL, era = NULL, interactive = TRUE, module = NULL, @@ -25,7 +25,7 @@ in \code{ref_line} is used to plot a reference line or calculate relative spawni \item{unit_label}{String. Abbreviated catch units -Default: "mt"} +Default: NULL} \item{era}{String. Era of data. @@ -81,7 +81,8 @@ There are options to return a \code{\link[gt:gt]{gt::gt()}} object or export an containing a gt-based table, caption, and LaTeX-based table. } \examples{ -table_total_catch(stockplotr::example_data, +table_total_catch( + stockplotr::example_data, module = "TIME_SERIES" )