Skip to content

Commit

Permalink
fix(bugs in fxns): minor adjustments to rec devs, SR, and SB plots
Browse files Browse the repository at this point in the history
  • Loading branch information
Schiano-NOAA committed Nov 15, 2024
1 parent 1f424ae commit 74aa50c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 20 deletions.
23 changes: 21 additions & 2 deletions R/plot_recruitment_deviations.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ plot_recruitment_deviations <- function (
} else {
end_year <- end_year
}
stryr <- min(as.numeric(dat$year), na.rm = TRUE)
start_year <- min(as.numeric(dat$year), na.rm = TRUE)

rec_devs <- dat |>
dplyr::filter(label == "recruitment_deviations" | label == "log_recruitment_deviations",
Expand All @@ -39,9 +39,22 @@ plot_recruitment_deviations <- function (
stop("No recruitment deviations found in data.")
}

# change plot breaks
x_n_breaks <- round(length(rec_devs[["year"]]) / 10)
if (x_n_breaks <= 5) {
x_n_breaks <- round(length(rec_devs[["year"]]) / 5)
} else if (x_n_breaks > 10) {
x_n_breaks <- round(length(rec_devs[["year"]]) / 15)
}

# Plot
plt <- ggplot2::ggplot(data = rec_devs) +
ggplot2::geom_point(ggplot2::aes(x = year, y = estimate), shape = 1, size = 2.5) +
ggplot2::geom_point(
ggplot2::aes(
x = year,
y = estimate),
shape = 1,
size = 2.5) +
# ggplot2::geom_pointrange(
# ggplot2::aes(
# x = year,
Expand All @@ -59,6 +72,12 @@ plot_recruitment_deviations <- function (
ggplot2::labs(
x = "Year",
y = "Recruitment Deviations"
) +
ggplot2::scale_x_continuous(
n.breaks = x_n_breaks,
guide = ggplot2::guide_axis(
minor.ticks = TRUE
)
)
suppressWarnings(add_theme(plt))
}
34 changes: 17 additions & 17 deletions R/plot_spawn_recruitment.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,9 @@ plot_spawn_recruitment <- function(
recruitment_label = "metric tons",
end_year = NULL
) {
if (is.null(end_year)){
endyr <- max(rec$year)
} else {
endyr <- end_year
}
stryr <- min(rec$year)

# Extract spawning biomass
sb <- output |>
dplyr::filter(label == "spawning_biomass",
# Extract recruitment
rec <- dat |>
dplyr::filter(label == "recruitment",
module_name == "TIME_SERIES" | module_name == "t.series",
!is.na(year),
is.na(fleet) | length(unique(fleet)) <= 1,
Expand All @@ -33,12 +26,19 @@ plot_spawn_recruitment <- function(
) |> # SS3 and BAM target module names
dplyr::mutate(estimate = as.numeric(estimate),
year = as.numeric(year)) |>
dplyr::rename(spawning_biomass = estimate) |>
dplyr::rename(recruitment = estimate) |>
dplyr::select(-c(module_name, label))

# Extract recruitment
rec <- dat |>
dplyr::filter(label == "recruitment",
if (is.null(end_year)){
endyr <- max(rec$year)
} else {
endyr <- end_year
}
stryr <- min(rec$year)

# Extract spawning biomass
sb <- dat |>
dplyr::filter(label == "spawning_biomass",
module_name == "TIME_SERIES" | module_name == "t.series",
!is.na(year),
is.na(fleet) | length(unique(fleet)) <= 1,
Expand All @@ -49,17 +49,17 @@ plot_spawn_recruitment <- function(
) |> # SS3 and BAM target module names
dplyr::mutate(estimate = as.numeric(estimate),
year = as.numeric(year)) |>
dplyr::rename(recruitment = estimate) |>
dplyr::rename(spawning_biomass = estimate) |>
dplyr::select(-c(module_name, label))

# merge DF
sr <- dplyr::full_join(sb, rec)

# Plot
plt <- ggplot2::ggplot(data = sr) +
ggplot2::geom_line(ggplot2::aes(x = spawning_biomass, y = recruitment/1000), linewidth = 1) +
ggplot2::geom_point(ggplot2::aes(x = spawning_biomass, y = recruitment/1000)) +
ggplot2::labs(x = glue::glue("Spawning Biomass ({spawning_biomass_label})"),
y = glue::glue("Recruitment ({recruitment_label})")) +
ggplot2::theme(legend.position = "none")
suppressWarnings(add_theme(sr_plt))
suppressWarnings(add_theme(plt))
}
9 changes: 8 additions & 1 deletion R/plot_spawning_biomass.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ plot_spawning_biomass <- function(
dat,
unit_label = "metric ton",
scale_amount = 1,
ref_line = c("target", "unfished"),
ref_line = c("target", "unfished", "msy"),
end_year = NULL,
relative = FALSE,
n_projected_years = 10
Expand All @@ -45,6 +45,8 @@ plot_spawning_biomass <- function(
stopifnot(any(end_year >= all_years))

# Select value for reference line and label
# TODO: add case if ref_line not indicated or hard to find - find one of the
# options and set as ref_line
ref_line_val <- as.numeric(dat[
grep(
pattern = glue::glue("^spawning_biomass.*{tolower(ref_line)}"),
Expand All @@ -57,6 +59,11 @@ plot_spawning_biomass <- function(
"The resulting reference value of `spawning_biomass_{ref_line}` was
not found in `dat[[\"label\"]]`."
))
} else if (length(ref_line_val > 1)) {
warning(glue::glue(
"More than one of the resulting reference value of `spawning_biomass_{ref_line}` was
not in `dat[[\"label\"]]`."
))
}
sb <- dat |>
dplyr::filter(
Expand Down

0 comments on commit 74aa50c

Please sign in to comment.