Skip to content

Commit 222ba43

Browse files
Add theming function (#72)
* Create first working version of new nmfs theme, for continuous palettes only * Add functionality for nmfs theme, for discrete and non-interpolated palettes * Add functionality for nmfs theme, for discrete and interpolated palettes; update documentation * Update output type of display_nmfs_palette() and associated test * Add note directing user to ggplot2's v4.0.0 release notes; update documentation * Add new tests for theming fxn * Update README * Updates as suggested by @k-doering-NOAA during review
1 parent 5009b19 commit 222ba43

File tree

12 files changed

+1360
-11
lines changed

12 files changed

+1360
-11
lines changed

DESCRIPTION

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: nmfspalette
22
Title: A Color Palette for NOAA Fisheries
3-
Version: 3.2.0.000
3+
Version: 3.3.0.000
44
Authors@R: c(
55
person("Christine", "Stawitz", , "christine.stawitz@noaa.gov", role = "aut"),
66
person("Bai", "Li", , "bai.li@noaa.gov", role = "aut"),
@@ -17,8 +17,10 @@ BugReports: https://github.com/nmfs-ost/nmfspalette/issues
1717
Imports:
1818
cli,
1919
ggplot2,
20+
graphics,
2021
pals,
21-
rlang
22+
rlang,
23+
utils
2224
Suggests:
2325
dichromat,
2426
dplyr,
@@ -36,4 +38,4 @@ Config/testthat/edition: 3
3638
Encoding: UTF-8
3739
LazyData: true
3840
Roxygen: list(markdown = TRUE)
39-
RoxygenNote: 7.3.2
41+
RoxygenNote: 7.3.3

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ export(nmfs_cols)
66
export(nmfs_palette)
77
export(scale_color_nmfs)
88
export(scale_fill_nmfs)
9+
export(theme_nmfs)

R/ggplot2-scales.R

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,115 @@ To avoid this error, use a larger palette or `interpolate = TRUE`.",
115115
ggplot2::scale_fill_gradientn(colours = pal(256), ...)
116116
}
117117
}
118+
119+
120+
#' Create theme for nmfs colors
121+
#' @inheritParams scale_color_nmfs
122+
#' @param base_size The base font size, as defined in the
123+
#' [ggplot2::theme_gray()] function
124+
#' @param lab_size The axis text size
125+
#' @param ink The plot foreground color, as defined in the
126+
#' [ggplot2::theme_gray()] function
127+
#' @param paper The plot background color, as defined in the
128+
#' [ggplot2::theme_gray()] function
129+
#' @param accent The plot's accented elements' color, as defined in the
130+
#' [ggplot2::theme_gray()] function
131+
#' @param ... Additional arguments passed to: [ggplot2::scale_fill_gradientn()]
132+
#' when `discrete` is TRUE; [ggplot2::discrete_scale()] when `discrete` is FALSE
133+
#' and `interpolate` is TRUE; and [ggplot2::scale_fill_manual()] when `discrete`
134+
#' is FALSE and `interpolate` is FALSE.
135+
#' @return A theme that applies nmfs color palettes and theme-related elements (such as label sizes, borders, axis lines, and more) to a plot. For more information about the ggplot2 advances that enabled this function, check out the [ggplot2 version 4.0.0 release notes](https://tidyverse.org/blog/2025/09/ggplot2-4-0-0/).
136+
#' @examples
137+
#' library(ggplot2)
138+
#' ggplot(mtcars, aes(x = mpg, y = disp, color = as.factor(cyl))) +
139+
#' geom_point(size = 3) +
140+
#' theme_nmfs(discrete = TRUE, interpolate = TRUE, palette = "crustacean")
141+
#' ggplot(mtcars, aes(x = mpg, y = disp, color = as.factor(cyl))) +
142+
#' geom_point(size = 3) +
143+
#' theme_nmfs(discrete = TRUE, interpolate = FALSE, palette = "crustacean")
144+
#' ggplot(mtcars, aes(x = mpg, y = disp, fill = hp)) +
145+
#' geom_point(size = 3, shape = 24) +
146+
#' theme_nmfs(discrete = FALSE, interpolate = TRUE, palette = "seagrass")
147+
#' @export
148+
theme_nmfs <- function(
149+
palette = "oceans",
150+
discrete = TRUE,
151+
reverse = FALSE,
152+
interpolate = TRUE,
153+
base_size = 14,
154+
lab_size = 12,
155+
ink = "black",
156+
paper = "white",
157+
accent = "#003087",
158+
...) {
159+
if (utils::packageVersion("ggplot2") < "4.0.0"){
160+
rlang::warn(
161+
message = paste0("Your `ggplot2` version is ", utils::packageVersion("ggplot2"), ", which is older than the version required to use `theme_nmfs()` (4.0.0). Update your `ggplot2` package to use this new function!"),
162+
.frequency = "once",
163+
.frequency_id = "ggplot2_version_warning"
164+
)
165+
}
166+
167+
168+
# get palette
169+
pal <- nmfs_palette(palette = palette,
170+
reverse = reverse)
171+
172+
pal_length <- length(nmfs_palettes[[palette]])
173+
174+
base_theme1 <- ggplot2::theme_bw(
175+
base_size = base_size,
176+
ink = ink,
177+
paper = paper,
178+
accent = accent,
179+
...
180+
)
181+
182+
base_theme2 <- ggplot2::theme(
183+
axis.line = element_line(color = "black",
184+
linewidth = 0.75),
185+
axis.text.x = element_text(size = lab_size,
186+
color = "black"),
187+
axis.text.y = element_text(size = lab_size,
188+
color = "black"),
189+
panel.border = element_blank(),
190+
panel.grid.major = element_blank(),
191+
panel.grid.minor = element_blank(),
192+
...
193+
)
194+
195+
if (discrete) {
196+
if (interpolate) {
197+
198+
base_theme1 +
199+
base_theme2 +
200+
ggplot2::theme(
201+
palette.colour.discrete = pal,
202+
palette.fill.discrete = pal
203+
)
204+
205+
} else {
206+
cli::cli_alert_info("The {palette} palette has {pal_length} colors.")
207+
rlang::warn(
208+
message = "An error will occur if there are too few palette colors for your plot.
209+
To avoid this error, use a larger palette or `interpolate = TRUE`.",
210+
.frequency = "once",
211+
.frequency_id = "too_few_colors_warning_fill"
212+
)
213+
214+
base_theme1 +
215+
base_theme2 +
216+
ggplot2::theme(
217+
palette.colour.discrete = nmfs_palette(palette)(pal_length),
218+
palette.fill.discrete = nmfs_palette(palette)(pal_length)
219+
)
220+
}
221+
} else {
222+
base_theme1 +
223+
base_theme2 +
224+
ggplot2::theme(
225+
palette.color.continuous = nmfs_palette(palette)(pal_length),
226+
palette.fill.continuous = nmfs_palette(palette)(pal_length)
227+
)
228+
}
229+
}

