Skip to content
Open
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
91 changes: 53 additions & 38 deletions R/plot_biomass.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ plot_biomass <- function(
group = NULL,
facet = NULL,
ref_line = "msy",
era = NULL,
unit_label = "metric tons",
module = NULL,
scale_amount = 1,
Expand Down Expand Up @@ -74,55 +75,48 @@ plot_biomass <- function(
scale_amount <- 1
}

# Filter data for spawning biomass
# Filter data for biomass
# TODO: determine method to ID that first point in the timeseries is actually Bunfished ref pt
prepared_data <- filter_data(
dat = dat,
label_name = "^biomass$",
label_name = ifelse(relative, "biomass_biomass_unfished|biomass_ratio", "^biomass$"), # what exactly is biomass_ratio?
geom = geom,
group = group,
facet = facet,
era = era,
module = module,
scale_amount = scale_amount,
interactive = interactive
)

# check if all 3 are present and subset for one or two
if (length(unique(prepared_data$label)) > 1 & any(grepl("biomass$", unique(prepared_data$label)))) {
# cli::cli_alert_info("> 1 label name. Selecting total biomass only.")
prepared_data <- prepared_data |>
dplyr::filter(
grepl("biomass$", label)
)
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'.")
stop()
}
} else {
# check if all 3 are present and subset for one or two
if (length(unique(prepared_data$label)) > 1 & any(grepl("biomass$", unique(prepared_data$label)))) {
# cli::cli_alert_info("> 1 label name. Selecting total biomass only.")
prepared_data <- prepared_data |>
dplyr::filter(
grepl("biomass$", label)
)
}
}

# Process data for indexing/grouping
# TODO: check and add into process_data step to summarize when theres >1 label
processing <- process_data(
prepared_data,
group,
facet
)

# variable <- processing[[1]]
prepared_data <- processing[[1]]
group <- processing[[2]]
if (!is.null(processing[[3]])) facet <- processing[[3]]

# Calculate estimate if relative
if (relative) {
if (!is.null(names(ref_line))) {
ref_line_val <- ref_line[[1]]
# ref_line <- names(ref_line)
} else {
ref_line_val <- calculate_reference_point(
dat = rp_dat,
reference_name = glue::glue("^biomass_", ref_line)
) / scale_amount
}
if (is.na(ref_line_val)) cli::cli_abort("Reference value not found. Cannot plot relative values.")
prepared_data <- prepared_data |>
dplyr::mutate(estimate = estimate / ref_line_val)
}


