From e912c0a6fa2603856bfbbfc1b8102fc2ed6facaa Mon Sep 17 00:00:00 2001 From: Sam Schiano <125507018+Schiano-NOAA@users.noreply.github.com> Date: Tue, 21 Jul 2026 15:50:50 -0400 Subject: [PATCH 01/14] add note for where editing will occur --- R/plot_spawning_biomass.R | 1 + 1 file changed, 1 insertion(+) diff --git a/R/plot_spawning_biomass.R b/R/plot_spawning_biomass.R index 33a67fca..486a5d09 100644 --- a/R/plot_spawning_biomass.R +++ b/R/plot_spawning_biomass.R @@ -228,6 +228,7 @@ plot_spawning_biomass <- function( ggplot2::geom_point(ggplot2::aes(x = min_year - 1, y = ref_point)) + # should I keep -1 or set as first year? theme_noaa() } else { + # add apply/purrr/or for loop for reference lines -- not just the first anymore final <- reference_line( plot = plt, dat = rp_dat, From c7acf3aa3113cf70dd97b5cbd2c8105ccbdc42d1 Mon Sep 17 00:00:00 2001 From: Sam Schiano <125507018+Schiano-NOAA@users.noreply.github.com> Date: Thu, 23 Jul 2026 17:09:11 -0400 Subject: [PATCH 02/14] update reference_line to use plus operator and remove calc ref point fron function; replace old ref line code with for loop for multiple lines --- R/plot_spawning_biomass.R | 85 +++++++++++++++++++++++++-------------- R/utils_plot.R | 69 ++++++++++++------------------- 2 files changed, 80 insertions(+), 74 deletions(-) diff --git a/R/plot_spawning_biomass.R b/R/plot_spawning_biomass.R index 486a5d09..5c7e8af1 100644 --- a/R/plot_spawning_biomass.R +++ b/R/plot_spawning_biomass.R @@ -146,11 +146,11 @@ plot_spawning_biomass <- function( } ) # Pull first df if in a list to find reference point - if (!is.data.frame(dat)) { - rp_dat <- dat[[1]] - } else { - rp_dat <- dat - } + # if (!is.data.frame(dat)) { + # rp_dat <- dat[[1]] + # } else { + # rp_dat <- dat + # } if (relative & scale_amount > 1) { cli::cli_alert_warning("Scale amount is not applicable when relative = TRUE. Resetting scale_amount to 1.") @@ -206,38 +206,61 @@ plot_spawning_biomass <- function( ... ) # Add reference line + # Conditions for ref line + # 1. all are comparing same value = msy, target, unfished... + # 2. custom input vector = c("sabe23_msy"=30, "sable25_msy"=40) -- user must indicate which model in label otherwise it will assign in order of dat + # 3. input vector of labels = c("msy", "target") # getting data set - an ifelse statement in the fxn wasn't working if (relative) { # don't add any reference line here and just add theme for final plot final <- plt + theme_noaa() } else { - if ("unfished" %in% c(names(ref_line), ref_line)) { - # find the minimum x axis value from the plot - min_year <- min <- ggplot2::ggplot_build(plt)[["data"]][[2]] |> - as.data.frame() |> - dplyr::pull(x) |> - min() |> - round(digits = 2) - # find the reference point value for unfished - ref_point <- calculate_reference_point( - dat = rp_dat, - reference_name = "spawing_biomass_unfished" - ) / scale_amount - # add point to plot and add theme - final <- plt + - ggplot2::geom_point(ggplot2::aes(x = min_year - 1, y = ref_point)) + # should I keep -1 or set as first year? - theme_noaa() - } else { - # add apply/purrr/or for loop for reference lines -- not just the first anymore - final <- reference_line( - plot = plt, - dat = rp_dat, - lbs = lbs, - label_name = "spawning_biomass", - reference = ref_line, - scale_amount = scale_amount - ) + theme_noaa() + plt2 <- plt + # Put into for loop and add lines sequentially to plt + for (i in 1:length(ref_line)) { + # find the reference point value + if (is.null(names(ref_line[i]))) { + ref_line_x <- calculate_reference_point( + dat = dat[[i]], + reference_name = i, + lbs = lbs + ) / scale_amount + ref_line_x <- setNames(ref_line_x, i) + } else { + ref_line_x <- ref_line[i] / scale_amount + } + + if ("unfished" %in% names(ref_line_x)) { + # find the minimum x axis value from the plot + min_year <- min <- ggplot2::ggplot_build(plt)[["data"]][[2]] |> # I think this was causing issues on linux + as.data.frame() |> + dplyr::pull(x) |> + min() |> + round(digits = 2) + # add point to plot and add theme + plt2 <- plt2 + + ggplot2::geom_point(ggplot2::aes(x = min_year - 1, y = ref_point)) + # should I keep -1 or set as first year? + theme_noaa() + } else { + # add apply/purrr/or for loop for reference lines -- not just the first anymore + plt2 <- plt2 + + reference_line( + label_name = names(dat)[i], #"spawning_biomass", + ref_line = ref_line_x, + scale_amount = 1 + ) + + # reference_line( + # plot = plt, + # dat = rp_dat, + # lbs = lbs, + # label_name = "spawning_biomass", + # reference = ref_line, + # scale_amount = scale_amount + # ) + theme_noaa() + } } + final <- plt2 + theme_noaa() } ### Make RDA ---- diff --git a/R/utils_plot.R b/R/utils_plot.R index 44817291..458fc20c 100644 --- a/R/utils_plot.R +++ b/R/utils_plot.R @@ -529,11 +529,9 @@ cohort_line <- function( #' Preformatted reference line #' #' @inheritParams plot_spawning_biomass -#' @param plot Plot object. A ggplot2 object where the reference line will be added -#' @param dat Data frame. Standard data frame where reference point should be extracted #' @param label_name String. Name of the quantity that users want to #' extract the reference point from -#' @param reference String of the reference point +#' @param ref_line String of the reference point #' #' Options: Including, but not limited to: "msy", "unfished", "target" #' @@ -546,49 +544,34 @@ cohort_line <- function( #' reference_line(dat, "biomass", "msy") #' } reference_line <- function( - plot, - dat, label_name, - reference, - scale_amount = 1, - lbs = FALSE + ref_line, + scale_amount = 1 ) { - if (!is.null(names(reference))) { - ref_line_val <- reference[[1]] - reference <- names(reference) - } else { - # calculate reference point value - ref_line_val <- calculate_reference_point( - dat = dat, - reference_name = glue::glue("{label_name}_{reference}"), - lbs = lbs - ) - } + # Extract reference line values and labels + ref_line_val <- ref_line[[1]] + reference <- names(ref_line) - # Add geom for ref line - if (is.null(ref_line_val)) { - plot - } else { - plot + - ggplot2::geom_hline( - # ggplot2::aes( - yintercept = ref_line_val / scale_amount, - # ), - color = "black", - linetype = "dashed" - ) + - ggplot2::annotate( - geom = "text", - # TODO: need to change this for general process - x = as.numeric(max(ggplot2::ggplot_build(plot)[["data"]][[2]][["x"]], na.rm = TRUE)), # - as.numeric(max(dat$year[dat$era == "time"], na.rm = TRUE))/200, - y = ref_line_val / scale_amount, - label = glue::glue("{stringr::str_replace_all(label_name, '_', '~')}[{reference}]"), # list(bquote(label_name[.(reference)])), - parse = TRUE, - hjust = 1, - vjust = 0, - size = 5 # this is not foolproof - ) - } + list( + # Add geom for ref line + ggplot2::geom_hline( + yintercept = ref_line_val / scale_amount, + color = "black", + linetype = "dashed" + ), + # add line annotation + ggplot2::annotate( + geom = "text", + # TODO: need to change this for general process + x = Inf, + y = ref_line_val / scale_amount, + label = glue::glue("{stringr::str_replace_all(label_name, '_', '~')}[{reference}]"), # list(bquote(label_name[.(reference)])), + parse = TRUE, + hjust = 1.05, # slight offset so text doesn't hit border + vjust = -0.5, #slightly above line + size = 5 # this is not foolproof + ) + ) } #------------------------------------------------------------------------------ From 12348bc510eb2ab0bf0f6ddf4ba0d53babab3058 Mon Sep 17 00:00:00 2001 From: Sam Schiano <125507018+Schiano-NOAA@users.noreply.github.com> Date: Fri, 24 Jul 2026 10:15:27 -0400 Subject: [PATCH 03/14] add ifelse statement for single model ref line label --- R/plot_spawning_biomass.R | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/R/plot_spawning_biomass.R b/R/plot_spawning_biomass.R index 5c7e8af1..ed4084a0 100644 --- a/R/plot_spawning_biomass.R +++ b/R/plot_spawning_biomass.R @@ -245,19 +245,11 @@ plot_spawning_biomass <- function( # add apply/purrr/or for loop for reference lines -- not just the first anymore plt2 <- plt2 + reference_line( - label_name = names(dat)[i], #"spawning_biomass", + # conditionally add label name + label_name = ifelse(length(names(ref_line)) == 1, "spawning_biomass", names(dat)[i]), #"spawning_biomass", ref_line = ref_line_x, scale_amount = 1 ) - - # reference_line( - # plot = plt, - # dat = rp_dat, - # lbs = lbs, - # label_name = "spawning_biomass", - # reference = ref_line, - # scale_amount = scale_amount - # ) + theme_noaa() } } final <- plt2 + theme_noaa() From ed04eef040d649448a77c663dfce4fd93ff88aeb Mon Sep 17 00:00:00 2001 From: Sam Schiano <125507018+Schiano-NOAA@users.noreply.github.com> Date: Fri, 24 Jul 2026 10:43:38 -0400 Subject: [PATCH 04/14] other adjustments to allow the use to make ref explicit or search for each model --- R/plot_spawning_biomass.R | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/R/plot_spawning_biomass.R b/R/plot_spawning_biomass.R index ed4084a0..b5780df5 100644 --- a/R/plot_spawning_biomass.R +++ b/R/plot_spawning_biomass.R @@ -145,12 +145,6 @@ plot_spawning_biomass <- function( ) } ) - # Pull first df if in a list to find reference point - # if (!is.data.frame(dat)) { - # rp_dat <- dat[[1]] - # } else { - # rp_dat <- dat - # } if (relative & scale_amount > 1) { cli::cli_alert_warning("Scale amount is not applicable when relative = TRUE. Resetting scale_amount to 1.") @@ -216,23 +210,26 @@ plot_spawning_biomass <- function( final <- plt + theme_noaa() } else { plt2 <- plt + # Check if length of ref_line = dat + # replicate value if not + if (length(ref_line) != length(dat)) ref_line <- rep(ref_line, length(dat)) # Put into for loop and add lines sequentially to plt for (i in 1:length(ref_line)) { # find the reference point value if (is.null(names(ref_line[i]))) { ref_line_x <- calculate_reference_point( dat = dat[[i]], - reference_name = i, + reference_name = glue::glue("spawning_biomass_{ref_line[i]}"), lbs = lbs ) / scale_amount - ref_line_x <- setNames(ref_line_x, i) + ref_line_x <- setNames(ref_line_x, ref_line[i]) } else { ref_line_x <- ref_line[i] / scale_amount } if ("unfished" %in% names(ref_line_x)) { # find the minimum x axis value from the plot - min_year <- min <- ggplot2::ggplot_build(plt)[["data"]][[2]] |> # I think this was causing issues on linux + min_year <- min <- ggplot2::ggplot_build(plt2)[["data"]][[2]] |> # I think this was causing issues on linux? as.data.frame() |> dplyr::pull(x) |> min() |> From 7b6170d9c58d3e82cd4b5e14d50c81073bf1e1f9 Mon Sep 17 00:00:00 2001 From: Sam Schiano <125507018+Schiano-NOAA@users.noreply.github.com> Date: Fri, 24 Jul 2026 11:11:02 -0400 Subject: [PATCH 05/14] update ref line fxn man --- man/reference_line.Rd | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/man/reference_line.Rd b/man/reference_line.Rd index fbd71ec4..72ac2bd7 100644 --- a/man/reference_line.Rd +++ b/man/reference_line.Rd @@ -4,29 +4,19 @@ \alias{reference_line} \title{Preformatted reference line} \usage{ -reference_line(plot, dat, label_name, reference, scale_amount = 1, lbs = FALSE) +reference_line(label_name, ref_line, scale_amount = 1) } \arguments{ -\item{plot}{Plot object. A ggplot2 object where the reference line will be added} - -\item{dat}{Data frame. Standard data frame where reference point should be extracted} - \item{label_name}{String. Name of the quantity that users want to extract the reference point from} -\item{reference}{String of the reference point +\item{ref_line}{String of the reference point Options: Including, but not limited to: "msy", "unfished", "target"} \item{scale_amount}{Number. A number to scale the y-axis values. Default: 1} - -\item{lbs}{Logical. TRUE/FALSE; indicate whether to convert the y-axis values from -kilograms to pounds. The default units match the default in the -unit_label argument - 'mt'. - -Default: `FALSE`} } \value{ A ggplot2 geom_hline object for a reference point that can be added From 711627cb59401d8605c14be867b75a6149f4d750 Mon Sep 17 00:00:00 2001 From: Sam Schiano <125507018+Schiano-NOAA@users.noreply.github.com> Date: Fri, 24 Jul 2026 11:33:52 -0400 Subject: [PATCH 06/14] make adjustment to reference lines for plot_biomass --- R/plot_biomass.R | 69 +++++++++++++++++++++++++-------------- R/plot_spawning_biomass.R | 2 +- 2 files changed, 45 insertions(+), 26 deletions(-) diff --git a/R/plot_biomass.R b/R/plot_biomass.R index 45dbf3f4..e12fddf4 100644 --- a/R/plot_biomass.R +++ b/R/plot_biomass.R @@ -141,37 +141,56 @@ plot_biomass <- function( ... ) # Add reference line + # Conditions for ref line + # 1. all are comparing same value = msy, target, unfished... + # 2. custom input vector = c("sable23_msy"=30, "sable25_msy"=40) -- user must indicate which model in label otherwise it will assign in order of dat + # 3. input vector of labels = c("msy", "target") # getting data set - an ifelse statement in the fxn wasn't working if (relative) { # don't add any reference line here and just add theme for final plot final <- plt + theme_noaa() } else { - if ("unfished" %in% c(names(ref_line), ref_line)) { - # find the minimum x axis value from the plot - min_year <- min <- ggplot2::ggplot_build(plt)[["data"]][[2]] |> - as.data.frame() |> - dplyr::pull(x) |> - min() |> - round(digits = 2) - # find the reference point value for unfished - ref_point <- calculate_reference_point( - dat = stockplotr::example_data, - reference_name = "biomass_unfished" - ) / scale_amount - # add point to plot and add theme - final <- plt + - ggplot2::geom_point(ggplot2::aes(x = min_year - 1, y = ref_point)) + # should I keep -1 or set as first year? - theme_noaa() - } else { - final <- reference_line( - plot = plt, - dat = rp_dat, - label_name = "biomass", - reference = ref_line, - scale_amount = scale_amount - ) + - theme_noaa() + plt2 <- plt + # Check if length of ref_line = dat + # replicate value if not + if (length(ref_line) != length(dat)) ref_line <- rep(ref_line, length(dat)) + # Put into for loop and add lines sequentially to plt + for (i in 1:length(ref_line)) { + # find the reference point value + if (is.null(names(ref_line[i]))) { + ref_line_x <- calculate_reference_point( + dat = dat[[i]], + reference_name = glue::glue("biomass_{ref_line[i]}"), + lbs = lbs + ) / scale_amount + ref_line_x <- setNames(ref_line_x, ref_line[i]) + } else { + ref_line_x <- ref_line[i] / scale_amount + } + + if ("unfished" %in% names(ref_line_x)) { + # find the minimum x axis value from the plot + min_year <- min <- ggplot2::ggplot_build(plt2)[["data"]][[2]] |> # I think this was causing issues on linux? + as.data.frame() |> + dplyr::pull(x) |> + min() |> + round(digits = 2) + # add point to plot and add theme + plt2 <- plt2 + + ggplot2::geom_point(ggplot2::aes(x = min_year - 1, y = ref_point)) + # should I keep -1 or set as first year? + theme_noaa() + } else { + # add apply/purrr/or for loop for reference lines -- not just the first anymore + plt2 <- plt2 + + reference_line( + # conditionally add label name + label_name = ifelse(length(names(ref_line)) == 1, "biomass", names(dat)[i]), #"spawning_biomass", + ref_line = ref_line_x, + scale_amount = 1 + ) + } } + final <- plt2 + theme_noaa() } ### Make RDA ---- diff --git a/R/plot_spawning_biomass.R b/R/plot_spawning_biomass.R index b5780df5..187ca92e 100644 --- a/R/plot_spawning_biomass.R +++ b/R/plot_spawning_biomass.R @@ -202,7 +202,7 @@ plot_spawning_biomass <- function( # Add reference line # Conditions for ref line # 1. all are comparing same value = msy, target, unfished... - # 2. custom input vector = c("sabe23_msy"=30, "sable25_msy"=40) -- user must indicate which model in label otherwise it will assign in order of dat + # 2. custom input vector = c("sable23_msy"=30, "sable25_msy"=40) -- user must indicate which model in label otherwise it will assign in order of dat # 3. input vector of labels = c("msy", "target") # getting data set - an ifelse statement in the fxn wasn't working if (relative) { From 98286c2238cc2b6ce2ca5b87975edc4374f11621 Mon Sep 17 00:00:00 2001 From: Sam Schiano <125507018+Schiano-NOAA@users.noreply.github.com> Date: Fri, 24 Jul 2026 11:36:41 -0400 Subject: [PATCH 07/14] add new ref line approach for plot F --- R/plot_fishing_mortality.R | 60 +++++++++++++++++++++++++++++++++----- 1 file changed, 52 insertions(+), 8 deletions(-) diff --git a/R/plot_fishing_mortality.R b/R/plot_fishing_mortality.R index 56b01401..aebda3ae 100644 --- a/R/plot_fishing_mortality.R +++ b/R/plot_fishing_mortality.R @@ -76,14 +76,58 @@ plot_fishing_mortality <- function( facet = facet, ... ) - # Add reference line and theme - final <- reference_line( - plot = plt, - dat = dat, - label_name = "fishing_mortality", - reference = ref_line, - scale_amount = 1 - ) + theme_noaa() + # Add reference line + # Conditions for ref line + # 1. all are comparing same value = msy, target, unfished... + # 2. custom input vector = c("sable23_msy"=30, "sable25_msy"=40) -- user must indicate which model in label otherwise it will assign in order of dat + # 3. input vector of labels = c("msy", "target") + # getting data set - an ifelse statement in the fxn wasn't working + if (relative) { + # don't add any reference line here and just add theme for final plot + final <- plt + theme_noaa() + } else { + plt2 <- plt + # Check if length of ref_line = dat + # replicate value if not + if (length(ref_line) != length(dat)) ref_line <- rep(ref_line, length(dat)) + # Put into for loop and add lines sequentially to plt + for (i in 1:length(ref_line)) { + # find the reference point value + if (is.null(names(ref_line[i]))) { + ref_line_x <- calculate_reference_point( + dat = dat[[i]], + reference_name = glue::glue("fishing_mortality_{ref_line[i]}"), + lbs = lbs + ) / scale_amount + ref_line_x <- setNames(ref_line_x, ref_line[i]) + } else { + ref_line_x <- ref_line[i] / scale_amount + } + + if ("unfished" %in% names(ref_line_x)) { + # find the minimum x axis value from the plot + min_year <- min <- ggplot2::ggplot_build(plt2)[["data"]][[2]] |> # I think this was causing issues on linux? + as.data.frame() |> + dplyr::pull(x) |> + min() |> + round(digits = 2) + # add point to plot and add theme + plt2 <- plt2 + + ggplot2::geom_point(ggplot2::aes(x = min_year - 1, y = ref_point)) + # should I keep -1 or set as first year? + theme_noaa() + } else { + # add apply/purrr/or for loop for reference lines -- not just the first anymore + plt2 <- plt2 + + reference_line( + # conditionally add label name + label_name = ifelse(length(names(ref_line)) == 1, "fishing_mortality", names(dat)[i]), #"spawning_biomass", + ref_line = ref_line_x, + scale_amount = 1 + ) + } + } + final <- plt2 + theme_noaa() + } ### Make RDA ---- if (make_rda) { From 6cb15b456c5063486433928c08c79dd8700b760d Mon Sep 17 00:00:00 2001 From: Sam Schiano <125507018+Schiano-NOAA@users.noreply.github.com> Date: Mon, 27 Jul 2026 16:24:50 -0400 Subject: [PATCH 08/14] move reference line code into own function for simplicity --- R/plot_biomass.R | 76 +++++++++++++++++++------------------- R/plot_fishing_mortality.R | 76 +++++++++++++++++++------------------- R/plot_spawning_biomass.R | 54 ++++++--------------------- R/utils_plot.R | 58 +++++++++++++++++++++++++++++ 4 files changed, 147 insertions(+), 117 deletions(-) diff --git a/R/plot_biomass.R b/R/plot_biomass.R index e12fddf4..9652ac71 100644 --- a/R/plot_biomass.R +++ b/R/plot_biomass.R @@ -151,43 +151,45 @@ plot_biomass <- function( final <- plt + theme_noaa() } else { plt2 <- plt - # Check if length of ref_line = dat - # replicate value if not - if (length(ref_line) != length(dat)) ref_line <- rep(ref_line, length(dat)) - # Put into for loop and add lines sequentially to plt - for (i in 1:length(ref_line)) { - # find the reference point value - if (is.null(names(ref_line[i]))) { - ref_line_x <- calculate_reference_point( - dat = dat[[i]], - reference_name = glue::glue("biomass_{ref_line[i]}"), - lbs = lbs - ) / scale_amount - ref_line_x <- setNames(ref_line_x, ref_line[i]) - } else { - ref_line_x <- ref_line[i] / scale_amount - } - - if ("unfished" %in% names(ref_line_x)) { - # find the minimum x axis value from the plot - min_year <- min <- ggplot2::ggplot_build(plt2)[["data"]][[2]] |> # I think this was causing issues on linux? - as.data.frame() |> - dplyr::pull(x) |> - min() |> - round(digits = 2) - # add point to plot and add theme - plt2 <- plt2 + - ggplot2::geom_point(ggplot2::aes(x = min_year - 1, y = ref_point)) + # should I keep -1 or set as first year? - theme_noaa() - } else { - # add apply/purrr/or for loop for reference lines -- not just the first anymore - plt2 <- plt2 + - reference_line( - # conditionally add label name - label_name = ifelse(length(names(ref_line)) == 1, "biomass", names(dat)[i]), #"spawning_biomass", - ref_line = ref_line_x, - scale_amount = 1 - ) + if (is.null(names(ref_line[i]))) { + # Check if length of ref_line = dat + # replicate value if not + if (length(ref_line) != length(dat)) ref_line <- rep(ref_line, length(dat)) + # Put into for loop and add lines sequentially to plt + for (i in 1:length(ref_line)) { + # find the reference point value + if (is.null(names(ref_line[i]))) { + ref_line_x <- calculate_reference_point( + dat = dat[[i]], + reference_name = glue::glue("biomass_{ref_line[i]}"), + lbs = lbs + ) / scale_amount + ref_line_x <- setNames(ref_line_x, ref_line[i]) + } else { + ref_line_x <- ref_line[i] / scale_amount + } + + if ("unfished" %in% names(ref_line_x)) { + # find the minimum x axis value from the plot + min_year <- min <- ggplot2::ggplot_build(plt2)[["data"]][[2]] |> # I think this was causing issues on linux? + as.data.frame() |> + dplyr::pull(x) |> + min() |> + round(digits = 2) + # add point to plot and add theme + plt2 <- plt2 + + ggplot2::geom_point(ggplot2::aes(x = min_year - 1, y = ref_point)) + # should I keep -1 or set as first year? + theme_noaa() + } else { + # add apply/purrr/or for loop for reference lines -- not just the first anymore + plt2 <- plt2 + + reference_line( + # conditionally add label name + label_name = ifelse(length(names(ref_line)) == 1, "biomass", names(dat)[i]), #"spawning_biomass", + ref_line = ref_line_x, + scale_amount = 1 + ) + } } } final <- plt2 + theme_noaa() diff --git a/R/plot_fishing_mortality.R b/R/plot_fishing_mortality.R index aebda3ae..82668e5c 100644 --- a/R/plot_fishing_mortality.R +++ b/R/plot_fishing_mortality.R @@ -87,43 +87,45 @@ plot_fishing_mortality <- function( final <- plt + theme_noaa() } else { plt2 <- plt - # Check if length of ref_line = dat - # replicate value if not - if (length(ref_line) != length(dat)) ref_line <- rep(ref_line, length(dat)) - # Put into for loop and add lines sequentially to plt - for (i in 1:length(ref_line)) { - # find the reference point value - if (is.null(names(ref_line[i]))) { - ref_line_x <- calculate_reference_point( - dat = dat[[i]], - reference_name = glue::glue("fishing_mortality_{ref_line[i]}"), - lbs = lbs - ) / scale_amount - ref_line_x <- setNames(ref_line_x, ref_line[i]) - } else { - ref_line_x <- ref_line[i] / scale_amount - } - - if ("unfished" %in% names(ref_line_x)) { - # find the minimum x axis value from the plot - min_year <- min <- ggplot2::ggplot_build(plt2)[["data"]][[2]] |> # I think this was causing issues on linux? - as.data.frame() |> - dplyr::pull(x) |> - min() |> - round(digits = 2) - # add point to plot and add theme - plt2 <- plt2 + - ggplot2::geom_point(ggplot2::aes(x = min_year - 1, y = ref_point)) + # should I keep -1 or set as first year? - theme_noaa() - } else { - # add apply/purrr/or for loop for reference lines -- not just the first anymore - plt2 <- plt2 + - reference_line( - # conditionally add label name - label_name = ifelse(length(names(ref_line)) == 1, "fishing_mortality", names(dat)[i]), #"spawning_biomass", - ref_line = ref_line_x, - scale_amount = 1 - ) + if (is.null(names(ref_line[i]))) { + # Check if length of ref_line = dat + # replicate value if not + if (length(ref_line) != length(dat)) ref_line <- rep(ref_line, length(dat)) + # Put into for loop and add lines sequentially to plt + for (i in 1:length(ref_line)) { + # find the reference point value + if (is.null(names(ref_line[i]))) { + ref_line_x <- calculate_reference_point( + dat = dat[[i]], + reference_name = glue::glue("fishing_mortality_{ref_line[i]}"), + lbs = lbs + ) / scale_amount + ref_line_x <- setNames(ref_line_x, ref_line[i]) + } else { + ref_line_x <- ref_line[i] / scale_amount + } + + if ("unfished" %in% names(ref_line_x)) { + # find the minimum x axis value from the plot + min_year <- min <- ggplot2::ggplot_build(plt2)[["data"]][[2]] |> # I think this was causing issues on linux? + as.data.frame() |> + dplyr::pull(x) |> + min() |> + round(digits = 2) + # add point to plot and add theme + plt2 <- plt2 + + ggplot2::geom_point(ggplot2::aes(x = min_year - 1, y = ref_point)) + # should I keep -1 or set as first year? + theme_noaa() + } else { + # add apply/purrr/or for loop for reference lines -- not just the first anymore + plt2 <- plt2 + + reference_line( + # conditionally add label name + label_name = ifelse(length(names(ref_line)) == 1, "fishing_mortality", names(dat)[i]), #"spawning_biomass", + ref_line = ref_line_x, + scale_amount = 1 + ) + } } } final <- plt2 + theme_noaa() diff --git a/R/plot_spawning_biomass.R b/R/plot_spawning_biomass.R index 187ca92e..2338fe6b 100644 --- a/R/plot_spawning_biomass.R +++ b/R/plot_spawning_biomass.R @@ -196,8 +196,8 @@ plot_spawning_biomass <- function( ylab = spawning_biomass_label, group = group, # add check in case facet is returned as character(0) - facet = if (length(facet) > 0) facet else NULL, - ... + facet = if (length(facet) > 0) facet else NULL#, + # ... ) # Add reference line # Conditions for ref line @@ -209,47 +209,15 @@ plot_spawning_biomass <- function( # don't add any reference line here and just add theme for final plot final <- plt + theme_noaa() } else { - plt2 <- plt - # Check if length of ref_line = dat - # replicate value if not - if (length(ref_line) != length(dat)) ref_line <- rep(ref_line, length(dat)) - # Put into for loop and add lines sequentially to plt - for (i in 1:length(ref_line)) { - # find the reference point value - if (is.null(names(ref_line[i]))) { - ref_line_x <- calculate_reference_point( - dat = dat[[i]], - reference_name = glue::glue("spawning_biomass_{ref_line[i]}"), - lbs = lbs - ) / scale_amount - ref_line_x <- setNames(ref_line_x, ref_line[i]) - } else { - ref_line_x <- ref_line[i] / scale_amount - } - - if ("unfished" %in% names(ref_line_x)) { - # find the minimum x axis value from the plot - min_year <- min <- ggplot2::ggplot_build(plt2)[["data"]][[2]] |> # I think this was causing issues on linux? - as.data.frame() |> - dplyr::pull(x) |> - min() |> - round(digits = 2) - # add point to plot and add theme - plt2 <- plt2 + - ggplot2::geom_point(ggplot2::aes(x = min_year - 1, y = ref_point)) + # should I keep -1 or set as first year? - theme_noaa() - } else { - # add apply/purrr/or for loop for reference lines -- not just the first anymore - plt2 <- plt2 + - reference_line( - # conditionally add label name - label_name = ifelse(length(names(ref_line)) == 1, "spawning_biomass", names(dat)[i]), #"spawning_biomass", - ref_line = ref_line_x, - scale_amount = 1 - ) - } - } - final <- plt2 + theme_noaa() + final <- plt + + add_reference_line( + dat = dat, + ref_line = ref_line, + label = "spawning_biomass", + lbs = lbs, + scale_amount = scale_amount + ) + + theme_noaa() } ### Make RDA ---- diff --git a/R/utils_plot.R b/R/utils_plot.R index 458fc20c..d107848b 100644 --- a/R/utils_plot.R +++ b/R/utils_plot.R @@ -1045,3 +1045,61 @@ plot_obsvpred <- function( } final } + +#------------------------------------------------------------------------------ + +add_reference_line <- function( + dat, + ref_line, + label, + lbs = FALSE, + scale_amount = 1 +) { + ref_lines_list <- list() + if (!is.null(ref_line)) { + # Check if length of ref_line = dat + # replicate value if not + if (!is.data.frame(dat) & length(ref_line) != length(dat)) ref_line <- rep(ref_line, length(dat)) + # Put into for loop and add lines sequentially to plt + for (i in 1:length(ref_line)) { + # find the reference point value + if (is.null(names(ref_line[i]))) { + ref_line_x <- calculate_reference_point( + dat = if (is.data.frame(dat)) { dat } else { dat[[i]] }, + reference_name = glue::glue("{label}_{ref_line[i]}"), + lbs = lbs + ) / scale_amount + ref_line_x <- setNames(ref_line_x, ref_line[i]) + } else { + ref_line_x <- ref_line[i] / scale_amount + } + + if ("unfished" %in% names(ref_line_x)) { + # find the minimum x axis value from the plot + min_year <- dat |> + dplyr::filter(grepl(label, label)) |> + dplyr::pull(estimate) |> + min() |> + round(digits = 2) + # add point to plot and add theme + # plt2 <- plt2 + + ref_lines_list <- append(ref_lines_list, + ggplot2::geom_point(ggplot2::aes(x = min_year - 1, y = ref_point)) + # should I keep -1 or set as first year? + theme_noaa() + ) + } else { + # add apply/purrr/or for loop for reference lines -- not just the first anymore + # plt2 <- plt2 + + ref_lines_list <- append(ref_lines_list, + reference_line( + # conditionally add label name + label_name = ifelse(length(names(dat)[i]) == 1, "spawning_biomass", names(dat)[i]), #"spawning_biomass", + ref_line = ref_line_x, + scale_amount = scale_amount + ) + ) + } + } # close ref_line for loop + } # close if ref_line NULL + ref_lines_list +} From b47e247eab9a321ccce60b65095c19340e445bf0 Mon Sep 17 00:00:00 2001 From: Sam Schiano <125507018+Schiano-NOAA@users.noreply.github.com> Date: Mon, 27 Jul 2026 16:31:33 -0400 Subject: [PATCH 09/14] add new ref line function into other relevant fxns and replace code --- R/plot_biomass.R | 57 ++++++-------------------------------- R/plot_fishing_mortality.R | 52 ++++++---------------------------- R/plot_spawning_biomass.R | 5 ---- R/utils_plot.R | 6 ++++ 4 files changed, 24 insertions(+), 96 deletions(-) diff --git a/R/plot_biomass.R b/R/plot_biomass.R index 9652ac71..5c7bcd01 100644 --- a/R/plot_biomass.R +++ b/R/plot_biomass.R @@ -141,58 +141,19 @@ plot_biomass <- function( ... ) # Add reference line - # Conditions for ref line - # 1. all are comparing same value = msy, target, unfished... - # 2. custom input vector = c("sable23_msy"=30, "sable25_msy"=40) -- user must indicate which model in label otherwise it will assign in order of dat - # 3. input vector of labels = c("msy", "target") - # getting data set - an ifelse statement in the fxn wasn't working if (relative) { # don't add any reference line here and just add theme for final plot final <- plt + theme_noaa() } else { - plt2 <- plt - if (is.null(names(ref_line[i]))) { - # Check if length of ref_line = dat - # replicate value if not - if (length(ref_line) != length(dat)) ref_line <- rep(ref_line, length(dat)) - # Put into for loop and add lines sequentially to plt - for (i in 1:length(ref_line)) { - # find the reference point value - if (is.null(names(ref_line[i]))) { - ref_line_x <- calculate_reference_point( - dat = dat[[i]], - reference_name = glue::glue("biomass_{ref_line[i]}"), - lbs = lbs - ) / scale_amount - ref_line_x <- setNames(ref_line_x, ref_line[i]) - } else { - ref_line_x <- ref_line[i] / scale_amount - } - - if ("unfished" %in% names(ref_line_x)) { - # find the minimum x axis value from the plot - min_year <- min <- ggplot2::ggplot_build(plt2)[["data"]][[2]] |> # I think this was causing issues on linux? - as.data.frame() |> - dplyr::pull(x) |> - min() |> - round(digits = 2) - # add point to plot and add theme - plt2 <- plt2 + - ggplot2::geom_point(ggplot2::aes(x = min_year - 1, y = ref_point)) + # should I keep -1 or set as first year? - theme_noaa() - } else { - # add apply/purrr/or for loop for reference lines -- not just the first anymore - plt2 <- plt2 + - reference_line( - # conditionally add label name - label_name = ifelse(length(names(ref_line)) == 1, "biomass", names(dat)[i]), #"spawning_biomass", - ref_line = ref_line_x, - scale_amount = 1 - ) - } - } - } - final <- plt2 + theme_noaa() + final <- plt + + add_reference_line( + dat = dat, + ref_line = ref_line, + label = "biomass", + lbs = lbs, + scale_amount = scale_amount + ) + + theme_noaa() } ### Make RDA ---- diff --git a/R/plot_fishing_mortality.R b/R/plot_fishing_mortality.R index 82668e5c..2161f486 100644 --- a/R/plot_fishing_mortality.R +++ b/R/plot_fishing_mortality.R @@ -86,49 +86,15 @@ plot_fishing_mortality <- function( # don't add any reference line here and just add theme for final plot final <- plt + theme_noaa() } else { - plt2 <- plt - if (is.null(names(ref_line[i]))) { - # Check if length of ref_line = dat - # replicate value if not - if (length(ref_line) != length(dat)) ref_line <- rep(ref_line, length(dat)) - # Put into for loop and add lines sequentially to plt - for (i in 1:length(ref_line)) { - # find the reference point value - if (is.null(names(ref_line[i]))) { - ref_line_x <- calculate_reference_point( - dat = dat[[i]], - reference_name = glue::glue("fishing_mortality_{ref_line[i]}"), - lbs = lbs - ) / scale_amount - ref_line_x <- setNames(ref_line_x, ref_line[i]) - } else { - ref_line_x <- ref_line[i] / scale_amount - } - - if ("unfished" %in% names(ref_line_x)) { - # find the minimum x axis value from the plot - min_year <- min <- ggplot2::ggplot_build(plt2)[["data"]][[2]] |> # I think this was causing issues on linux? - as.data.frame() |> - dplyr::pull(x) |> - min() |> - round(digits = 2) - # add point to plot and add theme - plt2 <- plt2 + - ggplot2::geom_point(ggplot2::aes(x = min_year - 1, y = ref_point)) + # should I keep -1 or set as first year? - theme_noaa() - } else { - # add apply/purrr/or for loop for reference lines -- not just the first anymore - plt2 <- plt2 + - reference_line( - # conditionally add label name - label_name = ifelse(length(names(ref_line)) == 1, "fishing_mortality", names(dat)[i]), #"spawning_biomass", - ref_line = ref_line_x, - scale_amount = 1 - ) - } - } - } - final <- plt2 + theme_noaa() + final <- plt + + add_reference_line( + dat = dat, + ref_line = ref_line, + label = "fishing_mortality", + lbs = lbs, + scale_amount = scale_amount + ) + + theme_noaa() } ### Make RDA ---- diff --git a/R/plot_spawning_biomass.R b/R/plot_spawning_biomass.R index 2338fe6b..cc12c70c 100644 --- a/R/plot_spawning_biomass.R +++ b/R/plot_spawning_biomass.R @@ -200,11 +200,6 @@ plot_spawning_biomass <- function( # ... ) # Add reference line - # Conditions for ref line - # 1. all are comparing same value = msy, target, unfished... - # 2. custom input vector = c("sable23_msy"=30, "sable25_msy"=40) -- user must indicate which model in label otherwise it will assign in order of dat - # 3. input vector of labels = c("msy", "target") - # getting data set - an ifelse statement in the fxn wasn't working if (relative) { # don't add any reference line here and just add theme for final plot final <- plt + theme_noaa() diff --git a/R/utils_plot.R b/R/utils_plot.R index d107848b..6e406db8 100644 --- a/R/utils_plot.R +++ b/R/utils_plot.R @@ -1055,6 +1055,12 @@ add_reference_line <- function( lbs = FALSE, scale_amount = 1 ) { + # Add reference line + # Conditions for ref line + # 1. all are comparing same value = msy, target, unfished... + # 2. custom input vector = c("sable23_msy"=30, "sable25_msy"=40) -- user must indicate which model in label otherwise it will assign in order of dat + # 3. input vector of labels = c("msy", "target") + # getting data set - an ifelse statement in the fxn wasn't working ref_lines_list <- list() if (!is.null(ref_line)) { # Check if length of ref_line = dat From 1ac2b748afe5f031b74dd975403ab6daabe2e817 Mon Sep 17 00:00:00 2001 From: Sam Schiano <125507018+Schiano-NOAA@users.noreply.github.com> Date: Tue, 28 Jul 2026 17:40:58 -0400 Subject: [PATCH 10/14] remove lbs from B and F plots in ref line; consolidate note in utils --- R/plot_biomass.R | 3 +-- R/plot_fishing_mortality.R | 11 ++--------- R/utils_plot.R | 26 +++++++++++++++++--------- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/R/plot_biomass.R b/R/plot_biomass.R index 5c7bcd01..725d3a36 100644 --- a/R/plot_biomass.R +++ b/R/plot_biomass.R @@ -149,8 +149,7 @@ plot_biomass <- function( add_reference_line( dat = dat, ref_line = ref_line, - label = "biomass", - lbs = lbs, + label = "biomass", scale_amount = scale_amount ) + theme_noaa() diff --git a/R/plot_fishing_mortality.R b/R/plot_fishing_mortality.R index 2161f486..93b73e1f 100644 --- a/R/plot_fishing_mortality.R +++ b/R/plot_fishing_mortality.R @@ -82,20 +82,13 @@ plot_fishing_mortality <- function( # 2. custom input vector = c("sable23_msy"=30, "sable25_msy"=40) -- user must indicate which model in label otherwise it will assign in order of dat # 3. input vector of labels = c("msy", "target") # getting data set - an ifelse statement in the fxn wasn't working - if (relative) { - # don't add any reference line here and just add theme for final plot - final <- plt + theme_noaa() - } else { - final <- plt + + final <- plt + add_reference_line( dat = dat, ref_line = ref_line, - label = "fishing_mortality", - lbs = lbs, - scale_amount = scale_amount + label = "fishing_mortality" ) + theme_noaa() - } ### Make RDA ---- if (make_rda) { diff --git a/R/utils_plot.R b/R/utils_plot.R index 6e406db8..360f0dc3 100644 --- a/R/utils_plot.R +++ b/R/utils_plot.R @@ -1063,8 +1063,7 @@ add_reference_line <- function( # getting data set - an ifelse statement in the fxn wasn't working ref_lines_list <- list() if (!is.null(ref_line)) { - # Check if length of ref_line = dat - # replicate value if not + # Check if length of ref_line = dat -- replicate value if not if (!is.data.frame(dat) & length(ref_line) != length(dat)) ref_line <- rep(ref_line, length(dat)) # Put into for loop and add lines sequentially to plt for (i in 1:length(ref_line)) { @@ -1075,23 +1074,32 @@ add_reference_line <- function( reference_name = glue::glue("{label}_{ref_line[i]}"), lbs = lbs ) / scale_amount + if (length(ref_line_x) == 0) { + cli::cli_alert_warning("{label}_{ref_line[i]} not found for {ifelse(is.data.frame(dat), 'data', names(dat[i]))}") + next + } ref_line_x <- setNames(ref_line_x, ref_line[i]) } else { ref_line_x <- ref_line[i] / scale_amount } if ("unfished" %in% names(ref_line_x)) { + if (is.data.frame(dat)) { + sel_dat <- dat + } else { + sel_dat <- dat[[i]] + } + plt_lab <- label # find the minimum x axis value from the plot - min_year <- dat |> - dplyr::filter(grepl(label, label)) |> - dplyr::pull(estimate) |> - min() |> + min_year <- sel_dat |> + dplyr::filter(grepl(plt_lab, label), year != 1) |> + dplyr::pull(year) |> + min(na.rm = TRUE) |> round(digits = 2) # add point to plot and add theme # plt2 <- plt2 + ref_lines_list <- append(ref_lines_list, - ggplot2::geom_point(ggplot2::aes(x = min_year - 1, y = ref_point)) + # should I keep -1 or set as first year? - theme_noaa() + ggplot2::geom_point(ggplot2::aes(x = min_year - 1, y = ref_line_x)) # should I keep -1 or set as first year? ) } else { # add apply/purrr/or for loop for reference lines -- not just the first anymore @@ -1099,7 +1107,7 @@ add_reference_line <- function( ref_lines_list <- append(ref_lines_list, reference_line( # conditionally add label name - label_name = ifelse(length(names(dat)[i]) == 1, "spawning_biomass", names(dat)[i]), #"spawning_biomass", + label_name = ifelse(length(names(dat)[i]) == 1, label, names(dat)[i]), #"spawning_biomass", ref_line = ref_line_x, scale_amount = scale_amount ) From 222f420754241228cb625647bd3f1b839ea61b1f Mon Sep 17 00:00:00 2001 From: Sam Schiano <125507018+Schiano-NOAA@users.noreply.github.com> Date: Tue, 28 Jul 2026 17:49:15 -0400 Subject: [PATCH 11/14] add todo to adjust color of unfished ref pt --- R/utils_plot.R | 1 + 1 file changed, 1 insertion(+) diff --git a/R/utils_plot.R b/R/utils_plot.R index 360f0dc3..d7b60615 100644 --- a/R/utils_plot.R +++ b/R/utils_plot.R @@ -1098,6 +1098,7 @@ add_reference_line <- function( round(digits = 2) # add point to plot and add theme # plt2 <- plt2 + + # TODO: set color for each point to match that of the line for the model ref_lines_list <- append(ref_lines_list, ggplot2::geom_point(ggplot2::aes(x = min_year - 1, y = ref_line_x)) # should I keep -1 or set as first year? ) From 1f44495b98328af4b1b6a190e44662015354cf7f Mon Sep 17 00:00:00 2001 From: Sam Schiano <125507018+Schiano-NOAA@users.noreply.github.com> Date: Wed, 29 Jul 2026 10:29:00 -0400 Subject: [PATCH 12/14] add missing pkg dependencies from used functions --- R/utils_plot.R | 2 +- R/utils_rda.R | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/R/utils_plot.R b/R/utils_plot.R index d7b60615..9defe9d8 100644 --- a/R/utils_plot.R +++ b/R/utils_plot.R @@ -1078,7 +1078,7 @@ add_reference_line <- function( cli::cli_alert_warning("{label}_{ref_line[i]} not found for {ifelse(is.data.frame(dat), 'data', names(dat[i]))}") next } - ref_line_x <- setNames(ref_line_x, ref_line[i]) + ref_line_x <- utils::setNames(ref_line_x, ref_line[i]) } else { ref_line_x <- ref_line[i] / scale_amount } diff --git a/R/utils_rda.R b/R/utils_rda.R index a5b84d2c..27721710 100644 --- a/R/utils_rda.R +++ b/R/utils_rda.R @@ -177,7 +177,7 @@ calc_kqs <- function(returned_kq, if (returned_kq == "F.min") { return( prepared_data$estimate |> - na.omit() |> + data.table::na.omit() |> min() |> round(digits = 3) ) @@ -186,7 +186,7 @@ calc_kqs <- function(returned_kq, if (returned_kq == "F.max") { return( prepared_data$estimate |> - na.omit() |> + data.table::na.omit() |> max() |> round(digits = 3) ) From 0ba22c43bfa44c48b905f8b30533a1b228ccd235 Mon Sep 17 00:00:00 2001 From: Sam Schiano <125507018+Schiano-NOAA@users.noreply.github.com> Date: Wed, 29 Jul 2026 16:31:57 -0400 Subject: [PATCH 13/14] set legends to show under certain conditions for elements of line plot and ref line --- R/utils_plot.R | 64 ++++++++++++++++++-------------------------------- 1 file changed, 23 insertions(+), 41 deletions(-) diff --git a/R/utils_plot.R b/R/utils_plot.R index 9defe9d8..bf9a14da 100644 --- a/R/utils_plot.R +++ b/R/utils_plot.R @@ -71,30 +71,19 @@ plot_timeseries <- function( ) { # Start plot plot <- ggplot2::ggplot() - # make into new geom? - # more defaults and fxnality for ggplot # Add geom plot <- switch(geom, "point" = { - # point_size = ifelse( - # is.null(list(...)$size), - # 2.0, - # list(...)$size - # ) plot + ggplot2::geom_point( data = dat, ggplot2::aes( .data[[x]], .data[[y]], - # TODO: add more groupings - # shape = ifelse(any(grepl("shape", names(group))), .data[[group[[grep("shape", names(group))]]]], 1), - # color = ifelse(any(grepl("color", names(group))), .data[[group[[grep("color", names(group))]]]], "black") color = model, shape = group_var ), - # size = point_size, ... ) }, @@ -119,7 +108,8 @@ plot_timeseries <- function( } } ), - alpha = 0.3 + alpha = 0.3, + show.legend = ifelse(all(is.na(dat$estimate_lower)), FALSE, TRUE) ) + # } ggplot2::geom_line( @@ -127,8 +117,6 @@ plot_timeseries <- function( ggplot2::aes( x = .data[[x]], y = .data[[y]], - # linetype = group_var, - # linetype = ifelse(!is.null(group), group_var, "solid"), color = { if (length(unique(.data[["model"]])) > 1) { interaction(model, group_var) @@ -137,7 +125,7 @@ plot_timeseries <- function( } } ), - # linewidth = 1.0, + show.legend = FALSE, #ifelse(all(is.na(dat$estimate_lower)), TRUE, FALSE), ... ) }, @@ -148,7 +136,7 @@ plot_timeseries <- function( ggplot2::aes( x = .data[[x]], y = .data[[y]], - fill = model + fill = interaction(model, group_var) # model ), ... ) @@ -160,10 +148,6 @@ plot_timeseries <- function( labs <- plot + ggplot2::labs( x = xlab, y = ylab - # color = "Model", - # linetype = cap_first_letter(group), - # fill = cap_first_letter(group), - # shape = cap_first_letter(group) ) + ggplot2::theme(legend.title = ggplot2::element_blank()) } else { @@ -195,15 +179,7 @@ plot_timeseries <- function( # return plot if option beyond line and point for now labs ) - } # else if (length(unique(dat$model)) == 1) { - # labs <- switch(geom, - # "line" = labs + ggplot2::guides(color = "none"), - # "point" = labs + ggplot2::guides(color = "none"), - # "area" = labs + ggplot2::guides(fill = "none"), - # # return plot if option beyond line and point for now - # labs - # ) - # } + } # Calc axis breaks x_n_breaks <- axis_breaks(dat[[x]]) @@ -546,7 +522,8 @@ cohort_line <- function( reference_line <- function( label_name, ref_line, - scale_amount = 1 + scale_amount = 1, + model_name = "1" ) { # Extract reference line values and labels ref_line_val <- ref_line[[1]] @@ -555,21 +532,25 @@ reference_line <- function( list( # Add geom for ref line ggplot2::geom_hline( - yintercept = ref_line_val / scale_amount, - color = "black", - linetype = "dashed" + data = data.frame(yintercept = ref_line_val, model = model_name, group_var = "1"), + ggplot2::aes( + yintercept = ref_line_val / scale_amount, + color = interaction(model, group_var) # model + ), + linetype = "dashed", + show.legend = FALSE ), # add line annotation ggplot2::annotate( geom = "text", # TODO: need to change this for general process - x = Inf, + x = -Inf, y = ref_line_val / scale_amount, - label = glue::glue("{stringr::str_replace_all(label_name, '_', '~')}[{reference}]"), # list(bquote(label_name[.(reference)])), + label = glue::glue("{reference}"), # glue::glue("{stringr::str_replace_all(label_name, '_', '~')}[{reference}]"), # list(bquote(label_name[.(reference)])), parse = TRUE, - hjust = 1.05, # slight offset so text doesn't hit border - vjust = -0.5, #slightly above line - size = 5 # this is not foolproof + hjust = -0.05, # slight offset so text doesn't hit border + vjust = -0.5 # , #slightly above line + # size = 5 # this is not foolproof ) ) } @@ -1078,7 +1059,7 @@ add_reference_line <- function( cli::cli_alert_warning("{label}_{ref_line[i]} not found for {ifelse(is.data.frame(dat), 'data', names(dat[i]))}") next } - ref_line_x <- utils::setNames(ref_line_x, ref_line[i]) + ref_line_x <- stats::setNames(ref_line_x, ref_line[i]) } else { ref_line_x <- ref_line[i] / scale_amount } @@ -1100,7 +1081,7 @@ add_reference_line <- function( # plt2 <- plt2 + # TODO: set color for each point to match that of the line for the model ref_lines_list <- append(ref_lines_list, - ggplot2::geom_point(ggplot2::aes(x = min_year - 1, y = ref_line_x)) # should I keep -1 or set as first year? + ggplot2::geom_point(ggplot2::aes(x = min_year - 1, y = ref_line_x, color = model)) # should I keep -1 or set as first year? ) } else { # add apply/purrr/or for loop for reference lines -- not just the first anymore @@ -1110,7 +1091,8 @@ add_reference_line <- function( # conditionally add label name label_name = ifelse(length(names(dat)[i]) == 1, label, names(dat)[i]), #"spawning_biomass", ref_line = ref_line_x, - scale_amount = scale_amount + scale_amount = scale_amount, + model_name = ifelse(is.data.frame(dat), "1", names(dat)[i]) ) ) } From 64b6e7b21f392f5ec71949736b89d39534730936 Mon Sep 17 00:00:00 2001 From: Sam Schiano <125507018+Schiano-NOAA@users.noreply.github.com> Date: Thu, 30 Jul 2026 10:18:58 -0400 Subject: [PATCH 14/14] comment out conditional removal of legend --- R/utils_plot.R | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/R/utils_plot.R b/R/utils_plot.R index bf9a14da..eeaffbd1 100644 --- a/R/utils_plot.R +++ b/R/utils_plot.R @@ -108,8 +108,8 @@ plot_timeseries <- function( } } ), - alpha = 0.3, - show.legend = ifelse(all(is.na(dat$estimate_lower)), FALSE, TRUE) + alpha = 0.3 #, + # show.legend = ifelse(all(is.na(dat$estimate_lower)), FALSE, TRUE) ) + # } ggplot2::geom_line( @@ -125,7 +125,7 @@ plot_timeseries <- function( } } ), - show.legend = FALSE, #ifelse(all(is.na(dat$estimate_lower)), TRUE, FALSE), + # show.legend = FALSE, #ifelse(all(is.na(dat$estimate_lower)), TRUE, FALSE), ... ) },