Skip to content

Commit

Permalink
feature(plot_indices): create new plot of indices by fleet - fully fu…
Browse files Browse the repository at this point in the history
…nctional and tested

Testing not performed on rda
  • Loading branch information
Schiano-NOAA committed Dec 20, 2024
1 parent c9c3bb3 commit 31a2136
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
157 changes: 157 additions & 0 deletions R/plot_indices.R
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 31a2136

Please sign in to comment.