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
147 changes: 147 additions & 0 deletions R/plot_discard.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
#' Plot discards
#'
#' Plot discards as a line graph.
#'
#' @inheritParams plot_spawning_biomass
#'
#' @returns A ggplot showing discards over time, usually by fleet.
#'
#' @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. @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_discard(
#' dat = example_data
#'
#' )
plot_discard <- function(
dat,
group = NULL,
facet = NULL,
unit_label = "mt",
era = NULL,
lbs = FALSE,
scale_amount = 1,
interactive = TRUE,
module = NULL,
make_rda = FALSE,
figures_dir = getwd()
) {
# this assumes that the previous units were metric tons
if (lbs && unit_label %notin% c("lbs", "pounds", "lb")) {
cli::cli_alert_info("Unit label was not changed. Setting unit_label to 'lbs'.")
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,
label_name = "^discard$|discard_weight",
geom = "line",
era = era,
group = group,
facet = facet,
module = module,
scale_amount = scale_amount,
interactive = interactive
)

# Process the data to remove unneccessary columns and information
p_dat <- process_data(
prepared_data,
group = group,
facet = facet,
lbs = lbs
)
discards <- p_dat[[1]]
group <- p_dat[[2]]
facet <- p_dat[[3]]

# make the plot
plt <- plot_timeseries(
discards,
ylab = discard_label,
group = group,
facet = if (length(facet) > 0) facet else NULL#,
# ...
)

### Make RDA ----
if (make_rda) {
if (relative) {
rel.ssb.min <- calc_kqs(returned_kq = "rel.ssb.min",
final = final)


rel.ssb.max <- calc_kqs(returned_kq = "rel.ssb.max",
final = final)

# calculate & export key quantities
export_kqs(rel.ssb.min, rel.ssb.max)

# Add key quantities to captions/alt text
insert_kqs(rel.ssb.min, rel.ssb.max)
} else {
ssb.min <- min(plot_data$estimate) |> round(digits = 3)
ssb.max <- max(plot_data$estimate) |> round(digits = 3)

export_kqs(ssb.min, ssb.max)
insert_kqs(ssb.min, ssb.max)
}

# Obtain relevant key quantities for captions/alt text
ssb.ref.pt <- as.character(ref_line)
ssb.units <- as.character(unit_label)
ssb.start.year <- min(plot_data$year)
ssb.end.year <- max(plot_data$year)

# calculate & export key quantities
export_kqs(
ssb.ref.pt,
ssb.units,
ssb.start.year,
ssb.end.year
)

# Add key quantities to captions/alt text
insert_kqs(
ssb.ref.pt,
ssb.units,
ssb.start.year,
ssb.end.year
)

create_rda(
object = final,
topic_label = "discards",
fig_or_table = "figure",
dat = rp_dat,
dir = figures_dir,
scale_amount = scale_amount,
unit_label = unit_label
)
}
# Output final plot
plt + theme_noaa()
}
1 change: 1 addition & 0 deletions R/plot_spawning_biomass.R
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ plot_spawning_biomass <- function(
ggplot2::geom_point(ggplot2::aes(x = min_year - 1, y = ref_point)) + # should I keep -1 or set as first year?
theme_noaa()
} else {
# add apply/purrr/or for loop for reference lines -- not just the first anymore
final <- reference_line(
plot = plt,
dat = rp_dat,
Expand Down
4 changes: 0 additions & 4 deletions inst/resources/bam_var_names.csv
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,6 @@ LD.pr.tseries,L.klb,landings_weight
LD.pr.tseries,L.knum,landings_numbers
LD.pr.tseries,D.klb,discard_weight
LD.pr.tseries,D.knum,discard_numbers
LD.pr.tseries,l.klb,landings_weight
LD.pr.tseries,l.knum,landings_numbers
LD.pr.tseries,d.klb,discard_weight
LD.pr.tseries,d.knum,discard_numbers
a.series,length,length
a.series,length.cv,length_cv
a.series,length.sd,length_sd
Expand Down
Loading