From 31a2136cb418c0df388834281e90c5246354bb6f Mon Sep 17 00:00:00 2001 From: Schiano-NOAA Date: Fri, 20 Dec 2024 11:35:00 -0500 Subject: [PATCH] feature(plot_indices): create new plot of indices by fleet - fully functional and tested Testing not performed on rda --- NAMESPACE | 1 + R/plot_indices.R | 157 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 158 insertions(+) create mode 100644 R/plot_indices.R diff --git a/NAMESPACE b/NAMESPACE index 88020bd..246e3ec 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -3,6 +3,7 @@ export(add_theme) export(exp_all_figs_tables) export(plot_biomass) +export(plot_indices) export(plot_landings) export(plot_recruitment) export(plot_recruitment_deviations) diff --git a/R/plot_indices.R b/R/plot_indices.R new file mode 100644 index 0000000..c540beb --- /dev/null +++ b/R/plot_indices.R @@ -0,0 +1,157 @@ +#' Plot Index of Abundance +#' +#' @inheritParams plot_recruitment +#' @param unit_label units for index of abundance/CPUE +#' +#' @return Plot the estimated indices as indicated from a standard assessment +#' model output file. +#' @export +#' +plot_indices <- function( + dat, + unit_label = NULL, + make_rda = TRUE, + rda_dir = NULL +) { + # Set cpue unit label for plot + u_units <- glue::glue("Estimaed CPUE ({unit_label})") + + # Load data + output <- dat |> + dplyr::filter(module_name == "INDEX_2" | module_name == "t.series") + # Check for U + if (any(unique(output$module_name=="INDEX_2"))) { + output <- output |> + dplyr::filter(grepl("expected_indices", label)) # grepl("input_indices", label) | + } else if (any(unique(output$module_name=="t.series"))) { + output <- output |> + dplyr::filter(grepl("cpue", label)) + } + # Extract fleet names + fleet_names <- unique(as.character(output$fleet)) + factors <- c("year", "fleet", "fleet_name", "age", "sex", "area", "seas", "season", "time", "era", "subseas", "subseason", "platoon", "platoo","growth_pattern", "gp") + # re-structure df for table + indices <- output |> + tidyr::pivot_wider( + # id_cols = c(year, uncertainty, uncertainty_label), + names_from = label, + values_from = estimate + ) |> + dplyr::select(year, fleet, unique(output$label), uncertainty, uncertainty_label) # |> + + # na.omit() + # check if uncertainty is a measure in the df + if(all(is.na(indices$uncertainty))){ + indices <- indices |> + dplyr::select(-c(uncertainty_label, uncertainty)) + } else { + uncertainty_col <- paste("uncertainty_", unique(indices$uncertainty_label), sep = "") + colnames(indices) <- stringr::str_replace(colnames(indices), "^uncertainty$", uncertainty_col) + indices <- dplyr::select(indices, -uncertainty_label) + } + + # Check if observed/inital values are in the df + if (any(grepl("observed", colnames(indices)))) { + indices <- indices |> + dplyr::select(-colnames(indices)[grep(c("observed"), colnames(indices))]) + } + + # rename columns to remove cpue/effort + if(any(grep("_indices", colnames(indices)))){ + colnames(indices) <- stringr::str_replace_all(colnames(indices), "_indices", "") + } else { + colnames(indices) <- stringr::str_replace_all(colnames(indices), "cpue_", "") + } + + # Check for which column is U and filter out na values + if (any(grep("predicted", colnames(indices)))) { + indices <- indices |> + dplyr::filter(!is.na(predicted)) # |> + # dplyr::rename(estimated = predicted) + } + if (any(grep("expected", colnames(indices)))) { + indices <- indices |> + dplyr::filter(!is.na(expected)) # |> + # dplyr::rename(estimated = predicted) + } + + # Check for correct number of columns in dataframe + if (4 < ncol(indices)| ncol(indices) > 4) stop("Incorrect number of columns. Additional factor present.") + # Check for error type to present to user + # Double check this warning is correct + # if (grep("cv", colnames(indices)[4])) warning("Confidence Intervals were calculated using cv rather than se. Please use caution interpreting of the output plot.") + # Create condition for error type + if (grepl("cv", colnames(indices)[4])) { + err_val <- TRUE # indicates that the error value is cv + } else { + err_val <- FALSE # indicated that the error value is se + } + + # Rename column names for easier plotting + # This will break if columns are not in the correct order - add check? + colnames(indices) <- c("year", "fleet", "estimate", "uncertainty") + + # Final data set for plotting + indices2 <- indices |> + dplyr::mutate( + estimate_lower = dplyr::case_when(err_val ~ (estimate - uncertainty), + TRUE ~ (estimate - 1.96 * uncertainty)), + estimate_upper = dplyr::case_when(err_val ~ (estimate + uncertainty), + TRUE ~ (estimate + 1.96 * uncertainty)), + fleet = as.character(fleet), + year = as.numeric(year) + ) + + # create plot + plt <- ggplot2::ggplot(data = indices2) + + ggplot2::geom_line(ggplot2::aes(x = year, y = estimate), linewidth = 1) + + ggplot2::geom_ribbon( + data = indices2 |> dplyr::filter(!is.na(estimate_lower)), + ggplot2::aes( + x = year, + ymin = estimate_lower, + ymax = estimate_upper + ), + colour = "grey", + alpha = 0.3, + ) + + ggplot2::labs( + x = "Year", + y = u_units + ) + + ggplot2::facet_wrap(~ fleet) # + + # ggplot2::scale_x_continuous( + # n.breaks = x_n_breaks, + # guide = ggplot2::guide_axis(minor.ticks = TRUE) + # ) + + plt_fin <- suppressWarnings(add_theme(plt)) + + if (make_rda) { + # create plot-specific variables to use throughout fxn for naming and IDing + topic_label <- "indices_abun" + + # identify output + fig_or_table <- "figure" + + # run write_captions.R if its output doesn't exist + if (!file.exists( + fs::path(getwd(), "captions_alt_text.csv")) + ) { + satf::write_captions(dat = dat, + dir = rda_dir, + year = end_year) + } + + # extract this plot's caption and alt text + caps_alttext <- extract_caps_alttext(topic_label = topic_label, + fig_or_table = fig_or_table) + + export_rda(plt_fin = plt_fin, + caps_alttext = caps_alttext, + rda_dir = rda_dir, + topic_label = topic_label, + fig_or_table = fig_or_table) + } + plt_fin +}