R/nmfs_cols.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ nmfs_palette <- function(palette = "oceans", reverse = FALSE, ...) {
356356
#' @param n Number of colors in palette.
357357
#' @examples
358358
#' display_nmfs_palette("oceans", 10)
359-
#' @return A list object showing a specific nmfs color palette in the plot window.
359+
#' @return An object showing a specific nmfs color palette in the plot window.
360360
#' @export
361361
display_nmfs_palette <- function(name, n) {
362362
pal <- nmfs_palette(name)(n)
@@ -400,10 +400,10 @@ display_nmfs_palette <- function(name, n) {
400400
#' @export
401401
all_nmfs_palettes <- function() {
402402
# default setting
403-
old_par <- par(mar = c(0, 6, 0, 0))
403+
old_par <- graphics::par(mar = c(0, 6, 0, 0))
404404

405405
# reset par to original setting
406-
on.exit(par(old_par))
406+
on.exit(graphics::par(old_par))
407407

408408
do.call(
409409
pals::pal.bands,

README.Rmd

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,20 @@ You can change the number of colors (10 shown below).
6161
nmfs_palette("oceans")(10)
6262
```
6363

64+
## Theme for `ggplot` objects
65+
66+
**New feature**: With the new `theme_nmfs()` function, you can add a NMFS-colored theme to your `ggplot` objects!
67+
This new theme that applies NMFS color palettes and theme-related elements (such as label sizes, borders, axis lines, and more) to a plot.
68+
69+
```{r}
70+
#| label: theme_example
71+
72+
library(ggplot2)
73+
ggplot(mtcars, aes(x = mpg, y = disp, color = as.factor(cyl))) +
74+
geom_point(size = 3) +
75+
nmfspalette::theme_nmfs(discrete = TRUE, interpolate = TRUE, palette = "crustacean")
76+
```
77+
6478
## Palettes
6579

6680
Use the `all_nmfs_palettes()` function to see all available palettes.

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,24 @@ nmfs_palette("oceans")(10)
5959
#> [8] "#002467" "#001D55" "#001743"
6060
```
6161

62+
## Theme for `ggplot` objects
63+
64+
**New feature**: With the new `theme_nmfs()` function, you can add a
65+
NMFS-colored theme to your `ggplot` objects! This new theme that applies
66+
NMFS color palettes and theme-related elements (such as label sizes,
67+
borders, axis lines, and more) to a plot.
68+
69+
``` r
70+
library(ggplot2)
71+
ggplot(mtcars, aes(x = mpg, y = disp, color = as.factor(cyl))) +
72+
geom_point(size = 3) +
73+
nmfspalette::theme_nmfs(discrete = TRUE, interpolate = TRUE, palette = "crustacean")
74+
#> Warning: This function may not work if your `ggplot2` version is below 4.0.0. Update your package version to utilize this new function!
75+
#> This warning is displayed once per session.
76+
```
77+
78+
<img src="man/figures/README-theme_example-1.png" width="100%" />
79+
6280
## Palettes
6381

6482
Use the `all_nmfs_palettes()` function to see all available palettes.

man/display_nmfs_palette.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
4.78 KB
Loading

man/theme_nmfs.Rd

Lines changed: 70 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)