diff --git a/R/plot_biomass.R b/R/plot_biomass.R index 8740f4dd..6d7cc096 100644 --- a/R/plot_biomass.R +++ b/R/plot_biomass.R @@ -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( diff --git a/R/plot_indices.R b/R/plot_indices.R index bd034fef..6a9e5838 100644 --- a/R/plot_indices.R +++ b/R/plot_indices.R @@ -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, @@ -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 diff --git a/R/plot_spawning_biomass.R b/R/plot_spawning_biomass.R index a4715ba0..904df894 100644 --- a/R/plot_spawning_biomass.R +++ b/R/plot_spawning_biomass.R @@ -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( diff --git a/R/process_data.R b/R/process_data.R index 46907a58..662c7f17 100644 --- a/R/process_data.R +++ b/R/process_data.R @@ -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(