Skip to content
1 change: 1 addition & 0 deletions R/plot_biomass_at_age.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ plot_biomass_at_age <- function(
label_name = "^biomass",
geom = "point",
group = "age",
era = "time",
scale_amount = scale_amount,
interactive = interactive
)
Expand Down
2 changes: 1 addition & 1 deletion R/plot_fishing_mortality.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ plot_fishing_mortality <- function(
group = NULL,
facet = NULL,
ref_line = "msy",
era = "time",
era = NULL,
module = NULL,
make_rda = FALSE,
figures_dir = getwd(),
Expand Down
40 changes: 20 additions & 20 deletions R/plot_recruitment.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ plot_recruitment <- function(
dat,
unit_label = "mt",
scale_amount = 1,
era = "time",
era = NULL,
group = NULL,
facet = NULL,
# relative = FALSE,
Expand Down Expand Up @@ -129,25 +129,25 @@ plot_recruitment <- function(
}

# Plot vertical lines if era is not filtering
if (is.null(era)) {
# Find unique era
eras <- unique(filter_data$era)
if (length(eras) > 1) {
year_vlines <- c()
for (i in 2:length(eras)) {
erax <- filter_data |>
dplyr::filter(era == eras[i]) |>
dplyr::pull(year) |>
min(na.rm = TRUE)
year_vlines <- c(year_vlines, erax)
}
}
final <- final +
ggplot2::geom_vline(
xintercept = year_vlines,
color = "#999999"
)
}
# if (is.null(era)) {
# # Find unique era
# eras <- unique(filter_data$era)
# if (length(eras) > 1) {
# year_vlines <- c()
# for (i in 2:length(eras)) {
# erax <- filter_data |>
# dplyr::filter(era == eras[i]) |>
# dplyr::pull(year) |>
# min(na.rm = TRUE)
# year_vlines <- c(year_vlines, erax)
# }
# }
# final <- final +
# ggplot2::geom_vline(
# xintercept = year_vlines,
# color = "#999999"
# )
# }

# Make RDA
if (make_rda) {
Expand Down
48 changes: 24 additions & 24 deletions R/plot_recruitment_deviations.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
plot_recruitment_deviations <- function(
dat,
module = NULL,
era = "time",
era = NULL,
interactive = TRUE,
make_rda = FALSE,
figures_dir = getwd(),
Expand Down Expand Up @@ -78,29 +78,29 @@ plot_recruitment_deviations <- function(
theme_noaa()

# Plot vertical lines if era is not filtering
if (is.null(era)) {
# Find unique era
eras <- unique(filter_data$era)
if (length(eras) > 1) {
# era1 <- filter_data |>
# dplyr::filter(era == eras[1]) |>
# dplyr::pull(year) |>
# max(na.rm = TRUE)
year_vlines <- c()
for (i in 2:length(eras)) {
erax <- filter_data |>
dplyr::filter(era == eras[i]) |>
dplyr::pull(year) |>
min(na.rm = TRUE)
year_vlines <- c(year_vlines, erax)
}
}
final <- final +
ggplot2::geom_vline(
xintercept = year_vlines,
color = "#999999"
)
}
# if (is.null(era)) {
# # Find unique era
# eras <- unique(filter_data$era)
# if (length(eras) > 1) {
# # era1 <- filter_data |>
# # dplyr::filter(era == eras[1]) |>
# # dplyr::pull(year) |>
# # max(na.rm = TRUE)
# year_vlines <- c()
# for (i in 2:length(eras)) {
# erax <- filter_data |>
# dplyr::filter(era == eras[i]) |>
# dplyr::pull(year) |>
# min(na.rm = TRUE)
# year_vlines <- c(year_vlines, erax)
# }
# }
# final <- final +
# ggplot2::geom_vline(
# xintercept = year_vlines,
# color = "#999999"
# )
# }

# Make RDA
if (make_rda) {
Expand Down
2 changes: 1 addition & 1 deletion R/plot_spawning_biomass.R
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ plot_spawning_biomass <- function(
) + theme_noaa()
}
}

### Make RDA ----
if (make_rda) {
if (relative) {
Expand Down
68 changes: 52 additions & 16 deletions R/plot_stock_recruitment.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@
#' interactive = FALSE,
#' spawning_biomass_label = "metric tons",
#' recruitment_label = "metric tons",
#' module = "SPAWN_RECRUIT"
#' module = "DERIVED_QUANTITIES"
#' )
plot_stock_recruitment <- function(
dat,
spawning_biomass_label = "mt",
recruitment_label = "mt",
interactive = TRUE,
# era = "time",
era = NULL,
module = NULL,
scale_amount = 1,
make_rda = FALSE,
Expand All @@ -42,45 +42,80 @@ plot_stock_recruitment <- function(
# Extract recruitment
recruitment <- filter_data(
dat = dat,
# TODO: change string to ^recruitment in naming convention change PR
label_name = "recruitment",
era = "time",
era = era,
geom = "point",
scale_amount = scale_amount,
interactive = interactive,
module = module
) |>
# filter for year !na
dplyr::filter(
!is.na(year),
# filter out rec devs if in data
!grepl("deviations", label)
)

process_rec <- process_data(
recruitment
)

rec_proc <- process_rec[[1]]
group <- process_rec[[2]]
facet <- process_rec[[3]]

if (length(unique(recruitment$label)) > 1) {
recruitment <- recruitment |>
rec_proc <- rec_proc |>
tidyr::pivot_wider(
id_cols = c(year, model, group_var, estimate_lower, estimate_upper),
id_cols = dplyr::any_of(c("year", "model", "group_var", facet)),
names_from = label,
values_from = estimate
values_from = c(estimate, estimate_lower, estimate_upper)
)
# rename columns to remove "estimate"
colnames(rec_proc) <- gsub("estimate_", "", colnames(rec_proc))
} else {
recruitment <- recruitment |>
dplyr::rename(predicted_recruitment = estimate) |>
dplyr::select(-c(label))
rec_proc <- rec_proc|>
dplyr::rename(
predicted_recruitment = estimate,
lower_predicted_recruitment = estimate_lower,
upper_predicted_recruitment = estimate_upper
) |>
dplyr::select(-label)
}

if (any(grepl("^recruitment$", colnames(recruitment)))) {
recruitment <- dplyr::rename(recruitment, predicted_recruitment = recruitment)
}
# if (any(grepl("^recruitment$", colnames(recruitment)))) {
# # TODO: adjust naming to recruitment_predicted in naming convention change PR
# recruitment <- dplyr::rename(recruitment, predicted_recruitment = recruitment)
# }

# Extract spawning biomass
sb <- filter_data(
dat = dat,
label_name = "spawning biomass",
geom = "point",
era = "time",
era = era,
scale_amount = scale_amount,
interactive = interactive,
module = module
) |>
dplyr::rename(spawning_biomass = estimate) |>
dplyr::select(-c(label))
dplyr::filter(!is.na(year))

process_sb <- process_data(
sb
)
sb_proc <- process_sb[[1]] |>
dplyr::rename(
spawning_biomass = estimate,
lower_spawning_biomass = estimate_lower,
upper_spawning_biomass = estimate_upper
) |>
dplyr::select(-label)
# group <- process_sb[[2]]
# facet <- process_sb[[3]]

# Merge recruitment and spawning biomass data
sr <- dplyr::left_join(sb, recruitment)
sr <- dplyr::left_join(sb_proc, rec_proc, by = c("year", "model", "group_var"))

# Labs
recruitment_lab <- label_magnitude(
Expand All @@ -100,6 +135,7 @@ plot_stock_recruitment <- function(
final <- plot_timeseries(
dat = sr,
x = "spawning_biomass",
# TODO: change name to recruitment_predicted in naming convention change PR
y = "predicted_recruitment",
geom = "point",
color = "black",
Expand Down
2 changes: 1 addition & 1 deletion R/save_all_plots.R
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ save_all_plots <- function(
cli::cli_alert_danger("plot_stock_recruitment failed to run.")
cli::cli_alert("Tip: check that your arguments are correct.")
cli::cli_li("spawning_biomass_label = {spawning_biomass_label}")
cli::cli_li("recruitment_label = {recruitment_label}")
cli::cli_li("recruitment_label = {recruitment_unit_label}")
print(e)
}
)
Expand Down
2 changes: 1 addition & 1 deletion R/utils_plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,7 @@ filter_data <- function(
dat,
label_name,
module = NULL,
era = "time",
era = NULL,
geom,
group = NULL,
facet = NULL,
Expand Down
2 changes: 1 addition & 1 deletion data-raw/DATASET.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
## code to prepare `DATASET` dataset goes here
# Path to SS3 output file
EX_REPORT_PATH <- file.path("inst", "extdata", "Report.sso")
# Install asar
# Install stockplotr
devtools::install_github("nmfs-ost/stockplotr")
# Convert output
example_data <- stockplotr::convert_output(EX_REPORT_PATH)
Expand Down
2 changes: 1 addition & 1 deletion man/filter_data.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/plot_fishing_mortality.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/plot_recruitment.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/plot_recruitment_deviations.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion man/plot_stock_recruitment.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
8 changes: 4 additions & 4 deletions tests/testthat/test-export_rda.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ test_that("export_rda works for figures", {
)

# make a simple plot
library(ggplot2)
final <- ggplot2::ggplot(Orange, aes(circumference, age)) +
# library(ggplot2)
final <- ggplot2::ggplot(Orange, ggplot2::aes(circumference, age)) +
ggplot2::geom_point()

# export rda
Expand All @@ -43,7 +43,7 @@ test_that("export_rda works for figures", {

# erase temporary testing files
file.remove(fs::path(getwd(), "captions_alt_text.csv"))
file.remove(fs::path(getwd(), "key_quantities.csv"))
# file.remove(fs::path(getwd(), "key_quantities.csv")) # file is not made in this test
unlink(fs::path(getwd(), "figures"), recursive = T)
})

Expand Down Expand Up @@ -85,6 +85,6 @@ test_that("export_rda works for tables", {

# erase temporary testing files
file.remove(fs::path(getwd(), "captions_alt_text.csv"))
file.remove(fs::path(getwd(), "key_quantities.csv"))
# file.remove(fs::path(getwd(), "key_quantities.csv")) # file is not made in this test ?
unlink(fs::path(getwd(), "tables"), recursive = T)
})
1 change: 0 additions & 1 deletion tests/testthat/test-plot_landings.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ test_that("plot_landings generates plots without errors", {
test_that("rda file made when indicated", {
# export rda
plot_landings(out_new,
end_year = 2024,
make_rda = TRUE,
unit_label = "metric tons",
figures_dir = getwd()
Expand Down
Loading