1 |
#' Color Palettes Used in NEST |
|
2 |
#' |
|
3 |
#' @description `r lifecycle::badge("stable")` |
|
4 |
#' |
|
5 |
#' A standardized color palette to be used for all plots within |
|
6 |
#' the NEST project. |
|
7 |
#' |
|
8 |
#' @param n (\code{numeric} value) \cr |
|
9 |
#' The number of colors to be returned from |
|
10 |
#' the color palettes. Please note the colors will be repeated after |
|
11 |
#' a certain numerical limit per palette: |
|
12 |
#' |
|
13 |
#' \itemize{ |
|
14 |
#' \item{\code{nest}} {96 colors} |
|
15 |
#' \item{\code{stream}} {38 colors} |
|
16 |
#' \item{\code{viridis}} {49 colors} |
|
17 |
#' \item{\code{accessible}} {18 colors} |
|
18 |
#' } |
|
19 |
#' |
|
20 |
#' @param palette (\code{character} value)\cr |
|
21 |
#' The name of a palette supported by this function |
|
22 |
#' |
|
23 |
#' \itemize{ |
|
24 |
#' \item{\code{nest}} {A color palette developed by the NEST team representing colors in the NEST logo.} |
|
25 |
#' \item{\code{stream}} {A color palette proposed by the stream documentation of stream version \code{2_05}} |
|
26 |
#' \item{\code{viridis}} {A color palette provided by the \code{viridis::plasma} function.} |
|
27 |
#' \item{\code{accessible}} {A color palette consisting of accessible colors for those with color blindness.} |
|
28 |
#' } |
|
29 |
#' |
|
30 |
#' @examples |
|
31 |
#' library(grid) |
|
32 |
#' |
|
33 |
#' plot_pal <- function(x) { |
|
34 |
#' grid.newpage() |
|
35 |
#' pushViewport(plotViewport(rep(1, 4))) |
|
36 |
#' pushViewport(viewport(layout = grid.layout(1, ncol = length(x)))) |
|
37 |
#' for (i in seq_along(x)) { |
|
38 |
#' grid.rect( |
|
39 |
#' gp = gpar(fill = x[i], col = NA), |
|
40 |
#' vp = viewport(layout.pos.col = i, layout.pos.row = 1) |
|
41 |
#' ) |
|
42 |
#' } |
|
43 |
#' } |
|
44 |
#' |
|
45 |
#' plot_pal(color_palette(n = 10, palette = "nest")) |
|
46 |
#' |
|
47 |
#' plot_pal(color_palette(n = 10, palette = "stream")) |
|
48 |
#' |
|
49 |
#' plot_pal(color_palette(n = 10, palette = "viridis")) |
|
50 |
#' |
|
51 |
#' plot_pal(color_palette(n = 10, palette = "accessible")) |
|
52 |
#' |
|
53 |
#' @export |
|
54 |
color_palette <- function(n = 10, palette = "nest") { |
|
55 |
# Validate inputs |
|
56 | 5x |
checkmate::assert_string(palette) |
57 | 5x |
checkmate::assert_number(n) |
58 | ||
59 |
# Define colors |
|
60 | 5x |
palettes <- list( |
61 | 5x |
"nest" = c( |
62 | 5x |
"#ff2951ff", "#2995ffff", "#81832Bff", "#AC1CF8", "#1CF853", "#F8761C", "#F8341C", |
63 | 5x |
"#F81CC5", "#2D1CF8", "#1CF8DC", "#6EF81C", "#F8B81C", "#F81C25", "#F81C85", "#EC1CF8", |
64 | 5x |
"#6D1CF8", "#1C4BF8", "#1CD0F8", "#1CF898", "#2AF81C", "#B3F81C", "#F8D91C", "#F8971C", |
65 | 5x |
"#F8551C", "#F8241C", "#F81C35", "#F81C65", "#F81CA5", "#F81CE5", "#CC1CF8", "#8D1CF8", |
66 | 5x |
"#4D1CF8", "#1C2BF8", "#1C6BF8", "#1CAEF8", "#1CF2F8", "#1CF8BA", "#1CF876", "#1CF831", |
67 | 5x |
"#4CF81C", "#91F81C", "#D5F81C", "#F8E91C", "#F8C91C", "#F8A81C", "#F8871C", "#F8661C", |
68 | 5x |
"#F8451C", "#F81C1D", "#F81C2D", "#F81C3D", "#F81C55", "#F81C75", "#F81C95", "#F81CB5", |
69 | 5x |
"#F81CD5", "#F81CF5", "#DC1CF8", "#BC1CF8", "#9D1CF8", "#7D1CF8", "#5D1CF8", "#3D1CF8", |
70 | 5x |
"#1D1CF8", "#1C3BF8", "#1C5BF8", "#1C7BF8", "#1C9DF8", "#1CBFF8", "#1CE1F8", "#1CF8ED", |
71 | 5x |
"#1CF8CB", "#1CF8A9", "#1CF887", "#1CF865", "#1CF842", "#1CF820", "#3BF81C", "#5DF81C", |
72 | 5x |
"#80F81C", "#A2F81C", "#C4F81C", "#E6F81C", "#F8F11C", "#F8E11C", "#F8D11C", "#F8C11C", |
73 | 5x |
"#F8B01C", "#F8A01C", "#F88F1C", "#F87F1C", "#F86E1C", "#F85E1C", "#F84D1C", "#F83D1C", |
74 | 5x |
"#F82C1C" |
75 |
), |
|
76 | 5x |
"stream" = c( |
77 | 5x |
"#343cff", "#ff484b", "#232323", "#329032", "#ffa41c", "#750375", "#1d831c", "#767676", |
78 | 5x |
"#9b2525", "#008080", "#ff1f8e", "#bdb771", "#ffd92d", "#000bff", "#ff0004", "#090909", |
79 | 5x |
"#379336", "#ffa722", "#750375", "#2f9999", "#ff2c95", "#bfb976", "#ffdd41", "#3438ff", |
80 | 5x |
"#ff0004", "#000000", "#047503", "#ff9a05", "#7a0d7a", "#a94343", "#008080", "#ffa825", |
81 | 5x |
"#ff1388", "#bdb771", "#ffe258", "#141dff", "#ff0004", "#000000" |
82 |
), |
|
83 | 5x |
"viridis" = c( |
84 | 5x |
"#0D0887", "#FB9E3B", "#FDC927", "#D8576C", "#47039F", "#BE3686", "#47039F", |
85 | 5x |
"#7301A8", "#ED7953", "#9C179E", "#F0F921", "#BE3686", "#47039F", "#BE3686", |
86 | 5x |
"#ED7953", "#0D0887", "#47039F", "#9C179E", "#D8576C", "#F0F921", "#D8576C", |
87 | 5x |
"#FB9E3B", "#F0F921", "#FDC927", "#0D0887", "#7301A8", "#FB9E3B", "#9C179E", |
88 | 5x |
"#FDC927", "#7301A8", "#F0F921", "#ED7953", "#BE3686", "#F0F921", "#D8576C", |
89 | 5x |
"#9C179E", "#D8576C", "#9C179E", "#0D0887", "#47039F", "#FDC927", "#FDC927", |
90 | 5x |
"#FB9E3B", "#FB9E3B", "#7301A8", "#BE3686", "#ED7953", "#ED7953", "#7301A8" |
91 |
), |
|
92 | ||
93 |
# Inclusive/accessible color palettes |
|
94 | 5x |
"accessible" = c( |
95 | 5x |
"#E8F086", "#6FDE6E", "#FF4242", "#A691AE", "#235FA4", "#0A284B", |
96 | 5x |
"#BDD9BF", "#929084", "#FFC857", "#A997DF", "#E5323B", "#2E4052", |
97 | 5x |
"#E1DAAE", "#FF934F", "#CC2D35", "#058ED9", "#848FA2", "#2D3142" |
98 |
) |
|
99 |
) |
|
100 | ||
101 |
# Validate input |
|
102 | 5x |
match.arg(palette, names(palettes)) |
103 | ||
104 |
# Get colors for specified palette/color scheme |
|
105 | 5x |
color_scheme <- unlist( |
106 | 5x |
palettes[palette], |
107 | 5x |
use.names = FALSE |
108 |
) |
|
109 | ||
110 |
# Return vector of colors |
|
111 | 5x |
colors <- rep( |
112 | 5x |
color_scheme, |
113 | 5x |
ceiling( |
114 | 5x |
n / length(color_scheme) |
115 |
) |
|
116 |
) |
|
117 | 5x |
return(colors[seq_len(n)]) |
118 |
} |
1 |
.onLoad <- function(libname, pkgname) { # nolint |
|
2 | ! |
pal <- color_palette(n = 20, palette = "stream") |
3 | ! |
options( |
4 | ! |
ggplot2.discrete.colour = pal, |
5 | ! |
ggplot2.discrete.fill = pal |
6 |
) |
|
7 | ! |
invisible() |
8 |
} |
1 |
#' Returns a custom `NEST` `ggplot2` theme |
|
2 |
#' |
|
3 |
#' @param font_size (`numeric`) font size to be passed to `[ggplot2::element_text()]` |
|
4 |
#' @param ... (`list`) additional arguments to `[ggplot2::theme()]` |
|
5 |
#' |
|
6 |
#' @export |
|
7 |
#' @examples |
|
8 |
#' plot <- ggplot2::ggplot(iris, ggplot2::aes(x = Sepal.Length, y = Sepal.Width)) + |
|
9 |
#' ggplot2::geom_point() + |
|
10 |
#' theme_nest() |
|
11 |
theme_nest <- function(font_size = 10, ...) { |
|
12 | 4x |
checkmate::assert_number(font_size, finite = TRUE) |
13 | 2x |
ggplot2::theme( |
14 | 2x |
panel.grid.major = ggplot2::element_blank(), |
15 | 2x |
panel.grid.minor = ggplot2::element_blank(), |
16 | 2x |
panel.background = ggplot2::element_blank(), |
17 | 2x |
panel.border = ggplot2::element_rect(colour = "grey", fill = NA, linewidth = 1), |
18 | 2x |
legend.position = "bottom", |
19 | 2x |
legend.background = ggplot2::element_blank(), |
20 | 2x |
legend.box.background = ggplot2::element_rect(colour = "grey", fill = NA, linewidth = 1), |
21 | 2x |
legend.direction = "horizontal", |
22 | 2x |
legend.title = ggplot2::element_text(face = "bold"), |
23 | 2x |
text = ggplot2::element_text(size = font_size), |
24 | 2x |
plot.caption = ggplot2::element_text(hjust = 0), |
25 |
... |
|
26 |
) |
|
27 |
} |