|
6 | 6 | #' Default is TRUE. |
7 | 7 | #' @param reverse Boolean indicating whether the palette should be reversed. |
8 | 8 | #' 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. |
12 | 16 | #' @examples |
13 | 17 | #' library(ggplot2) |
14 | 18 | #' ggplot(iris, aes(Sepal.Width, Sepal.Length, color = Species)) + |
15 | 19 | #' geom_point(size = 4) + |
16 | 20 | #' 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) |
17 | 27 | #' @export |
18 | 28 | scale_color_nmfs <- function( |
19 | 29 | palette = "oceans", |
20 | 30 | discrete = TRUE, |
21 | 31 | reverse = FALSE, |
| 32 | + interpolate = TRUE, |
22 | 33 | ...) { |
23 | 34 | pal <- nmfs_palette(palette = palette, reverse = reverse) |
24 | 35 |
|
| 36 | + pal_length <- length(nmfs_palettes[[palette]]) |
| 37 | + |
25 | 38 | 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 | + } |
31 | 55 | } else { |
32 | 56 | ggplot2::scale_color_gradientn(colours = pal(256), ...) |
33 | 57 | } |
34 | 58 | } |
35 | 59 |
|
36 | 60 | #' 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. |
47 | 66 | #' @examples |
48 | 67 | #' library(ggplot2) |
49 | 68 | #' ggplot(mpg, aes(x = hwy, y = cty, fill = cyl)) + |
50 | 69 | #' geom_point(shape = 21) + |
51 | 70 | #' theme(axis.text.x = element_text(angle = 45, hjust = 1)) + |
52 | 71 | #' 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) |
53 | 78 | #' @export |
54 | 79 | scale_fill_nmfs <- function( |
55 | 80 | palette = "oceans", |
56 | 81 | discrete = TRUE, |
57 | 82 | reverse = FALSE, |
| 83 | + interpolate = TRUE, |
58 | 84 | ...) { |
59 | 85 | pal <- nmfs_palette(palette = palette, reverse = reverse) |
60 | 86 |
|
| 87 | + pal_length <- length(nmfs_palettes[[palette]]) |
| 88 | + |
61 | 89 | 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 | + } |
67 | 106 | } else { |
68 | 107 | ggplot2::scale_fill_gradientn(colours = pal(256), ...) |
69 | 108 | } |
|
0 commit comments