From c73b3539c5e92924a27d1fe8ee1a0e5492abe8b4 Mon Sep 17 00:00:00 2001 From: sbreitbart-NOAA Date: Wed, 4 Mar 2026 10:10:32 -0500 Subject: [PATCH 1/2] Update theme_noaa() to use ggplot2 v4.0 syntax and allow global theming, prompted by https://github.com/nmfs-ost/stockplotr/issues/143; update DESCRIPTION --- DESCRIPTION | 2 +- R/theme_noaa.R | 65 +++++++++++++++++++++++++------------------------- 2 files changed, 33 insertions(+), 34 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 56d73a6e..66469ece 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -39,7 +39,7 @@ Imports: dplyr, flextable, fs, - ggplot2, + ggplot2 (>= 4.0.0), glue, gt, httr, diff --git a/R/theme_noaa.R b/R/theme_noaa.R index 9a8965e7..6d9a2f00 100644 --- a/R/theme_noaa.R +++ b/R/theme_noaa.R @@ -17,48 +17,47 @@ #' ) + #' ggplot2::geom_point() + #' theme_noaa() +#'\dontrun{ +#' ggplot2::theme_set(stockplotr::theme_noaa()) +#'} theme_noaa <- function(discrete = TRUE, ...) { - theme <- list( - # might not need the ggplot2 dependencies if make this pkg an extension + + if (utils::packageVersion("ggplot2") < "4.0.0") { + rlang::warn( + message = paste0("Your `ggplot2` version is ", utils::packageVersion("ggplot2"), ", which is older than the version required to use `theme_noaa()` (4.0.0). Please update your `ggplot2` package."), + .frequency = "once", + .frequency_id = "ggplot2_version_warning" + ) + } + + theme <- ggplot2::theme_bw() + ggplot2::theme( plot.background = ggplot2::element_rect(fill = "transparent"), panel.background = ggplot2::element_rect(fill = "transparent"), panel.grid = ggplot2::element_blank(), panel.border = ggplot2::element_rect(colour = "black", fill = NA, linewidth = 0.5), - complete = TRUE, text = ggplot2::element_text(size = 14), - plot.margin = ggplot2::margin(0.5, 1, 0.5, 0.5, "cm") - ) + plot.margin = ggplot2::margin(0.5, 1, 0.5, 0.5, "cm"), + ... ) + # create main palette + pal <- function(n) viridisLite::mako(n, begin = 0.1, end = 0.85) + if (discrete) { - list( - theme, - ggplot2::scale_color_viridis_d( - option = "mako", - begin = 0.1, - end = 0.85 - ), - ggplot2::scale_fill_viridis_d( - option = "mako", - begin = 0.1, - end = 0.85 - ) - ) - } else { - list( - theme, - ggplot2::scale_color_viridis_c( - option = "mako", - begin = 0.1, - end = 0.85 - ), - ggplot2::scale_fill_viridis_c( - option = "mako", - begin = 0.1, - end = 0.85 - ) - ) - } + theme <- theme + + ggplot2::theme( + palette.colour.discrete = pal, + palette.fill.discrete = pal + ) + } else { + theme <- theme + + ggplot2::theme( + palette.colour.continuous = pal, + palette.fill.continuous = pal + ) + } + + theme } From 2e955a33b675769452ac5b763fda089008555039 Mon Sep 17 00:00:00 2001 From: sbreitbart-NOAA Date: Wed, 4 Mar 2026 10:25:27 -0500 Subject: [PATCH 2/2] Fix tests --- tests/testthat/test-plot_abundance_at_age.R | 2 +- tests/testthat/test-plot_biomass_at_age.R | 2 +- tests/testthat/test-plot_catch_comp.R | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/testthat/test-plot_abundance_at_age.R b/tests/testthat/test-plot_abundance_at_age.R index fce1f13f..91b37add 100644 --- a/tests/testthat/test-plot_abundance_at_age.R +++ b/tests/testthat/test-plot_abundance_at_age.R @@ -164,5 +164,5 @@ test_that("plot contains a legend", { make_rda = FALSE, figures_dir = getwd() ) - expect_true(is.null(plot$theme$legend.position)) + expect_true(!is.null(plot$theme$legend.position)) }) diff --git a/tests/testthat/test-plot_biomass_at_age.R b/tests/testthat/test-plot_biomass_at_age.R index 08f72016..a710335b 100644 --- a/tests/testthat/test-plot_biomass_at_age.R +++ b/tests/testthat/test-plot_biomass_at_age.R @@ -150,5 +150,5 @@ test_that("plot contains a legend", { make_rda = FALSE, figures_dir = getwd() ) - expect_true(is.null(plot$theme$legend.position)) + expect_true(!is.null(plot$theme$legend.position)) }) diff --git a/tests/testthat/test-plot_catch_comp.R b/tests/testthat/test-plot_catch_comp.R index 71c59c62..06994454 100644 --- a/tests/testthat/test-plot_catch_comp.R +++ b/tests/testthat/test-plot_catch_comp.R @@ -164,7 +164,7 @@ test_that("plot contains a legend", { make_rda = FALSE, figures_dir = getwd() ) - expect_true(is.null(plot$theme$legend.position)) + expect_true(!is.null(plot$theme$legend.position)) }) test_that("year class lines are plotted", {