Skip to content

Commit 98a774f

Browse files
Add option to use exact palette colors (#64)
* Add option to use specific colors from nmfspalette palettes, as opposed to the default behavior (interpolation), for scale_color_nmfs; update documentation * Add interpolation option to scale_fill_nmfs; add messages if interpolate = FALSE; update documentation * Add and update tests for new interpolate argument * Make warnings displayed only once per session
1 parent f05cc79 commit 98a774f

File tree

6 files changed

+443
-36
lines changed

6 files changed

+443
-36
lines changed

DESCRIPTION

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ License: GPL-3 | file LICENSE
1515
URL: https://github.com/nmfs-ost/nmfspalette
1616
BugReports: https://github.com/nmfs-ost/nmfspalette/issues
1717
Imports:
18-
ggplot2
18+
cli,
19+
ggplot2,
20+
rlang
1921
Suggests:
2022
dichromat,
2123
dplyr,

R/ggplot2-scales.R

Lines changed: 62 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,64 +6,103 @@
66
#' Default is TRUE.
77
#' @param reverse Boolean indicating whether the palette should be reversed.
88
#' Default is FALSE.
9-
#' @param ... Additional arguments passed to [ggplot2::discrete_scale()] or
10-
#' [ggplot2::scale_color_gradientn()], used respectively when
11-
#' `discrete` is TRUE or FALSE.
9+
#' @param interpolate Boolean indicating whether the colors assigned to plot
10+
#' objects should interpolated from palettes, with the alternative that only the
11+
#' defined colors in the palette are used. Default is TRUE.
12+
#' @param ... Additional arguments passed to: [ggplot2::scale_color_gradientn()]
13+
#' when `discrete` is TRUE; [ggplot2::discrete_scale()] when `discrete` is FALSE
14+
#' and `interpolate` is TRUE; and [ggplot2::scale_color_manual()] when `discrete`
15+
#' is FALSE and `interpolate` is FALSE.
1216
#' @examples
1317
#' library(ggplot2)
1418
#' ggplot(iris, aes(Sepal.Width, Sepal.Length, color = Species)) +
1519
#' geom_point(size = 4) +
1620
#' scale_color_nmfs("coral")
21+
#'
22+
#' ggplot(mtcars, aes(mpg, disp, color = as.factor(gear))) +
23+
#' geom_point(size = 4) +
24+
#' scale_color_nmfs("regional",
25+
#' interpolate = FALSE,
26+
#' discrete = TRUE)
1727
#' @export
1828
scale_color_nmfs <- function(
1929
palette = "oceans",
2030
discrete = TRUE,
2131
reverse = FALSE,
32+
interpolate = TRUE,
2233
...) {
2334
pal <- nmfs_palette(palette = palette, reverse = reverse)
2435

36+
pal_length <- length(nmfs_palettes[[palette]])
37+
2538
if (discrete) {
26-
ggplot2::discrete_scale(
27-
aesthetics = "colour",
28-
palette = pal,
29-
...
30-
)
39+
if (interpolate){
40+
ggplot2::discrete_scale(
41+
aesthetics = "colour",
42+
palette = pal,
43+
...
44+
)
45+
} else {
46+
cli::cli_alert_info("The {palette} palette has {pal_length} colors.")
47+
rlang::warn(message = "An error will occur if there are too few palette colors for your plot.
48+
To avoid this error, use a larger palette or `interpolate = TRUE`.",
49+
.frequency = "once",
50+
.frequency_id = "too_few_colors_warning_color")
51+
ggplot2::scale_color_manual(
52+
values = nmfs_palette(palette)(pal_length),
53+
...)
54+
}
3155
} else {
3256
ggplot2::scale_color_gradientn(colours = pal(256), ...)
3357
}
3458
}
3559

3660
#' Fill scale constructor for nmfs colors
37-
#'
38-
#' @param palette Character name of palette in `nmfs_palettes`. Default value
39-
#' is "oceans".
40-
#' @param discrete Boolean indicating whether color aesthetic is discrete.
41-
#' Default value is TRUE.
42-
#' @param reverse Boolean indicating whether the palette should be reversed.
43-
#' Default value is FALSE.
44-
#' @param ... Additional arguments passed to [ggplot2::discrete_scale()] or
45-
#' [ggplot2::scale_fill_gradientn()], used respectively when
46-
#' `discrete` is TRUE or FALSE.
61+
#' @inheritParams scale_color_nmfs
62+
#' @param ... Additional arguments passed to: [ggplot2::scale_fill_gradientn()]
63+
#' when `discrete` is TRUE; [ggplot2::discrete_scale()] when `discrete` is FALSE
64+
#' and `interpolate` is TRUE; and [ggplot2::scale_fill_manual()] when `discrete`
65+
#' is FALSE and `interpolate` is FALSE.
4766
#' @examples
4867
#' library(ggplot2)
4968
#' ggplot(mpg, aes(x = hwy, y = cty, fill = cyl)) +
5069
#' geom_point(shape = 21) +
5170
#' theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
5271
#' scale_fill_nmfs(palette = "crustacean", discrete = FALSE)
72+
#'
73+
#' ggplot(mtcars, aes(mpg, disp, color = as.factor(gear))) +
74+
#' geom_point(size = 4) +
75+
#' scale_fill_nmfs("regional",
76+
#' interpolate = FALSE,
77+
#' discrete = TRUE)
5378
#' @export
5479
scale_fill_nmfs <- function(
5580
palette = "oceans",
5681
discrete = TRUE,
5782
reverse = FALSE,
83+
interpolate = TRUE,
5884
...) {
5985
pal <- nmfs_palette(palette = palette, reverse = reverse)
6086

87+
pal_length <- length(nmfs_palettes[[palette]])
88+
6189
if (discrete) {
62-
ggplot2::discrete_scale(
63-
aesthetics = "fill",
64-
palette = pal,
65-
...
66-
)
90+
if (interpolate){
91+
ggplot2::discrete_scale(
92+
aesthetics = "fill",
93+
palette = pal,
94+
...
95+
)
96+
} else {
97+
cli::cli_alert_info("The {palette} palette has {pal_length} colors.")
98+
rlang::warn(message = "An error will occur if there are too few palette colors for your plot.
99+
To avoid this error, use a larger palette or `interpolate = TRUE`.",
100+
.frequency = "once",
101+
.frequency_id = "too_few_colors_warning_fill")
102+
ggplot2::scale_fill_manual(
103+
values = nmfs_palette(palette)(pal_length),
104+
...)
105+
}
67106
} else {
68107
ggplot2::scale_fill_gradientn(colours = pal(256), ...)
69108
}

man/scale_color_nmfs.Rd

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

man/scale_fill_nmfs.Rd

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

0 commit comments

Comments
 (0)