plt <- plot_timeseries(
dat = prepared_data,
Expand All @@ -135,16 +129,37 @@ plot_biomass <- function(
)
# Add reference line
# getting data set - an ifelse statement in the fxn wasn't working

final <- reference_line(
plot = plt,
dat = rp_dat,
label_name = "biomass",
reference = ref_line,
relative = relative,
scale_amount = scale_amount
) +
theme_noaa()
if (relative) {
# don't add any reference line here and just add theme for final plot
final <- plt + theme_noaa()
} else {
if ("unfished" %in% c(names(ref_line), ref_line)) {
# find the minimum x axis value from the plot
min_year <- min <- ggplot2::ggplot_build(plt)@data[[2]] |>
as.data.frame() |>
dplyr::pull(y) |>
min() |>
round(digits = 2)
# find the reference point value for unfished
ref_point <- calculate_reference_point(
dat = stockplotr::example_data,
reference_name = "biomass_unfished"
) / scale_amount
# add point to plot and add theme
final <- plt +
ggplot2::geom_point(ggplot2::aes(x = min_year - 1, y = ref_point)) + # should I keep -1 or set as first year?
theme_noaa()
} else {
final <- reference_line(
plot = plt,
dat = rp_dat,
label_name = "biomass",
reference = ref_line,
scale_amount = scale_amount
) +
theme_noaa()
}
}

### Make RDA ----
if (make_rda) {
Expand Down
1 change: 1 addition & 0 deletions R/plot_biomass_at_age.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ plot_biomass_at_age <- function(
label_name = "^biomass",
geom = "point",
group = "age",
era = "time",
scale_amount = scale_amount,
interactive = interactive
)
Expand Down
50 changes: 9 additions & 41 deletions R/plot_fishing_mortality.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ plot_fishing_mortality <- function(
group = NULL,
facet = NULL,
ref_line = "msy",
era = "time",
era = NULL,
module = NULL,
relative = FALSE,
make_rda = FALSE,
figures_dir = getwd(),
interactive = TRUE,
Expand Down Expand Up @@ -62,16 +61,6 @@ plot_fishing_mortality <- function(
group <- processed_data[[2]]
facet <- processed_data[[3]]

# Extract reference point unless explicit
# if (!is.null(names(ref_line))) {
# ref_line_val <- ref_line[[1]]
# } else {
# reference_point <- calculate_reference_point(
# dat = dat,
# reference_name = glue::glue("fishing_mortality_", ref_line)
# )
# }

# Create base plot
plt <- plot_timeseries(
dat = prepared_data,
Expand All @@ -89,39 +78,18 @@ plot_fishing_mortality <- function(
# era = "time",
label_name = "fishing_mortality",
reference = ref_line,
relative = relative,
scale_amount = 1
) + theme_noaa()

### Make RDA ----
if (make_rda) {
if (relative) {
# Obtain relevant key quantities for captions/alt text
# pulling out the 2nd df in 'data' works for several datasets
rel.F.min <- ggplot2::ggplot_build(final)@data[[2]] |>
as.data.frame() |>
dplyr::pull(y) |>
min() |>
round(digits = 2)
rel.F.max <- ggplot2::ggplot_build(final)@data[[2]] |>
as.data.frame() |>
dplyr::pull(y) |>
max() |>
round(digits = 2)

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

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

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


F.min <- min(prepared_data$estimate) |> round(digits = 3)
F.max <- max(prepared_data$estimate) |> round(digits = 3)

export_kqs(F.min, F.max)
insert_kqs(F.min, F.max)

F.ref.pt <- as.character(ref_line)
F.start.year <- min(prepared_data$year)
F.end.year <- max(prepared_data$year)
Expand All @@ -140,7 +108,7 @@ plot_fishing_mortality <- function(

create_rda(
object = final,
topic_label = ifelse(relative, "relative_fishing_mortality", "fishing_mortality"),
topic_label = "fishing_mortality", # ifelse(relative, "relative_fishing_mortality", "fishing_mortality"),
fig_or_table = "figure",
dat = dat,
dir = figures_dir,
Expand Down
4 changes: 2 additions & 2 deletions R/plot_indices.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ plot_indices <- function(
prepared_data <- prepared_data |>
dplyr::filter(fleet %in% focus)
}

processed_data <- process_data(
dat = prepared_data,
group = group,
Expand Down Expand Up @@ -109,7 +109,7 @@ plot_indices <- function(
facet_formula <- stats::reformulate(facet)
plt <- plt + ggplot2::facet_wrap(facet_formula, scales = "free")
}

### Make RDA ----
if (make_rda) {
# Obtain relevant key quantities for captions/alt text
Expand Down
40 changes: 20 additions & 20 deletions R/plot_recruitment.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ plot_recruitment <- function(
dat,
unit_label = "mt",
scale_amount = 1,
era = "time",
era = NULL,
group = NULL,
facet = NULL,
# relative = FALSE,
Expand Down Expand Up @@ -124,25 +124,25 @@ plot_recruitment <- function(
}

# Plot vertical lines if era is not filtering
if (is.null(era)) {
# Find unique era
eras <- unique(filter_data$era)
if (length(eras) > 1) {
year_vlines <- c()
for (i in 2:length(eras)) {
erax <- filter_data |>
dplyr::filter(era == eras[i]) |>
dplyr::pull(year) |>
min(na.rm = TRUE)
year_vlines <- c(year_vlines, erax)
}
}
final <- final +
ggplot2::geom_vline(
xintercept = year_vlines,
color = "#999999"
)
}
# if (is.null(era)) {
# # Find unique era
# eras <- unique(filter_data$era)
# if (length(eras) > 1) {
# year_vlines <- c()
# for (i in 2:length(eras)) {
# erax <- filter_data |>
# dplyr::filter(era == eras[i]) |>
# dplyr::pull(year) |>
# min(na.rm = TRUE)
# year_vlines <- c(year_vlines, erax)
# }
# }
# final <- final +
# ggplot2::geom_vline(
# xintercept = year_vlines,
# color = "#999999"
# )
# }

# Make RDA
if (make_rda) {
Expand Down
48 changes: 24 additions & 24 deletions R/plot_recruitment_deviations.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
plot_recruitment_deviations <- function(
dat,
module = NULL,
era = "time",
era = NULL,
interactive = TRUE,
make_rda = FALSE,
figures_dir = getwd(),
Expand Down Expand Up @@ -78,29 +78,29 @@ plot_recruitment_deviations <- function(
theme_noaa()

# Plot vertical lines if era is not filtering
if (is.null(era)) {
# Find unique era
eras <- unique(filter_data$era)
if (length(eras) > 1) {
# era1 <- filter_data |>
# dplyr::filter(era == eras[1]) |>
# dplyr::pull(year) |>
# max(na.rm = TRUE)
year_vlines <- c()
for (i in 2:length(eras)) {
erax <- filter_data |>
dplyr::filter(era == eras[i]) |>
dplyr::pull(year) |>
min(na.rm = TRUE)
year_vlines <- c(year_vlines, erax)
}
}
final <- final +
ggplot2::geom_vline(
xintercept = year_vlines,
color = "#999999"
)
}
# if (is.null(era)) {
# # Find unique era
# eras <- unique(filter_data$era)
# if (length(eras) > 1) {
# # era1 <- filter_data |>
# # dplyr::filter(era == eras[1]) |>
# # dplyr::pull(year) |>
# # max(na.rm = TRUE)
# year_vlines <- c()
# for (i in 2:length(eras)) {
# erax <- filter_data |>
# dplyr::filter(era == eras[i]) |>
# dplyr::pull(year) |>
# min(na.rm = TRUE)
# year_vlines <- c(year_vlines, erax)
# }
# }
# final <- final +
# ggplot2::geom_vline(
# xintercept = year_vlines,
# color = "#999999"
# )
# }

# Make RDA
if (make_rda) {
Expand Down
Loading
Loading