Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Streamline plots #13

Merged
merged 1 commit into from
Oct 3, 2024
Merged
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 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
Loading