Skip to content

Commit

Permalink
fix(plot_spawning_biomass): Remove complications
Browse files Browse the repository at this point in the history
Just have a standard plot and not all the if else statements by using
simple ifelse statements in the code. Also, removed plots for non-standard
output per Sam's guidance.

Changes to all plotting and tables
* dat is an object rather than reading in the file every time a figure or table
  is created, this change is carried over throughout the documentation and the
  code in all functions.
* Removed SS3 and BAM methods

Changes to `plot_spawning_biomass()`
* There was a problem with `if(any(grepl("target", output$label)))` because it
  ignored what the user specified in `ref_line` if target is present in label,
  i.e., you could never get to MSY. There should be a two-level check, i.e.,
  what does the user want and is it present. I have not fixed this latter
  problem.
* Fixed use of `..., ]$estimate` to `..., "estimate"]` because you should not
  use the dollar sign which allows for partial matches.
* Use `switch()` statements instead of nested `if` `else` statements. Actually,
  now the function is so simple that I do not even have any switch statements
  but they can be helpful instead of nested if else statements.
* Fixed the non-relative plots to truncate data to only use years less than end_yr
* Some of the scaled items were hard-coded to 1000 instead of using scale_amount
* Removed show_warnings because it was not actually doing anything
* Use end_year rather than renaming it to endyr
* Subset sb to end year before determining the correct number of breaks
* MSY does not work so I removed it from the default reference list, only
  target and unfished work

Need to fix:
  * units are not divided by scale amount to create new unit label
  * Why do we think that our determination of breaks is better than what ggplot2
    provides?
  * There is no example
  • Loading branch information
