Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extracting numeric (key) quantities #29

Open
wants to merge 39 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
13b10ff
Wrote code to extract numeric quantities for values associated with b…
sbreitbart-NOAA Nov 27, 2024
c429faa
Wrote code to extract numeric quantities for values associated with r…
sbreitbart-NOAA Dec 2, 2024
6b591d1
removed "div" from Kobe plot object names; shortened est stock recrui…
sbreitbart-NOAA Dec 2, 2024
18d3a2c
Wrote code to extract numeric quantities for values associated with b…
sbreitbart-NOAA Nov 27, 2024
6fe9e26
Wrote code to extract numeric quantities for values associated with r…
sbreitbart-NOAA Dec 2, 2024
f52c894
removed "div" from Kobe plot object names; shortened est stock recrui…
sbreitbart-NOAA Dec 2, 2024
e46e85e
Merge branch 'key-quantities' of https://github.com/nmfs-ost/satf int…
sbreitbart-NOAA Dec 3, 2024
60004af
replacing repeated string with internal data
sbreitbart-NOAA Dec 3, 2024
03ff4c9
update(write_captions):
sbreitbart-NOAA Dec 10, 2024
403751e
update(utils): updated file.path() --> fs:::path()
sbreitbart-NOAA Dec 10, 2024
1cb2682
fixed documentation for multiple plotting fxns, and updated associate…
sbreitbart-NOAA Dec 10, 2024
f75554f
Merge branch 'master' into key-quantities
sbreitbart-NOAA Dec 10, 2024
c652a4d
feature(plot_recruitment): add option to append relative recruitment …
Schiano-NOAA Dec 11, 2024
a491edb
update(write_captions):
sbreitbart-NOAA Dec 12, 2024
284aee5
removed make_rda param from exp_all_figs_tables.R and updated documen…
sbreitbart-NOAA Dec 12, 2024
f5952b5
Merge pull request #33 from nmfs-ost/update-rda
sbreitbart-NOAA Dec 12, 2024
6e4a3de
feature(plot_biomass, plot_spawning_biomass): add option to append re…
sbreitbart-NOAA Dec 13, 2024
12d369d
Updating captions and alt text with quantities for relative B, SSB, r…
sbreitbart-NOAA Dec 16, 2024
d23331e
-updated write_captions quantities to be numeric and round to nearest…
sbreitbart-NOAA Dec 16, 2024
6eac0bd
update documentation
sbreitbart-NOAA Dec 16, 2024
a92e893
fix(b and sb plots): reformat plots so they still fxn even missing th…
Schiano-NOAA Dec 16, 2024
91a77c3
feature(plot_biomass): add annotation for reference line
Schiano-NOAA Dec 17, 2024
bf38ef8
fix double + in plot_biomass plot
Schiano-NOAA Dec 17, 2024
742c212
Merge pull request #34 from nmfs-ost/fix-b-sb-lines
Schiano-NOAA Dec 17, 2024
094ac58
Wrote code to extract numeric quantities for values associated with b…
sbreitbart-NOAA Nov 27, 2024
c73caa5
Wrote code to extract numeric quantities for values associated with r…
sbreitbart-NOAA Dec 2, 2024
69fcf9a
removed "div" from Kobe plot object names; shortened est stock recrui…
sbreitbart-NOAA Dec 2, 2024
884446a
replacing repeated string with internal data
sbreitbart-NOAA Dec 3, 2024
9574048
update(write_captions):
sbreitbart-NOAA Dec 10, 2024
f36fff3
update(utils): updated file.path() --> fs:::path()
sbreitbart-NOAA Dec 10, 2024
97feaba
fixed documentation for multiple plotting fxns, and updated associate…
sbreitbart-NOAA Dec 10, 2024
37eb7c2
feature(plot_recruitment): add option to append relative recruitment …
Schiano-NOAA Dec 11, 2024
7b020fc
update(write_captions):
sbreitbart-NOAA Dec 12, 2024
ce8a5e3
feature(plot_biomass, plot_spawning_biomass): add option to append re…
sbreitbart-NOAA Dec 13, 2024
9371371
Updating captions and alt text with quantities for relative B, SSB, r…
sbreitbart-NOAA Dec 16, 2024
27ca83b
-updated write_captions quantities to be numeric and round to nearest…
sbreitbart-NOAA Dec 16, 2024
8978341
Merge branch 'key-quantities' of https://github.com/nmfs-ost/satf int…
sbreitbart-NOAA Dec 17, 2024
dc2cc92
update(write_captions):
sbreitbart-NOAA Dec 17, 2024
dc17837
Changed underscores to periods in key quantities to avoid issues with…
sbreitbart-NOAA Dec 18, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion R/exp_all_figs_tables.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ exp_all_figs_tables <- function(
end_year = NULL,
n_projected_years = 10,
relative = FALSE,
make_rda = FALSE,
rda_dir = getwd(),

# imported from plot_biomass
Expand All @@ -53,6 +52,8 @@ exp_all_figs_tables <- function(

) {

make_rda = TRUE
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you take this out of the arguments?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because if the user is using this function (to export all figs/tables), that implies that all figures and tables will be exported as rda files. So, make_rda would always be true.


# figures

satf::plot_recruitment(dat,
Expand Down
65 changes: 50 additions & 15 deletions R/plot_biomass.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
#' lower-case letters but you must use one of the options specified in the
#' default list to ensure that the label on the figure looks correct
#' regardless of how it is specified in `dat`.
#' @param ref_point A known value of the reference point along with the label
#' for the reference point as specified in the output file. Please use this
#' option if the ref_line cannot find your desired point. Indicate the
#' reference point in the form c("label" = value).
#' @return Plot total biomass from a stock assessment model as found in a NOAA
#' stock assessment report. Units of total biomass can either be manually added
#' or will be extracted from the provided file if possible. In later releases, model will not
Expand All @@ -18,14 +22,17 @@ plot_biomass <- function(
unit_label = "metric tons",
scale_amount = 1,
ref_line = c("target", "MSY", "msy", "unfished"),
ref_point = NULL,
end_year = NULL,
relative = FALSE,
make_rda = FALSE,
rda_dir = getwd()
){

if(length(ref_line)>1){
ref_line = "target"
if (!is.null(ref_point)) {
ref_line <- names(ref_point)
} else if(length(ref_line)>1){
ref_line <- "target"
} else {
ref_line <- match.arg(ref_line, several.ok = FALSE)
}
Expand All @@ -39,22 +46,45 @@ plot_biomass <- function(
# Select value for reference line and label
# update the target option later
# TODO: add option to indicate the reference pt
ref_line_val <- as.numeric(dat[
grep(
pattern = glue::glue("^biomass.*{tolower(ref_line)}$"),
x = dat[["label"]]
),
"estimate"
])
if (!is.null(ref_point)) {
ref_line_val <- as.numeric(ref_point)
} else {
if ( inherits( try( solve(as.numeric(dat[
grep(
pattern = glue::glue("^biomass.*{tolower(ref_line)}$"),
x = dat[["label"]]
),
"estimate"
])), silent = TRUE), "try-error")) {
ref_line_val <- NULL
} else {
ref_line_val <- as.numeric(dat[
grep(
pattern = glue::glue("^biomass.*{tolower(ref_line)}$"),
x = dat[["label"]]
),
"estimate"
])
}
# ref_line_val <- as.numeric(dat[
# grep(
# pattern = glue::glue("^biomass.*{tolower(ref_line)}$"),
# x = dat[["label"]]
# ),
# "estimate"
# ])
}

if (length(ref_line_val) == 0) {
stop(glue::glue(
warning(glue::glue(
"The resulting reference value of `biomass_{ref_line}` was
not found in `dat[[\"label\"]]`."
))
warning("Reference line will not be plotted on the figure.")
} else if (length(ref_line_val) > 1) {
warning(glue::glue(
"More than one of the resulting reference value of 'biomass_{ref_line}` was
not in `dat[[\"label\"]]`."
not in `dat[[\"label\"]]`. \n Both reference points will be plotted on the figure."
))
}

Expand Down Expand Up @@ -99,15 +129,20 @@ plot_biomass <- function(
ymax = estimate_upper),
colour = "grey",
alpha = 0.3) +
ggplot2::geom_hline(
yintercept = ref_line_val / ifelse(relative, ref_line_val, scale_amount),
linetype = 2) +
{if(!is.null(ref_line_val)) ggplot2::geom_hline(yintercept = ref_line_val / ifelse(relative, ref_line_val, scale_amount),linetype = 2)} +
ggplot2::labs(
x = "Year",
y = biomass_label) +
ggplot2::scale_x_continuous(
n.breaks = x_n_breaks,
guide = ggplot2::guide_axis(minor.ticks = TRUE))
guide = ggplot2::guide_axis(minor.ticks = TRUE)) +
ggplot2::annotate(
geom = "text",
x = end_year + 0.05,
y = ref_line_val / ifelse(relative, ref_line_val, scale_amount),
label = list(bquote(B[.(ref_line)])),
parse = TRUE
)

plt_fin <- add_theme(plt)

Expand Down
62 changes: 46 additions & 16 deletions R/plot_spawning_biomass.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
#' lower-case letters but you must use one of the options specified in the
#' default list to ensure that the label on the figure looks correct
#' regardless of how it is specified in `dat`.
#' @param ref_point A known value of the reference point along with the label
#' for the reference point as specified in the output file. Please use this
#' option if the ref_line cannot find your desired point. Indicate the
#' reference point in the form c("label" = value).
#' @return
#' Plot spawning biomass from the results of an assessment model translated to
#' the standard output. The {ggplot2} object is returned for further
Expand All @@ -22,13 +26,20 @@ plot_spawning_biomass <- function(
unit_label = "metric ton",
scale_amount = 1,
ref_line = c("target", "unfished", "msy"),
ref_point = NULL,
end_year = NULL,
relative = FALSE,
n_projected_years = 10,
make_rda = FALSE,
rda_dir = getwd()
) {
ref_line <- match.arg(ref_line)
if (!is.null(ref_point)) {
ref_line <- names(ref_point)
} else if(length(ref_line)>1){
ref_line <- "target"
} else {
ref_line <- match.arg(ref_line, several.ok = FALSE)
}
# TODO: Fix the unit label if scaling. Maybe this is up to the user to do if
# they want something scaled then they have to supply a better unit name
# or we create a helper function to do this.
Expand All @@ -49,22 +60,44 @@ plot_spawning_biomass <- function(
# 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)}"),
x = dat[["label"]]
),
"estimate"
])
if (!is.null(ref_point)) {
ref_line_val <- as.numeric(ref_point)
} else {
if ( inherits( try( solve(as.numeric(dat[
grep(
pattern = glue::glue("^spawning_biomass.*{tolower(ref_line)}$"),
x = dat[["label"]]
),
"estimate"
])), silent = TRUE), "try-error")) {
ref_line_val <- NULL
} else {
ref_line_val <- as.numeric(dat[
grep(
pattern = glue::glue("^spawning_biomass.*{tolower(ref_line)}$"),
x = dat[["label"]]
),
"estimate"
])
}
# ref_line_val <- as.numeric(dat[
# grep(
# pattern = glue::glue("^biomass.*{tolower(ref_line)}$"),
# x = dat[["label"]]
# ),
# "estimate"
# ])
}
if (length(ref_line_val) == 0) {
stop(glue::glue(
warning(glue::glue(
"The resulting reference value of `spawning_biomass_{ref_line}` was
not found in `dat[[\"label\"]]`."
))
} else if (length(ref_line_val > 1)) {
warning("Reference line will not be plotted on the figure.")
} 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\"]]`."
"More than one of the resulting reference value of 'spawing_biomass_{ref_line}` was
not in `dat[[\"label\"]]`. \n Both reference points will be plotted on the figure."
))
}
sb <- dat |>
Expand Down Expand Up @@ -99,10 +132,7 @@ plot_spawning_biomass <- function(
),
linewidth = 1
) +
ggplot2::geom_hline(
yintercept = ref_line_val / ifelse(relative, ref_line_val, scale_amount),
linetype = 2
) +
{if(!is.null(ref_line_val)) ggplot2::geom_hline(yintercept = ref_line_val / ifelse(relative, ref_line_val, scale_amount),linetype = 2)} +
# Only add confidence intervals for the non NA estimates
# which allows for no warnings if uncertainty = NA
ggplot2::geom_ribbon(
Expand Down
5 changes: 1 addition & 4 deletions man/exp_all_figs_tables.Rd

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

Loading