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
78 changes: 33 additions & 45 deletions R/plot_biomass.R
Original file line number Diff line number Diff line change
Expand Up @@ -75,53 +75,41 @@ plot_biomass <- function(
}

# Filter data for spawning biomass
prepared_data <- filter_data(
dat = dat,
label_name = "^biomass$",
geom = geom,
group = group,
facet = facet,
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)
)
}

# 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

} else {
prepared_data <- filter_data(
dat = dat,
label_name = "^biomass$",
geom = geom,
group = group,
facet = facet,
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 (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)

# 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]]
}

plt <- plot_timeseries(
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
70 changes: 29 additions & 41 deletions R/plot_spawning_biomass.R
Original file line number Diff line number Diff line change
Expand Up @@ -146,49 +146,37 @@ plot_spawning_biomass <- function(
}

# Filter data for spawning biomass
prepared_data <- filter_data(
dat = dat,
label_name = "^spawning_biomass$",
geom = geom,
era = era,
group = group,
facet = facet,
module = module,
scale_amount = scale_amount,
interactive = interactive
)

# process the data for grouping
processing <- process_data(
dat = prepared_data,
group = group,
facet = facet,
method = "sum"
)
# variable <- processing[[1]]
plot_data <- processing[[1]]
group <- processing[[2]]
if (!is.null(processing[[3]])) facet <- processing[[3]]

# Override grouping variable when there is only NA's
if (!is.null(group)) {
if (group %notin% colnames(plot_data)) group <- NULL
}

# 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("spawning_biomass_", ref_line)
) / scale_amount

} else {
prepared_data <- filter_data(
dat = dat,
label_name = "^spawning_biomass$",
geom = geom,
era = era,
group = group,
facet = facet,
module = module,
scale_amount = scale_amount,
interactive = interactive
)

# process the data for grouping
processing <- process_data(
dat = prepared_data,
group = group,
facet = facet,
method = "sum"
)
# variable <- processing[[1]]
plot_data <- processing[[1]]
group <- processing[[2]]
if (!is.null(processing[[3]])) facet <- processing[[3]]

# Override grouping variable when there is only NA's
if (!is.null(group)) {
if (group %notin% colnames(plot_data)) group <- NULL
}
if (is.na(ref_line_val)) cli::cli_abort("Reference value not found. Cannot plot relative values.")
plot_data <- plot_data |>
dplyr::mutate(estimate = estimate / ref_line_val)
}

plt <- plot_timeseries(
Expand Down
2 changes: 1 addition & 1 deletion R/process_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ process_data <- function(
group <- NULL
}
}

# Ensure that index_variables -- group or facets are non-numeric to be plotted accurately
data <- data |>
dplyr::mutate(
Expand Down
Loading