diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 11eeea0b..02a4544a 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -8,10 +8,11 @@ "features": { "ghcr.io/rocker-org/devcontainer-features/quarto-cli:1": {}, "ghcr.io/rocker-org/devcontainer-features/apt-packages:1": { - "packages": "make,gcc,g++,cmake,clang-tidy,clang-format" + "packages": "make,gcc,g++,cmake,clang-tidy,clang-format,libuv1-dev" }, - "ghcr.io/rocker-org/devcontainer-features/r-packages:1": { - "packages": "cli,rlang,usethis,devtools,ggplot2,remotes,pak", + "ghcr.io/rocker-org/devcontainer-features/r-packages:1": { + "packages": "cli,rlang,usethis,devtools,ggplot2,gt,remotes,pak", + "cranMirror": "https://cloud.r-project.org", "installSystemRequirements": true } }, @@ -42,7 +43,11 @@ "requireLocalPort": true, "onAutoForward": "ignore" } - }, + }, + "postCreateCommand": [ + "sudo", "-n", "Rscript", "-e", + "write('options(repos = c(CRAN = \"https://cloud.r-project.org\"))', file = file.path(R.home('etc'), 'Rprofile.site'), append = TRUE)" + ], "postAttachCommand": "sudo rstudio-server start", "remoteUser": "rstudio" diff --git a/DESCRIPTION b/DESCRIPTION index 39284503..20a84db9 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -54,6 +54,7 @@ Imports: utils Suggests: flextable, + ggridges, here, kableExtra, knitr, diff --git a/NAMESPACE b/NAMESPACE index 9c39f6d8..ba040edd 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -11,10 +11,12 @@ export(extract_sis_data) export(filter_data) export(plot_aa) export(plot_abundance_at_age) +export(plot_age_to_length_conversion) export(plot_biomass) export(plot_biomass_at_age) export(plot_catch_at_age) export(plot_data_availability) +export(plot_density) export(plot_discard) export(plot_fishing_mortality) export(plot_index) diff --git a/R/conout_ss3.R b/R/conout_ss3.R index b6b011c0..34bf252d 100644 --- a/R/conout_ss3.R +++ b/R/conout_ss3.R @@ -162,8 +162,10 @@ conout_ss3 <- function( rand <- c( "SPAWN_RECRUIT_CURVE", # RAND "sprseries", # RAND - "parameters" # RAND + "parameters", # RAND # "equil_yeild", # SKIP FOR NOW - RAND + "ALK" + # "AAK" ) aa.al <- c( "sizeselex", # AA.AL @@ -230,7 +232,7 @@ conout_ss3 <- function( extract <- file[[parm_sel]] } - if (!is.data.frame(extract)) { + if (!is.data.frame(extract) && parm_sel %notin% c("ALK")) { if (is.numeric(extract)) { df <- data.frame( label = parm_sel, @@ -677,6 +679,8 @@ conout_ss3 <- function( # "SPAWN_RECR_CURVE" # "Biology_at_age_in_endyr" # "PARAMETERS" + # "ALK" + # "AAK" if (parm_sel %in% c("SPAWN_RECR_CURVE", "SPAWN_RECRUIT_CURVE")) { # 32 @@ -985,6 +989,51 @@ conout_ss3 <- function( df4[setdiff(tolower(names(out_new)), tolower(names(df4)))] <- NA # Add to out list out_list[[parm_sel]] <- df4 + } else if (parm_sel == "ALK") { + # extract matrix into df + # ID dimensions and assign + df1 <- as.data.frame.table(extract, responseName = "value", stringsAsFactors = FALSE) + colnames(df1)[1:3] <- c("len_bins", "age", "slice_label") + + # Dynamically extract all "Key: Value" pairs and pivot + df2 <- df1 |> + # Match every 'Word: Number' pair (supporting decimals and negative numbers) + dplyr::mutate(pairs = stringr::str_extract_all(slice_label, "([A-Za-z0-9_]+):\\s*([-+]?\\d*\\.?\\d+)")) |> + tidyr::unnest(pairs) |> + tidyr::separate_wider_delim( + cols = pairs, + delim = ":", + names = c("variable", "val") + ) |> + dplyr::mutate( + variable = stringr::str_trim(variable), + val = type.convert(stringr::str_trim(val), as.is = TRUE) # Automatically parses numeric types + ) |> + tidyr::pivot_wider( + names_from = variable, + values_from = val + ) |> + dplyr::select(-slice_label) |> + dplyr::relocate(len_bins, age, tidyselect::where(is.numeric), value) + colnames(df2) <- tolower(colnames(df2)) + # rename columns if not std & add module_name + label + # I believe this is the only ones + rename_map <- c( + season = "seas", + subseason = "sub_seas" , + estimate = "value" + # gp = "growth_pattern" + ) + df3 <- df2 |> + dplyr::rename(dplyr::any_of(rename_map)) |> + dplyr::mutate( + module_name = parm_sel, + label = "age_to_length_conversion") + df3[setdiff(tolower(names(out_new)), tolower(names(df3)))] <- NA + # Add to out list + out_list[[parm_sel]] <- df3 + } else if (parm_sel == "AAK") { + miss_parms <- c(miss_parms, parm_sel) } else { miss_parms <- c(miss_parms, parm_sel) next diff --git a/R/convert_output.R b/R/convert_output.R index 3622f13d..df209797 100644 --- a/R/convert_output.R +++ b/R/convert_output.R @@ -329,7 +329,6 @@ convert_output <- function( dplyr::rename(length_bins = len_bins) } - out_new <- out_new |> dplyr::mutate( model=model_name, diff --git a/R/plot_age_to_length.R b/R/plot_age_to_length.R new file mode 100644 index 00000000..dbfef0ae --- /dev/null +++ b/R/plot_age_to_length.R @@ -0,0 +1,262 @@ +#' Plot Age-to-Length Conversion +#' +#' Creates a density plot of age-to-length conversion, either fixed input or +#' realized growth-derived output. +#' +#' @param dat Data frame or list. A tibble or named list of tibbles (input as `list()`) +#' returned from \link[stockplotr]{convert_output}. +#' +#' @param group String. Single column that groups the data. +#' +#' Set group = "none" to summarize data over all indexing values. +#' +#' Default: NULL +#' Options: Including, but not limited to: "year", "area", "fleet", "sex", "none", NULL +#' +#' @param facet Character vector. Column name or names used for faceting. +#' +#' Default: NULL +#' @param unit_label String. Length unit as indicated from the data. +#' +#' Default: "cm" +#' @param module Character vector. (Optional) Module name found in `dat$module_name`. +#' If selecting >1 module, place them in a vector like c("module1", "module2"). +#' +#' Default: NULL +#' +#' If the interactive and >1 module_name is found, user will select the +#' module_name in the console. @seealso [filter_data()] +#' @param make_rda Logical. TRUE/FALSE; indicate whether to save the object and +#' make an automated caption and alternative text in the form of an `rda` object. If TRUE, +#' the rda will be exported to the folder indicated in the argument "figures_dir". +#' +#' Default: `FALSE`. +#' @param figures_dir Path. Path to the "figures" folder. +#' +#' Default: `getwd()` +#' +#' The folder is created within the path if it does not exist. +#' @param interactive Logical. TRUE/FALSE; indicate whether the environment is interactive. +#' +#' Default: `FALSE` +#' @param ... Arguments called from \link[ggplot2]{geom_line} or \link[ggplot2]{geom_point} +#' @examples +#' \dontrun{ +#' plot_age_to_length_conversion( +#' dat = your_data, +#' unit_label = "cm" +#' ) +#' } +#' +#' @return A ggplot object. +#' +#' @export +plot_age_to_length_conversion <- function( + dat, + group = NULL, + facet = NULL, + module = NULL, + unit_label = "cm", + make_rda = FALSE, + figures_dir = getwd(), + interactive = TRUE, + ... +) { + # Filter data for age-to-length conversion + prepared_data <- filter_data( + dat = dat, + label_name = glue::glue("age_to_length_conversion"), + facet = facet, + module = module, + interactive = interactive, + geom = "line" + ) + + if (nrow(prepared_data) == 0) { + cli::cli_abort("No data found for relative age-to-length conversion. Please check that your data contains a label for 'age_to_length_conversion'.") + stop() + } + + # process the data for grouping + processing <- process_data( + dat = prepared_data, + facet = facet + ) + + plot_data <- processing[[1]] + group <- processing[[2]] + if (!is.null(processing[[3]])) facet <- processing[[3]] + if(group %in% c("age", "length_bins")){ + group <- NULL + } else { + facet <- c(facet, group) + group <- NULL + } + if(any(facet %in% c("age", "length_bins"))) { + facet <- facet[-grep("age|length_bins", facet)] + } + # Override grouping variable when there is only NA's + if (!is.null(group)) { + if (group %notin% colnames(plot_data)) group <- NULL + } + + length_bins <- sort(as.numeric(unique(plot_data[["length_bins"]]))) + n_length_bins <- dplyr::n_distinct(plot_data[["length_bins"]]) + + plt <- plot_density( + dat = plot_data, + xlab = ifelse(is.null(unit_label), "Length bins", glue::glue("Length bins ({unit_label})")), + n_length_bins = n_length_bins, + # add check in case facet is returned as character(0) + facet = if (length(facet) > 0) facet else NULL + # ... + ) + + theme_noaa() + + + ### Make RDA ---- + if (make_rda) { + + plot_data <- plot_data |> + dplyr::mutate( + age = as.numeric(age), + length_bins = as.numeric(length_bins) + ) + + #add any additional key quantities or processing needed before creating the RDA + age.length.group <- glue::glue_collapse(facet, sep = ", ", last = " and ") + length.units <- unit_label + age.length.age.min <- min(plot_data[["age"]], na.rm = TRUE) + age.length.age.max <- max(plot_data[["age"]], na.rm = TRUE) + age.length.length.min <- min(plot_data[["length_bins"]], na.rm = TRUE) + age.length.length.max <- max(plot_data[["length_bins"]], na.rm = TRUE) + + get_mean.length.description <- function(which.age) { + age_data <- plot_data |> + dplyr::filter(.data[["age"]] == which.age) + if (length(facet) > 0) { + age_data <- age_data |> + dplyr::group_by(dplyr::across(dplyr::all_of(facet))) + } + + age_data |> + dplyr::summarize( + length.at.age = sum( + .data[["length_bins"]] * .data[["estimate"]], + na.rm = TRUE + ) / sum(.data[["estimate"]], na.rm = TRUE), + .groups = "drop" + ) + } + + length.at.min.age.kq <- get_mean.length.description( + age.length.age.min + ) + + length.at.max.age.kq <- get_mean.length.description( + age.length.age.max + ) + + if (length(facet) > 0) { + # get the first two facet groups seen in the ggplot + panel.groups <- ggplot2::ggplot_build(plt)$layout$layout |> + dplyr::arrange(.data[["PANEL"]]) |> + dplyr::select(dplyr::all_of(facet)) |> + dplyr::distinct() |> + dplyr::slice_head(n = 2) + + # create tibble of key quantities for the first two facet groups + panel.kqs <- panel.groups |> + dplyr::left_join( + dplyr::rename( + length.at.min.age.kq, + length.at.min.age = length.at.age + ), + by = facet + ) |> + dplyr::left_join( + dplyr::rename( + length.at.max.age.kq, + length.at.max.age = length.at.age + ), + by = facet + ) + } else { + panel.kqs <- tibble::tibble( + length.at.min.age = length.at.min.age.kq[["length.at.age"]][[1]], + length.at.max.age = length.at.max.age.kq[["length.at.age"]][[1]] + ) + } + + # create an empty vector to store the mean length descriptions for each panel + mean.length.description <- character(nrow(panel.kqs)) + for (i in seq_len(nrow(panel.kqs))) { + facet.description <- if (length(facet) > 0) { + # create the facet label used in the ggplot + paste( + vapply( + facet, + function(f) { + paste(f, as.character(panel.kqs[[f]][[i]])) + }, + character(1) + ), + collapse = ", " + ) + } else { + NULL + } + # set variable for which facet is getting described + description.prefix <- if (is.null(facet.description)) { + "" + } else { + glue::glue("For {facet.description},") + } + + length.at.min.age <- signif(panel.kqs[["length.at.min.age"]][[i]], digits = 2) + length.at.max.age <- signif(panel.kqs[["length.at.max.age"]][[i]], digits = 2) + mean.length.description[[i]] <- glue::glue( + "{description.prefix} mean length increases from about {length.at.min.age} {length.units} at age {age.length.age.min} to about {length.at.max.age} {length.units} at age {age.length.age.max}. " + ) + } + + # create full mean length description for all panels + mean.length.description <- glue::glue_collapse( + mean.length.description, + sep = " " + ) + + # calculate & export key quantities + export_kqs( + length.units, + age.length.group, + age.length.age.min, + age.length.age.max, + age.length.length.min, + age.length.length.max, + mean.length.description, + dir = figures_dir + ) + # Add key quantities to captions/alt text + insert_kqs( + length.units, + age.length.group, + age.length.age.min, + age.length.age.max, + age.length.length.min, + age.length.length.max, + mean.length.description, + dir = figures_dir + ) + + create_rda( + object = plt, + topic_label = "age_to_length_conversion", + fig_or_table = "figure", + dat = plot_data, + dir = figures_dir + ) + } #end make_rda + # Output final plot + plt +} diff --git a/R/utils_plot.R b/R/utils_plot.R index cfb47702..d82642d6 100644 --- a/R/utils_plot.R +++ b/R/utils_plot.R @@ -1231,3 +1231,125 @@ add_reference_line <- function( } # close if ref_line NULL ref_lines_list } + +#------------------------------------------------------------------------------ + +#' Plot density distributions +#' +#' @param dat Data frame. Filtered data frame from standard output file(s) preformatted for +#' the target label from \link[stockplotr]{filter_data} +#' @param x String. Column name of data used to plot on the x-axis +#' +#' Default: "year" +#' @param y String. Column name of data used to plot on the y-axis +#' +#' Default: "estimate" +#' +#' @param geom String. Type of geom to use for plotting found in ggplot2 (e.g. "point", +#' "line", etc.). +#' +#' Default: "line" +#' +#' Options: "point" and "area" +#' @param xlab String. X-axis label +#' +#' Default: "Year" +#' @param ylab String. Y-axis label. If NULL, it will be set to the name +#' of `y`. +#' +#' Default: NULL +#' @param group String. Single column that groups the data. +#' Currently can only have one level of grouping. +#' +#' Default: NULL +#' Options: Including, but not limited to: "year", "area", "fleet", "sex", "none", NULL +#' +#' @param facet Character vector. Column name or names used for faceting +#' (e.g. "year", "area", etc.) +#' +#' Default: NULL +#' @param ... Inherited arguments from internal functions from ggplot2::geom_xx +#' +#' +#' @returns Create a time series plot for a stock assessment report. +#' @details The user can create a line, point, or area plot, where the x-axis is +#' year and y can vary for any time series quantity. Currently, grouping is +#' restricted to one group where faceting can be any number of facets. +#' +#' @export +#' +#' @examples +#' \dontrun{ +#' plot_density(dat, +#' x = "year", +#' y = "estimate", +#' geom = "line", +#' xlab = "Year", +#' ylab = "Biomass", +#' group = "fleet", +#' facet = "area" +#' ) +#' } +plot_density <- function( + dat, + x = "length_bins", + y = "age", + xlab = "Length bin", + ylab = "Age", + facet = NULL, + n_length_bins, + ... +) { + + # Start plot + plot <- ggplot2::ggplot() + + # Add geom + plot <- plot + + ggridges::geom_density_ridges( + data = dat, + ggplot2::aes( + x = as.numeric(.data[[x]]), + y = as.numeric(.data[[y]]), + weight = .data[["estimate"]], + group = .data[[y]], + color = .data[["model"]], + fill = .data[["model"]] + ), + stat = "binline", + bins = n_length_bins, + scale = 1.5, + alpha = 0.8, + ... + ) + + ggplot2::coord_flip() + + # Add labels to axis and legend + final <- plot + ggplot2::labs( + x = xlab, + y = ylab, + color = "Model", + fill = "Model" + ) + + # Remove legend if no group is selected + # TODO: determine why "model" not found in data + if (length(unique(dat$model)) == 1) { + final <- final + ggplot2::theme(legend.position = "none") + } + + # Check if facet(s) are desired + if (!is.null(facet) & length(facet) > 0) { + facet <- paste("~", paste(facet, collapse = " + ")) + facet_formula <- stats::reformulate(facet) + + final <- final + ggplot2::facet_wrap(facet_formula, labeller = function(labels) { + # Clean column names (e.g., "growth_pattern" -> "Growth Pattern") + names(labels) <- tools::toTitleCase(gsub("_", " ", names(labels))) + + # Combine into "Variable: Value" format + lapply(names(labels), function(var) paste0(var, ": ", labels[[var]])) + }) + } + final +} diff --git a/data/example_data.rda b/data/example_data.rda index 046324ed..53f8ffc5 100644 Binary files a/data/example_data.rda and b/data/example_data.rda differ diff --git a/data/ss3_simple.rda b/data/ss3_simple.rda index 91687838..de961080 100644 Binary files a/data/ss3_simple.rda and b/data/ss3_simple.rda differ diff --git a/inst/resources/captions_alt_text_template.csv b/inst/resources/captions_alt_text_template.csv index 8ef74d5f..ebf21ea1 100644 --- a/inst/resources/captions_alt_text_template.csv +++ b/inst/resources/captions_alt_text_template.csv @@ -47,7 +47,7 @@ naa,table,Historical population abundance (numbers) estimated by the base model catchability,table,Catchability over time for fleet catchability.fleet., sensitivity.runs,table,"Forecasted catch, biomass, spawning biomass, recruitment, fishing mortality, and stock status as projected from the base model configuration.", catch_at_age,figure,Fishery age composition (caa.start.year-caa.end.year). The area of the circle is proportional to the catch. Diagonal lines indicated the top 5\\% strongest year classes.,"Bubble plot showing estimated total catch at age. The x axis shows the year, which spans from caa.start.year to caa.end.year. The y axis shows age, which spans from caa.age.min to caa.age.max. The size of the bubbles range from tot.catch.min to tot.catch.max." -age_to_length_conversion,figure,"Age-to-length conversion shown as the probability distribution of length bins at each age. Density curves show the proportion of fish in each length bin at a given age, with wider curves indicating greater variability in length at age.","A set of density plots showing the age-to-length conversion for species, with one panel for each morph. The x axis shows age in years and the y axis shows length bins (in length.units). At each age, a vertical density curve shows the distribution of lengths. Mean length increases from about length.at.min.age length.units at age min.age to about length.at.max.age length.units at age max.age, after which the distributions stabilize, indicating that growth slows as fish age. " +age_to_length_conversion,figure,"Age-to-length conversion shown as the probability distribution of length bins at each age. Density curves show the proportion of fish in each length bin at a given age, with wider curves indicating greater variability in length at age.","A set of density plots showing the age-to-length conversion for species, with one panel for each age.length.group. The x axis shows age in years which spans from age.length.age.min to age.length.age.max and the y axis shows lengths from age.length.length.min to age.length.length.max (in length.units). At each age, a vertical density curve shows the distribution of lengths. mean.length.description" data_availability,figure,"Years with data for each fleet, by type of data (data.availability.types).","Grid of squares showing the years in which each fleet has each type of data. The x axis shows the year, which spans from data.availability.start.year to data.availability.end.year. The panels show the types of data (data.availability.types), and each row within a panel is one of data.availability.n.fleets fleets; a square marks a year with data." runs_test,figure,"Residuals of the fit to indices of abundance over time, with three-sigma control limits (shaded) and a runs test for non-random residuals. The shading is green when the residuals pass the runs test (p >= 0.05) and red when they fail, and points filled in red are outside the control limits. Residuals are the log of the observed index divided by the predicted index.","runs.test.panel.desc The x axis shows year, from runs.test.start.year to runs.test.end.year, and the y axis shows the residual, with a vertical line from zero to each residual. runs.test.summary" joint_residuals,figure,"joint.residuals.type of the fits to joint.residuals.index.list joint.residuals.layout.","The x axis shows year, from joint.residuals.start.year to joint.residuals.end.year, and the y axis shows the residual, with a vertical line from zero to each residual, colored by index. joint.residuals.summary" diff --git a/man/conout_bam.Rd b/man/conout_bam.Rd index 6cb63143..3cdb9be6 100644 --- a/man/conout_bam.Rd +++ b/man/conout_bam.Rd @@ -7,23 +7,19 @@ conout_bam(file, out_new, fleet_names = NULL) } \arguments{ -\item{file}{Path. Assessment model output file path. Currently accepts SS3 -\code{r4ss::ss_output()} or Report.sso file (being deprecated), BAM .rda file, Rceattle model object, and FIMS output -(the tibble returned by \code{FIMS::get_estimates()}, a \code{FIMSFit} object, or an .rds file with either).} - \item{out_new}{The empty standard output data frame from \code{\link[=convert_output]{convert_output()}}, which sets the columns of the output.} -\item{fleet_names}{List of Character vectors. Names of fleets in the assessment model as +\item{fleet_names}{List of named character vectors. Names of fleets in the assessment model as shortened in the output file or names of fleets to replace the native fleet names in the output. For example, if fleet names in the model are 1, 2, 3 then fleet_names = c("fleet1" = 1, "fleet2" = 2, "fleet3" = 3). Default: NULL -Should match length of \code{files}.} +Should match length of \code{files} if >1 model.} -\item{Path.}{Assessment model output file path. Currently accepts SS3 +\item{file.}{Assessment model output file path. Currently accepts SS3 Report.sso file, BAM .rda file, Rceattle model object, and FIMS model object.} } \value{ diff --git a/man/conout_rceattle.Rd b/man/conout_rceattle.Rd index 9e9e3734..7702fba6 100644 --- a/man/conout_rceattle.Rd +++ b/man/conout_rceattle.Rd @@ -7,23 +7,19 @@ conout_rceattle(file, out_new, fleet_names = NULL) } \arguments{ -\item{file}{Path. Assessment model output file path. Currently accepts SS3 -\code{r4ss::ss_output()} or Report.sso file (being deprecated), BAM .rda file, Rceattle model object, and FIMS output -(the tibble returned by \code{FIMS::get_estimates()}, a \code{FIMSFit} object, or an .rds file with either).} - \item{out_new}{The empty standard output data frame from \code{\link[=convert_output]{convert_output()}}, which sets the columns of the output.} -\item{fleet_names}{List of Character vectors. Names of fleets in the assessment model as +\item{fleet_names}{List of named character vectors. Names of fleets in the assessment model as shortened in the output file or names of fleets to replace the native fleet names in the output. For example, if fleet names in the model are 1, 2, 3 then fleet_names = c("fleet1" = 1, "fleet2" = 2, "fleet3" = 3). Default: NULL -Should match length of \code{files}.} +Should match length of \code{files} if >1 model.} -\item{Path.}{Assessment model output file path. Currently accepts SS3 +\item{file.}{Assessment model output file path. Currently accepts SS3 Report.sso file, BAM .rda file, Rceattle model object, and FIMS model object.} } \value{ diff --git a/man/conout_ss3.Rd b/man/conout_ss3.Rd index 8fdd5c43..25a79239 100644 --- a/man/conout_ss3.Rd +++ b/man/conout_ss3.Rd @@ -7,23 +7,19 @@ conout_ss3(file, out_new, fleet_names = NULL) } \arguments{ -\item{file}{Path. Assessment model output file path. Currently accepts SS3 -\code{r4ss::ss_output()} or Report.sso file (being deprecated), BAM .rda file, Rceattle model object, and FIMS output -(the tibble returned by \code{FIMS::get_estimates()}, a \code{FIMSFit} object, or an .rds file with either).} - \item{out_new}{The empty standard output data frame from \code{\link[=convert_output]{convert_output()}}, which sets the columns of the output.} -\item{fleet_names}{List of Character vectors. Names of fleets in the assessment model as +\item{fleet_names}{List of named character vectors. Names of fleets in the assessment model as shortened in the output file or names of fleets to replace the native fleet names in the output. For example, if fleet names in the model are 1, 2, 3 then fleet_names = c("fleet1" = 1, "fleet2" = 2, "fleet3" = 3). Default: NULL -Should match length of \code{files}.} +Should match length of \code{files} if >1 model.} -\item{Path.}{Assessment model output file path. Currently accepts SS3 +\item{file.}{Assessment model output file path. Currently accepts SS3 Report.sso file, BAM .rda file, Rceattle model object, and FIMS model object.} } \value{ diff --git a/man/convert_output.Rd b/man/convert_output.Rd index 30d010e4..71f61f17 100644 --- a/man/convert_output.Rd +++ b/man/convert_output.Rd @@ -4,13 +4,19 @@ \alias{convert_output} \title{Convert Output} \usage{ -convert_output(files, models = NULL, fleet_names = NULL, save_dir = NULL) +convert_output( + files, + models = NULL, + fleet_names = NULL, + save_dir = NULL, + start_year = NULL +) } \arguments{ \item{files}{List of Paths. Assessment model output file paths. Currently accepts SS3 \code{r4ss::ss_output()} or Report.sso file (being deprecated), BAM .rda file, Rceattle model object, -and FIMS output (the tibble returned by \code{FIMS::get_estimates()}, a \code{FIMSFit} object, or an -.rds file with either). If multiple elements, all valid models will be read, converted, and +and FIMS output (the tibble returned by \code{FIMS::get_estimates()}, a \code{FIMSFit} object, or an .rds +file with either). If multiple elements, all valid models will be read, converted, and appended together. \code{names(files)} will be used as the name of the model in the output.} @@ -21,16 +27,16 @@ Default: NULL Options: "ss3", "bam", "fims", "rceattle" -Should match length of \code{files}.} +Should match length of \code{files} if >1 model.} -\item{fleet_names}{List of Character vectors. Names of fleets in the assessment model as +\item{fleet_names}{List of named character vectors. Names of fleets in the assessment model as shortened in the output file or names of fleets to replace the native fleet names in the output. For example, if fleet names in the model are 1, 2, 3 then fleet_names = c("fleet1" = 1, "fleet2" = 2, "fleet3" = 3). Default: NULL -Should match length of \code{files}.} +Should match length of \code{files} if >1 model.} \item{save_dir}{Path. Full file path to save the converted output file. If saved file name is not indicated, the file will be saved as "std_output.rda" diff --git a/man/plot_age_to_length_conversion.Rd b/man/plot_age_to_length_conversion.Rd new file mode 100644 index 00000000..4c3256e6 --- /dev/null +++ b/man/plot_age_to_length_conversion.Rd @@ -0,0 +1,79 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/plot_age_to_length.R +\name{plot_age_to_length_conversion} +\alias{plot_age_to_length_conversion} +\title{Plot Age-to-Length Conversion} +\usage{ +plot_age_to_length_conversion( + dat, + group = NULL, + facet = NULL, + module = NULL, + unit_label = "cm", + make_rda = FALSE, + figures_dir = getwd(), + interactive = TRUE, + ... +) +} +\arguments{ +\item{dat}{Data frame or list. A tibble or named list of tibbles (input as \code{list()}) +returned from \link[stockplotr]{convert_output}.} + +\item{group}{String. Single column that groups the data. + +Set group = "none" to summarize data over all indexing values. + +Default: NULL +Options: Including, but not limited to: "year", "area", "fleet", "sex", "none", NULL} + +\item{facet}{Character vector. Column name or names used for faceting. + +Default: NULL} + +\item{module}{Character vector. (Optional) Module name found in \code{dat$module_name}. +If selecting >1 module, place them in a vector like c("module1", "module2"). + +Default: NULL + +If the interactive and >1 module_name is found, user will select the +module_name in the console. @seealso \code{\link[=filter_data]{filter_data()}}} + +\item{unit_label}{String. Length unit as indicated from the data. + +Default: "cm"} + +\item{make_rda}{Logical. TRUE/FALSE; indicate whether to save the object and +make an automated caption and alternative text in the form of an \code{rda} object. If TRUE, +the rda will be exported to the folder indicated in the argument "figures_dir". + +Default: \code{FALSE}.} + +\item{figures_dir}{Path. Path to the "figures" folder. + +Default: \code{getwd()} + +The folder is created within the path if it does not exist.} + +\item{interactive}{Logical. TRUE/FALSE; indicate whether the environment is interactive. + +Default: \code{FALSE}} + +\item{...}{Arguments called from \link[ggplot2]{geom_line} or \link[ggplot2]{geom_point}} +} +\value{ +A ggplot object. +} +\description{ +Creates a density plot of age-to-length conversion, either fixed input or +realized growth-derived output. +} +\examples{ +\dontrun{ +plot_age_to_length_conversion( + dat = your_data, + unit_label = "cm" +) +} + +} diff --git a/man/plot_density.Rd b/man/plot_density.Rd new file mode 100644 index 00000000..18cd3e3f --- /dev/null +++ b/man/plot_density.Rd @@ -0,0 +1,82 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/utils_plot.R +\name{plot_density} +\alias{plot_density} +\title{Plot density distributions} +\usage{ +plot_density( + dat, + x = "length_bins", + y = "age", + xlab = "Length bin", + ylab = "Age", + facet = NULL, + n_length_bins, + ... +) +} +\arguments{ +\item{dat}{Data frame. Filtered data frame from standard output file(s) preformatted for +the target label from \link[stockplotr]{filter_data}} + +\item{x}{String. Column name of data used to plot on the x-axis + +Default: "year"} + +\item{y}{String. Column name of data used to plot on the y-axis + +Default: "estimate"} + +\item{xlab}{String. X-axis label + +Default: "Year"} + +\item{ylab}{String. Y-axis label. If NULL, it will be set to the name +of \code{y}. + +Default: NULL} + +\item{facet}{Character vector. Column name or names used for faceting +(e.g. "year", "area", etc.) + +Default: NULL} + +\item{...}{Inherited arguments from internal functions from ggplot2::geom_xx} + +\item{geom}{String. Type of geom to use for plotting found in ggplot2 (e.g. "point", +"line", etc.). + +Default: "line" + +Options: "point" and "area"} + +\item{group}{String. Single column that groups the data. +Currently can only have one level of grouping. + +Default: NULL +Options: Including, but not limited to: "year", "area", "fleet", "sex", "none", NULL} +} +\value{ +Create a time series plot for a stock assessment report. +} +\description{ +Plot density distributions +} +\details{ +The user can create a line, point, or area plot, where the x-axis is +year and y can vary for any time series quantity. Currently, grouping is +restricted to one group where faceting can be any number of facets. +} +\examples{ +\dontrun{ +plot_density(dat, + x = "year", + y = "estimate", + geom = "line", + xlab = "Year", + ylab = "Biomass", + group = "fleet", + facet = "area" +) +} +} diff --git a/tests/testthat/test-plot-age-to-length.R b/tests/testthat/test-plot-age-to-length.R new file mode 100644 index 00000000..d18f4357 --- /dev/null +++ b/tests/testthat/test-plot-age-to-length.R @@ -0,0 +1,42 @@ +test_that("plot_age_to_length_conversion generates plots without errors", { + # expect error-free plot with minimal arguments + expect_no_error( + plot_age_to_length_conversion( + stockplotr::ss3_simple, + interactive = FALSE + ) + ) + + # expect error-free plot with many arguments + expect_no_error( + plot_age_to_length_conversion( + stockplotr::ss3_simple, + group = NULL, + facet = NULL, + interactive = FALSE, + make_rda = FALSE, + figures_dir = getwd() + ) + ) + + expect_no_error( + plot_age_to_length_conversion( + stockplotr::ss3_simple |> + dplyr::filter(age < 10), + group = NULL, + facet = NULL, + interactive = FALSE, + make_rda = FALSE, + figures_dir = getwd() + ) + ) + + # expect ggplot object is returned + expect_s3_class( + plot_age_to_length_conversion( + stockplotr::ss3_simple, + interactive = FALSE + ), + "ggplot" + ) +})