Skip to content
94 changes: 67 additions & 27 deletions R/plot_spr.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
#' Plot spawning potential ratio (SPR)
Comment thread
JZahner1 marked this conversation as resolved.
#'
#' @inheritParams plot_spawning_biomass
#' @param quantity String. SPR quantity to plot.
#'
#' Default: "spr".
#'
#' Options: "spr", "fishing_intensity", or "spr_ratio".
#'
#' "spr" plots spawning potential ratio. "fishing_intensity" plots 1-SPR.
#' "spr_ratio" plots quantity associated with SPRratio label (may be user
#' specified in some models; e.g., Stock Synthesis).
#'
#' @param ylab String. Y-axis label to show.
#'
#' @returns A plot showing spawning potential ratio (SPR).
#'
Expand All @@ -14,22 +25,27 @@
#' @export
#'
#' @examples
#' plot_spr(
#' plot_spawning_potential_ratio(
#' dat = stockplotr:::example_data,
#' quantity = "spr",
#' unit_label = "metric tons",
#' group = "fleet",
#' interactive = FALSE,
#' make_rda = FALSE
#' make_rda = FALSE,
#' ylab = "SPR"
#' )
#' plot_spr(
#' plot_spawning_potential_ratio(
#' dat = stockplotr:::example_data,
#' quantity = "spr_ratio
#' unit_label = "metric tons",
#' facet = "fleet",
#' interactive = FALSE,
#' make_rda = FALSE
#' make_rda = FALSE,
#' ylab = "(1-SPR)/(1-SPR_ref)"
#' )
plot_spr <- function(
plot_spawning_potential_ratio <- function(
dat,
quantity = c("spr", "fishing_intensity", "spr_ratio"),
geom = "line",
group = NULL,
facet = NULL,
Expand All @@ -39,13 +55,25 @@ plot_spr <- function(
interactive = TRUE,
make_rda = FALSE,
figures_dir = getwd(),
ylab = NULL,
...
) {


if (length(quantity) > 1)
quantity <- "spr"
else
quantity <- match.arg(quantity)

label_filter <- switch(quantity,
"spr"="spr$", # only get SPR from SPR_SERIES
"fishing_intensity"="spr_report$", # spr_report is apparently 1-SPR
"spr_ratio"="^spawning_potential_ratio"
)

# Filter data for spr
prepared_data <- filter_data(
dat = dat,
label_name = "^spawning_potential_ratio",
label_name = label_filter,
geom = geom,
#TODO: change this to era once stockplotr::example_data updated
era = NULL,
Expand All @@ -59,10 +87,13 @@ plot_spr <- function(
dplyr::filter(!is.na(year))

# set y axis label
if (unique(prepared_data$label) == "spawning_potential_ratio_ratio"){
spr_label <- "Relative Fishing Intensity: (1-SPR)/(1-SPR_50%)"
} else {
spr_label <- "Spawning Potential Ratio"
# default values if ylab not provided
if(is.null(ylab)){
ylab <- switch(quantity,
"spr"="SPR (Spawning Potential Ratio)",
"fishing_intensity"="Fishing Intensity (1-SPR)",
"spr_ratio"="SPR Ratio"
)
}


Expand All @@ -88,22 +119,13 @@ plot_spr <- function(
if (!is.null(group)) {
if (group %notin% colnames(prepared_data)) group <- NULL
}

# Extract ref_line value
#TODO: update this once ref_line PR merged
if (is.null(names(ref_line))){
ref_pt <- calculate_reference_point(
dat = dat,
reference_name = glue::glue("spawning_potential_ratio_{ref_line}")
)
}

# inital base plot
plt <- plot_timeseries(
dat = prepared_data,
y = "estimate",
geom = geom,
ylab = spr_label,
ylab = ylab,
group = group,
facet = facet,
...
Expand All @@ -112,9 +134,17 @@ plot_spr <- function(
color = "grey") +
ggplot2::geom_hline(yintercept = 0,
color = "grey") +
ggplot2::geom_hline(yintercept = ref_pt,
color = "red") +
theme_noaa()

plt <- reference_line(
plot = plt,
dat = dat,
lbs = FALSE,
label_name = "spawning_potential_ratio",
reference = ref_line,
scale_amount = 1
) + theme_noaa()


if (length(unique(prepared_data$group_var)) == 1) {
plt <- plt + ggplot2::theme(legend.position = "none")
Expand All @@ -124,25 +154,35 @@ plot_spr <- function(
if (make_rda) {
# TODO: Update caption, alt text, and quantities once plot is finalized
# Obtain relevant key quantities for captions/alt text
spr.quantity <- switch(quantity,
"spr"="spawning potential ratio (SPR) (SB~current~/SB~unfished~)",
"fishing_intensity"="fishing intensity (1-SPR) (1-SB~current~/SB~unfished~)",
"spr_ratio"="relative fishing intensity (1-SPR)/(1-SPR~target~)"
)

spr.start.year <- min(prepared_data$year)
spr.end.year <- max(prepared_data$year)
spr.min <- min(prepared_data$estimate) |> round(digits = 3)
spr.max <- max(prepared_data$estimate) |> round(digits = 3)

spr.ref.pt <- as.character(ref_line)

# calculate & export key quantities
export_kqs(
spr.quantity,
spr.start.year,
spr.end.year,
spr.min,
spr.max
spr.ref.pt
)

# Add key quantities to captions/alt text
insert_kqs(
spr.quantity,
spr.start.year,
spr.end.year,
spr.min,
spr.max
spr.max,
spr.ref.pt
)

create_rda(
Expand All @@ -153,7 +193,7 @@ plot_spr <- function(
dat = dat,
dir = figures_dir,
scale_amount = scale_amount,
unit_label = unit_label
scale_amount = 1
)
}
# Output final plot
Expand Down
2 changes: 1 addition & 1 deletion inst/resources/captions_alt_text_template.csv
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ recruitment_deviations,figure,Annual deviations (on natural log scale) in the nu
tot.b,figure,Estimated biomass (B) time series. The horizontal dashed line represents the biomass limit reference point at B.ref.pt B.units.,"Line graph showing estimated biomass time series. The x axis shows the year, which spans from B.start.year to B.end.year. The y axis shows estimated biomass in B.units, which spans from B.min to B.max."
spawning_biomass,figure,Estimated spawning biomass (SB) time series. The horizontal dashed line represents the spawning biomass associated with the biomass limit reference point (ssb.ref.pt ssb.units).,"Line graph showing estimated spawning biomass. The x axis shows the year, which spans from ssb.start.year to ssb.end.year. The y axis shows estimated spawning biomass in ssb.units, which spans from ssb.min to ssb.max."
relative.spawning.biomass,figure,Estimated relative spawning biomass time series. The horizontal dashed line represents the limit reference point calculated as SB/SB(reference point) (ssb.ref.pt ssb.units).,"Line graph showing estimated relative spawning biomass. The x axis shows the year, which spans from ssb.start.year to ssb.end.year. The y axis shows estimated relative spawning biomass (SB/SB~target~), which spans from rel.ssb.min to rel.ssb.max ssb.units."
spr,figure,Estimated spawning potential ratio (SPR) (SB~current~/SB~target~) time series. The horizontal dashed line represents the spawning potential ratio of the limit reference point at spr.ref.pt.,"Line graph showing estimated spawning potential ratio over time. The x axis shows the year, which spans from ssb.start.year to ssb.end.year. The y axis shows estimated spawning potential ratio in SB~current~/SB~target~, which spans from spr.min to spr.max."
spr,figure,Estimated spawning potential ratio (SPR) (SB~current~/SB~target~) time series. The horizontal dashed line represents the spawning potential ratio of the target reference point at spr.ref.pt.,"Line graph showing estimated spr.quantity over time. The x axis shows the year, which spans from spr.start.year to spr.end.year. The y axis shows estimated spr.quantity, which spans from spr.min to spr.max."
biomass_at_age,figure,Estimated population numbers at age and population biomass at age over time. The relative size of each bubble for a given year and age indicates the relative abundance or biomass in that category compared with others. ,"Bubble plot showing estimated population numbers at age and population biomass at age. The x axis shows the year, which spans from pop.baa.start.year to pop.baa.end.year. The y axis shows age, which spans from pop.baa.age.min to pop.baa.age.max. The size of the bubbles range from pop.baa.fish.min to pop.baa.fish.max."
proj.catch,figure,Forecasted catch in proj.catch.units over future years for different fishing mortality scenarios as indicated in the legend.,"Time series line graph showing forecasted catch over future years for different fishing mortality scenarios. The x axis shows the year, which spans from proj.catch.start.year to proj.catch.end.year. The y axis shows catch in proj.catch.units, which spans from proj.catch.min to proj.catch.max."
proj.biomass,table,Forecasted biomass over future years for different fishing mortality scenarios.,
Expand Down
3 changes: 3 additions & 0 deletions inst/resources/key_quantity_template.csv
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ selectivity.length.max,,maximum length for gear selectivity,,,,
selectivity.length.min,,minimum length for gear selectivity,,,,
selectivity.length.units,,units for selectivity length,,,,
selectivity.start.year,,start year for selectivity estimates,,,,
spr.quantity,,type of spawning potential ratio quantity calculated,,,,
spr.start.year,,start year of SPR timeseries,,,,
spr.end.year,,end year of SPR timeseries,,,,
spr.max,,maximum spawning potential ratio,,,,
spr.min,,minimum spawning potential ratio,,,,
spr.ref.pt,,spawning potential ratio reference point,,,,
Expand Down
54 changes: 54 additions & 0 deletions tests/testthat/test-plot_spawning_potential_ratio.R
Comment thread
JZahner1 marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
test_that("plot_spawning_potential_ratio generates plots without errors", {
# expect error-free plot with minimal arguments
expect_no_error(
plot_spawning_potential_ratio(stockplotr::example_data, quantity="spr"),
)

expect_no_error(
plot_spawning_potential_ratio(stockplotr::example_data, quantity="fishing_intensity")
)

expect_no_error(
plot_spawning_potential_ratio(stockplotr::example_data, quantity="spr_ratio")
)


# expect error-free plot with many arguments
expect_no_error(
plot_spawning_potential_ratio(
stockplotr::example_data,
quantity = "spr_ratio",
ref_line = "biomass_target"
)
)


# expect ggplot object is returned
expect_s3_class(
plot_spawning_potential_ratio(
stockplotr::example_data,
ref_line = c("target" = 0.80)
),
"gg"
)
})

test_that("rda file made when indicated", {
Comment thread
JZahner1 marked this conversation as resolved.
# export rda
plot_spawning_potential_ratio(
stockplotr::example_data,
ref_line = "msy",
module = "DERIVED_QUANTITIES",
make_rda = TRUE,
figures_dir = getwd()
)

# expect that both figures dir and the spawning_biomass_figure.rda file exist
expect_true(dir.exists(fs::path(getwd(), "figures")))
expect_true(file.exists(fs::path(getwd(), "figures", "spr_figure.rda")))

# erase temporary testing files
file.remove(fs::path(getwd(), "captions_alt_text.csv"))
file.remove(fs::path(getwd(), "key_quantities.csv"))
unlink(fs::path(getwd(), "figures"), recursive = T)
})
Loading