kellijohnson-NOAA committed Oct 1, 2024
1 parent b09ce6a commit 6664ebd
Show file tree
Hide file tree
Showing 11 changed files with 335 additions and 876 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Imports:
dplyr,
flextable,
ggplot2,
glue,
naniar,
nmfspalette,
stringr,
Expand Down
131 changes: 64 additions & 67 deletions R/plot_biomass.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#' @export
#'
plot_total_biomass <- function(dat,
model = "standard",
show_warnings = FALSE,
units = NULL,
scaled = FALSE,
Expand All @@ -40,81 +39,79 @@ plot_total_biomass <- function(dat,
bu <- "metric tons"
}

if(model == "standard"){
output <- read.csv(dat)
totb <- output |>
dplyr::filter(label == "biomass",
module_name == "DERIVED_QUANTITIES" | module_name == "t.series") |> # SS3 and BAM target module names
dplyr::mutate(estimate = as.numeric(estimate),
year = as.numeric(year))
if (is.null(end_year)){
endyr <- max(totb$year)
output <- dat
totb <- output |>
dplyr::filter(label == "biomass",
module_name == "DERIVED_QUANTITIES" | module_name == "t.series") |> # SS3 and BAM target module names
dplyr::mutate(estimate = as.numeric(estimate),
year = as.numeric(year))
if (is.null(end_year)){
endyr <- max(totb$year)
}
# Select value for reference line and label
# update the target option later
if (any(grepl("target", output$label))) {
ref_line_val <- as.numeric(output[grep("(?=.*biomass)(?=.*target)", output$label, perl = TRUE),]$estimate)
ref_line_label <- "target"
if (scaled) {
ann_add <- ggplot2::annotate("text", x = endyr + 0.05, y=ref_line_val, label = bquote(B[target])) # this might need to change
} else {
ann_add <- ggplot2::annotate("text", x = endyr + 0.05, y=ref_line_val/1000, label = bquote(B[target]))
}
# Select value for reference line and label
# update the target option later
if (any(grepl("target", output$label))) {
ref_line_val <- as.numeric(output[grep("(?=.*biomass)(?=.*target)", output$label, perl = TRUE),]$estimate)
ref_line_label <- "target"
if (scaled) {
ann_add <- ggplot2::annotate("text", x = endyr + 0.05, y=ref_line_val, label = bquote(B[target])) # this might need to change
} else {
ann_add <- ggplot2::annotate("text", x = endyr + 0.05, y=ref_line_val/1000, label = bquote(B[target]))
}
} else if (ref_line == "MSY" | ref_line == "msy") {
ref_line_val <- as.numeric(output[grep("(^biomass_msy)", output$label, perl = TRUE),]$estimate)
ref_line_label <- "MSY"
if (scaled) {
ann_add <- ggplot2::annotate("text", x = endyr + 0.05, y=ref_line_val, label = bquote(B[ref_line]))
} else {
ann_add <- ggplot2::annotate("text", x = endyr + 0.05, y=ref_line_val/1000, label = bquote(B[MSY]))
}
} else if (ref_line == "unfished") {
ref_line_val <- as.numeric(output[grep("(^biomass_unfished)", output$label, perl = TRUE),]$estimate)
ref_line_label <- "unfished"
if (scaled) {
ann_add <- ggplot2::annotate("text", x = endyr + 0.05, y=ref_line_val, label = bquote(B[unfished]))
} else {
ann_add <- ggplot2::annotate("text", x = endyr + 0.05, y=ref_line_val/1000, label = bquote(B[unfished]))
}
} else if (ref_line == "MSY" | ref_line == "msy") {
ref_line_val <- as.numeric(output[grep("(^biomass_msy)", output$label, perl = TRUE),]$estimate)
ref_line_label <- "MSY"
if (scaled) {
ann_add <- ggplot2::annotate("text", x = endyr + 0.05, y=ref_line_val, label = bquote(B[ref_line]))
} else {
ann_add <- ggplot2::annotate("text", x = endyr + 0.05, y=ref_line_val/1000, label = bquote(B[MSY]))
}
# Choose number of breaks for x-axis
x_n_breaks <- round(length(subset(totb, year<=endyr)$year)/10)
if (x_n_breaks <= 5) {
x_n_breaks <- round(length(subset(totb, year<=endyr)$year)/5)
} else if (ref_line == "unfished") {
ref_line_val <- as.numeric(output[grep("(^biomass_unfished)", output$label, perl = TRUE),]$estimate)
ref_line_label <- "unfished"
if (scaled) {
ann_add <- ggplot2::annotate("text", x = endyr + 0.05, y=ref_line_val, label = bquote(B[unfished]))
} else {
ann_add <- ggplot2::annotate("text", x = endyr + 0.05, y=ref_line_val/1000, label = bquote(B[unfished]))
}
if (relative) {
# plot relative TOTB
plt <- ggplot2::ggplot(data = subset(totb, year<=endyr)) +
ggplot2::geom_line(ggplot2::aes(x = year, y = estimate/ref_line_val), linewidth = 1) +
# ggplot2::geom_ribbon(ggplot2::aes(x = year, ymin = (value/ref_line_val - stddev/ref_line_val), ymax = (value/ref_line_val + stddev/ref_line_val)), colour = "grey", alpha = 0.3) +
ggplot2::geom_hline(yintercept = ref_line_val/ref_line_val, linetype = 2) +
}
# Choose number of breaks for x-axis
x_n_breaks <- round(length(subset(totb, year<=endyr)$year)/10)
if (x_n_breaks <= 5) {
x_n_breaks <- round(length(subset(totb, year<=endyr)$year)/5)
}
if (relative) {
# plot relative TOTB
plt <- ggplot2::ggplot(data = subset(totb, year<=endyr)) +
ggplot2::geom_line(ggplot2::aes(x = year, y = estimate/ref_line_val), linewidth = 1) +
# ggplot2::geom_ribbon(ggplot2::aes(x = year, ymin = (value/ref_line_val - stddev/ref_line_val), ymax = (value/ref_line_val + stddev/ref_line_val)), colour = "grey", alpha = 0.3) +
ggplot2::geom_hline(yintercept = ref_line_val/ref_line_val, linetype = 2) +
ggplot2::labs(x = "Year",
y = paste("Biomass (", bu, ")", sep = "")) +
ggplot2::scale_x_continuous(n.breaks = x_n_breaks,
guide = ggplot2::guide_axis(minor.ticks = TRUE))
} else {
if(scaled){
plt <- ggplot2::ggplot(data = totb) +
ggplot2::geom_line(ggplot2::aes(x = year, y = estimate), linewidth = 1) +
ggplot2::geom_hline(yintercept = ref_line_val, linetype = 2) +
ggplot2::labs(x = "Year",
y = paste("Biomass (", bu, ")", sep = "")) +
ggplot2::scale_x_continuous(n.breaks = x_n_breaks,
guide = ggplot2::guide_axis(minor.ticks = TRUE))
plt <- plt + ann_add
} else {
if(scaled){
plt <- ggplot2::ggplot(data = totb) +
ggplot2::geom_line(ggplot2::aes(x = year, y = estimate), linewidth = 1) +
ggplot2::geom_hline(yintercept = ref_line_val, linetype = 2) +
ggplot2::labs(x = "Year",
y = paste("Biomass (", bu, ")", sep = "")) +
ggplot2::scale_x_continuous(n.breaks = x_n_breaks,
guide = ggplot2::guide_axis(minor.ticks = TRUE))
plt <- plt + ann_add
} else {
plt <- ggplot2::ggplot(data = totb) +
ggplot2::geom_line(ggplot2::aes(x = year, y = estimate/1000), linewidth = 1) +
# ggplot2::geom_ribbon(ggplot2::aes(x = year, ymin = (value/1000 - stddev/1000), ymax = (value/1000 + stddev/1000)), colour = "grey", alpha = 0.3) +
ggplot2::geom_hline(yintercept = ref_line_val/1000, linetype = 2) +
ggplot2::labs(x = "Year",
y = paste("Biomass (", bu, ")", sep = "")) +
ggplot2::scale_x_continuous(n.breaks = x_n_breaks,
guide = ggplot2::guide_axis(minor.ticks = TRUE))
plt <- plt + ann_add
}
plt <- ggplot2::ggplot(data = totb) +
ggplot2::geom_line(ggplot2::aes(x = year, y = estimate/1000), linewidth = 1) +
# ggplot2::geom_ribbon(ggplot2::aes(x = year, ymin = (value/1000 - stddev/1000), ymax = (value/1000 + stddev/1000)), colour = "grey", alpha = 0.3) +
ggplot2::geom_hline(yintercept = ref_line_val/1000, linetype = 2) +
ggplot2::labs(x = "Year",
y = paste("Biomass (", bu, ")", sep = "")) +
ggplot2::scale_x_continuous(n.breaks = x_n_breaks,
guide = ggplot2::guide_axis(minor.ticks = TRUE))
plt <- plt + ann_add
}
plt_fin <- add_theme(plt)
}
plt_fin <- add_theme(plt)
return(plt_fin)
}
7 changes: 1 addition & 6 deletions R/plot_landings.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,9 @@
#' @export
#'
plot_landings <- function(dat,
model = "standard",
units = NULL){
# check to make sure file works with fxn
if (grepl(".sso|.rdat", dat)) {
stop("File type not compatible with function. Please use standard csv format of output files. An example can be found at https://github.com/nmfs-ost/satf")
}
# read standard data file and extract target quantity
land <- utils::read.csv(dat) |>
land <- dat |>
dplyr::filter(module_name == "t.series" | module_name == "CATCH", # t.series is associated with a conversion from BAM output and CATCH with SS3 converted output
grepl("landings", label) | label == "obs") |>
dplyr::mutate(estimate = as.numeric(estimate),
Expand Down
Loading

0 comments on commit 6664ebd

Please sign in to comment.