Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -23,6 +23,7 @@ export(plot_recruitment)
export(plot_recruitment_deviations)
export(plot_selectivity)
export(plot_spawning_biomass)
export(plot_spawning_potential_ratio)
export(plot_stock_recruitment)
export(plot_timeseries)
export(process_data)
Expand Down
29 changes: 19 additions & 10 deletions R/plot_abundance_at_age.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,34 +63,28 @@
#'
#' @examples
#' plot_abundance_at_age(
#' dat = stockplotr:::example_data,
#' dat = stockplotr::example_data,
#' facet = "growth_pattern",
#' unit_label = "fish",
#' scale_amount = 1000,
#' proportional = TRUE
#' )
#' plot_abundance_at_age(
#' dat = stockplotr:::example_data,
#' dat = stockplotr::example_data,
#' facet = "none",
#' proportional = FALSE
#' )
plot_abundance_at_age <- function(
dat,
facet = NULL,
unit_label = "fish",
scale_amount = 1000,
unit_label = NULL,
scale_amount = 1,
proportional = TRUE,
module = NULL,
interactive = TRUE,
make_rda = FALSE,
figures_dir = getwd()
) {
# Create label for abundance units in legend
abundance_label <- label_magnitude(
label = "Abundance",
unit_label = unit_label,
scale_amount = scale_amount
)
# Filter data
b <- filter_data(
dat = dat,
Expand All @@ -102,6 +96,21 @@ plot_abundance_at_age <- function(
interactive = interactive
)

# Create label for abundance units in legend
if (is.null(unit_label)) {
unit_label <- unique(b$unit)
if (grepl("number", unit_label)) unit_label <- "fish"
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 <- ""
}
}
abundance_label <- label_magnitude(
label = "Abundance",
unit_label = unit_label,
scale_amount = scale_amount
)

if (!is.null(facet) && facet == "none") {
data <- b |>
dplyr::group_by(year, age) |>
Expand Down
44 changes: 25 additions & 19 deletions R/plot_biomass.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@
#'
#' @examples
#' plot_biomass(
#' dat = stockplotr:::example_data,
#' dat = stockplotr::example_data,
#' unit_label = "mt",
#' ref_line = c("target" = 20000),
#' scale_amount = 100,
#' module = "TIME_SERIES",
#' figures_dir = getwd()
#' )
#' plot_biomass(
#' dat = stockplotr:::example_data,
#' dat = stockplotr::example_data,
#' ref_line = NULL,
#' module = "TIME_SERIES",
#' figures_dir = getwd()
Expand All @@ -48,7 +48,7 @@ plot_biomass <- function(
facet = NULL,
ref_line = "msy",
era = NULL,
unit_label = "mt",
unit_label = NULL,
module = NULL,
scale_amount = 1,
relative = FALSE,
Expand All @@ -57,22 +57,6 @@ plot_biomass <- function(
interactive = TRUE,
...
) {
# TODO: Fix the unit label if scaling. Maybe this is up to the user to do if
# they want something scaled then they have to supply a better unit name
# or we create a helper function to do this.
biomass_label <- ifelse(
relative,
yes = "Relative biomass",
no = {
label_magnitude(
label = "Biomass",
unit_label = unit_label,
scale_amount = scale_amount,
legend = FALSE
)
}
)

# Pull first df if in a list to find reference point
if (!is.data.frame(dat)) {
rp_dat <- dat[[1]]
Expand Down Expand Up @@ -101,6 +85,28 @@ plot_biomass <- function(
scale_amount = scale_amount,
interactive = interactive
)

if (is.null(unit_label)) {
unit_label <- unique(prepared_data$unit)
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 <- ""
}
}

biomass_label <- ifelse(
relative,
yes = "Relative biomass",
no = {
label_magnitude(
label = "Biomass",
unit_label = unit_label,
scale_amount = scale_amount,
legend = FALSE
)
}
)

if (relative) {
if (nrow(prepared_data) == 0) {
cli::cli_abort("No data found for relative biomass. Please check that your data contains a label for 'biomass_biomass_unfished'.")
Expand Down
30 changes: 20 additions & 10 deletions R/plot_biomass_at_age.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,35 +27,29 @@
#'
#' @examples
#' plot_biomass_at_age(
#' dat = stockplotr:::example_data,
#' dat = stockplotr::example_data,
#' unit_label = "mt",
#' scale_amount = 100,
#' proportional = FALSE, # displays legend
#' make_rda = FALSE
#' )
#' plot_biomass_at_age(
#' dat = stockplotr:::example_data,
#' dat = stockplotr::example_data,
#' unit_label = "mt",
#' facet = "none",
#' proportional = FALSE
#' )
plot_biomass_at_age <- function(
dat,
facet = NULL,
unit_label = "mt",
scale_amount = 1000,
unit_label = NULL,
scale_amount = 1,
proportional = TRUE,
module = NULL,
interactive = TRUE,
make_rda = FALSE,
figures_dir = getwd()
) {
# Create label for abundance units in legend
biomass_label <- label_magnitude(
label = "Biomass",
unit_label = unit_label,
scale_amount = scale_amount
)
# Filter data
b <- filter_data(
dat = dat |>
Expand All @@ -68,6 +62,22 @@ plot_biomass_at_age <- function(
scale_amount = scale_amount,
interactive = interactive
)

if (is.null(unit_label)) {
unit_label <- unique(b$unit)
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 <- ""
}
}

# Create label for abundance units in legend
biomass_label <- label_magnitude(
label = "Biomass",
unit_label = unit_label,
scale_amount = scale_amount
)

if (!is.null(facet) && facet == "none") {
data <- b |>
dplyr::group_by(year, age) |>
Expand Down
29 changes: 18 additions & 11 deletions R/plot_catch_comp.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,16 @@
#' @export
#' @examples
#' plot_catch_comp(
#' dat = stockplotr:::example_data,
#' dat = stockplotr::example_data,
#' facet = "fleet",
#' unit_label = "mt",
#' scale_amount = 100,
#' interactive = FALSE,
#' make_rda = FALSE,
#' figures_dir = getwd()
#' )
#'
#' plot_catch_comp(
#' dat = stockplotr:::example_data,
#' dat = stockplotr::example_data,
#' facet = "none",
#' unit_label = "mt",
#' scale_amount = 100,
Expand All @@ -69,21 +68,14 @@ plot_catch_comp <- function(
dat,
facet = NULL,
era = "time",
unit_label = "mt",
unit_label = NULL,
scale_amount = 1,
proportional = TRUE,
interactive = TRUE,
module = NULL,
make_rda = FALSE,
figures_dir = getwd()
) {
# Create label for abundance units in legend
catch_label <- label_magnitude(
label = "catch",
unit_label = unit_label,
scale_amount = scale_amount,
legend = TRUE
)
# Filter data
catch <- filter_data(
dat = dat |>
Expand All @@ -96,6 +88,21 @@ plot_catch_comp <- function(
interactive = interactive,
module = module
)
if (is.null(unit_label)) {
unit_label <- unique(catch$unit)
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 <- ""
}
}
# Create label for abundance units in legend
catch_label <- label_magnitude(
label = "catch",
unit_label = unit_label,
scale_amount = scale_amount,
legend = TRUE
)

# Check for extracted data, if not return warning and empty plot
if (nrow(catch) == 0) {
cli::cli_alert_warning("No data found for catch at age. Please check the input data.")
Expand Down
34 changes: 21 additions & 13 deletions R/plot_discard.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@
#'
#' @examples
#' plot_discard(
#' dat = stockplotr:::example_data,
#' dat = stockplotr::example_data,
#' unit_label = "fish",
#' module = "DISCARD_OUTPUT"
#' )
plot_discard <- function(
dat,
group = NULL,
facet = NULL,
unit_label = "mt",
unit_label = NULL,
era = NULL,
lbs = FALSE,
scale_amount = 1,
Expand All @@ -43,17 +44,6 @@ plot_discard <- function(
unit_label <- "lbs"
}

discard_label <- label_magnitude(
label = "Discard",
unit_label = unit_label,
scale_amount = dplyr::if_else(
lbs,
ifelse(unit_label %in% c("mt", "mts", "metric tons", "metric ton"), 1000, 1) * scale_amount,
scale_amount
),
legend = TRUE
)

# Filter data for discards
prepared_data <- filter_data(
dat = dat,
Expand All @@ -66,6 +56,24 @@ plot_discard <- function(
scale_amount = scale_amount,
interactive = interactive
)

# 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)
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 <- ""
}
} else {
if (grepl("lbs|pounds|lb", unit_label, ignore.case = TRUE)) lbs <- TRUE
}
discard_label <- label_magnitude(
label = "Discard",
unit_label = unit_label,
scale_amount = scale_amount,
legend = TRUE
)

if (any(grepl("observed|predicted", unique(prepared_data$label)))) {
# select only the observed and predicted discards for plotting
Expand Down
22 changes: 13 additions & 9 deletions R/plot_index.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#'
plot_index <- function(
dat,
unit_label = "",
unit_label = NULL,
group = NULL,
# facet always assigned to fleet since that is how indices are calc'd -- unless replaced with NULL
facet = NULL,
Expand All @@ -44,13 +44,6 @@ plot_index <- function(
figures_dir = getwd(),
...
) {
# Set cpue unit label for plot
u_units <- ifelse(
is.null(unit_label) | unit_label == "",
glue::glue("Estimated Index"),
glue::glue("Estimated Index ({unit_label})")
)

if (is.null(facet) || "fleet" %notin% facet) {
facet <- c(facet, "fleet")
}
Expand All @@ -68,6 +61,17 @@ plot_index <- function(
interactive = interactive,
module = module
)
if (unit_label != "" && !is.na(unique(prepared_data$unit))) unit_label <- unique(prepared_data$unit)
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 <- ""
}
# Set cpue unit label for plot
u_units <- ifelse(
is.null(unit_label) | unit_label == "",
glue::glue("Estimated Index"),
glue::glue("Estimated Index ({unit_label})")
)

# Subset data if focus
if (!is.null(focus)) {
Expand Down Expand Up @@ -102,7 +106,7 @@ plot_index <- function(
predicted_label = "index_predicted",
geom = "line",
xlab = "Year",
ylab = "Estimated Index",
ylab = u_units,
group = group,
facet = facet
) +
Expand Down
Loading
Loading