Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitece1d96

Browse files
authored
Internalise palette registry (#479)
1 parentd513781 commitece1d96

File tree

7 files changed

+178
-65
lines changed

7 files changed

+178
-65
lines changed

‎NAMESPACE‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ export(extended_breaks)
115115
export(format_format)
116116
export(format_log)
117117
export(fullseq)
118-
export(get_palette)
119118
export(gradient_n_pal)
120119
export(grey_pal)
121120
export(hms_trans)
@@ -199,7 +198,6 @@ export(pal_seq_gradient)
199198
export(pal_shape)
200199
export(pal_viridis)
201200
export(palette_na_safe)
202-
export(palette_names)
203201
export(palette_nlevels)
204202
export(palette_type)
205203
export(parse_format)
@@ -218,12 +216,10 @@ export(rescale_max)
218216
export(rescale_mid)
219217
export(rescale_none)
220218
export(rescale_pal)
221-
export(reset_palettes)
222219
export(reverse_trans)
223220
export(scientific)
224221
export(scientific_format)
225222
export(seq_gradient_pal)
226-
export(set_palette)
227223
export(shape_pal)
228224
export(show_col)
229225
export(sqrt_trans)

‎R/pal-.R‎

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,89 @@
1+
#' @name palette-recommendations
2+
#' @title Recommendations for colour palettes
3+
#' @description
4+
#' For the purposes of these recommendations, we define a palette as a function
5+
#' that either takes an `n` argument for the number of desired output colours or
6+
#' a `value` argument between 0-1 representing how far along a gradient output
7+
#' colours should be. The palette then returns a number of colours equal to `n`
8+
#' or `length(x)`.
9+
#'
10+
#' The convention in the scales package is to name functions that generate
11+
#' palettes (palette factories) with the `pal_`-prefix. The benefit of
12+
#' factories is that you can easily parameterise palettes, for example giving
13+
#' options for how a palette should be constructed.
14+
#'
15+
#' In the example below `pal_aurora()` is a palette factory parameterised by
16+
#' a `direction` argument.
17+
#'
18+
#' ```r
19+
#' pal_aurora <- function(direction = 1) {
20+
#' colours <- c("palegreen", "deepskyblue", "magenta")
21+
#' if (sign(direction) == -1) {
22+
#' colours <- rev(colours)
23+
#' }
24+
#' pal_manual(colours, type = "colour")
25+
#' }
26+
#'
27+
#' class(pal_aurora())
28+
#' #> [1] "pal_discrete" "scales_pal" "function"
29+
#' ```
30+
#'
31+
#' It is recommended that a palette factory returns a function with either the
32+
#' `pal_discrete` or `pal_continuous` class. If your factory constructs a
33+
#' plain vector of colours, then `pal_manual(type = "colour")` or
34+
#' `pal_gradient_n()` are useful to return a classed palette for this common
35+
#' use case.
36+
#'
37+
#' When your inner palette function does not return a defined vector of colours,
38+
#' it is recommended to use `new_discrete_palette` and `new_continuous_palette`
39+
#' instead and supplement the additional `type` and `na_safe`/`nlevels`
40+
#' properties. This should allow easy translation between discrete and
41+
#' continuous palettes.
42+
#'
43+
#' ```r
44+
#' pal_random <- function() {
45+
#' fun <- function(n) {
46+
#' sample(colours(distinct = TRUE), size = n)
47+
#' }
48+
#' new_discrete_palette(fun, type = "colour", nlevels = length(colours()))
49+
#' }
50+
#' ```
51+
#'
52+
#' If you don't have parameterised palettes, but also if you have palette
53+
#' factories, it is encouraged to export an (inner) palette function or
54+
#' plain colour vector. This is in addition to exporting the palette factory.
55+
#' Exporting this makes it easy for users to specify for example
56+
#' `as_continuous_pal(mypackage::aurora)`.
57+
#'
58+
#' ```
59+
#' #' @export
60+
#' aurora <- pal_aurora()
61+
#'
62+
#' # or:
63+
#'
64+
#' #' @export
65+
#' aurora <- c("palegreen", "deepskyblue", "magenta")
66+
#' ```
67+
#'
68+
#' Lastly, for testing purposes we encourage that your palettes can be
69+
#' interpreted both as discrete palette, but also a continuous palette.
70+
#' To test for this, you can test the output of `as_discrete_pal()` and
71+
#' `as_continuous_pal()`.
72+
#'
73+
#' ```r
74+
#' test_that("pal_aurora can be discrete or continuous", {
75+
#'
76+
#' my_pal <- pal_aurora()
77+
#' colours <- c("palegreen", "deepskyblue", "magenta")
78+
#'
79+
#' expect_equal(as_discrete_pal(my_pal)(3), colours)
80+
#' expect_equal(as_continuous_pal(my_pal)(c(0, 0.5, 1)), alpha(colours, NA))
81+
#'
82+
#' })
83+
#' ```
84+
#' @seealso [palette utilities][new_continuous_palette]
85+
NULL
86+
187
# Constructors ------------------------------------------------------------
288

389
#' Constructors for palettes
@@ -28,6 +114,7 @@
28114
#' For `palette_nlevels()` a single integer. For `palette_na_safe()` a boolean.
29115
#' For `palette_type()` a string.
30116
#' @export
117+
#' @seealso [palette recommendations][palette-recommendations]
31118
#'
32119
#' @examples
33120
#' # Creating a new discrete palette

‎R/palette-registry.R‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#'
1717
#' @return The `get_palette()` function returns a palette. The `set_palette()`
1818
#' function is called for side effects and returns nothing.
19-
#' @export
19+
#' @noRd
2020
#'
2121
#' @examples
2222
#' # Get one of the known palettes
@@ -61,8 +61,6 @@ get_palette <- function(name, ...) {
6161
cli::cli_abort("Failed to interpret {name} as palette.")
6262
}
6363

64-
#' @export
65-
#' @rdname get_palette
6664
set_palette<-function(name,palette,warn_conflict=TRUE) {
6765
name<- tolower(name)
6866
if (!is_function(palette)&&!is_character(palette)) {
@@ -78,14 +76,10 @@ set_palette <- function(name, palette, warn_conflict = TRUE) {
7876
invisible(NULL)
7977
}
8078

81-
#' @export
82-
#' @rdname get_palette
8379
palette_names<-function() {
8480
names(.known_palettes)
8581
}
8682

87-
#' @export
88-
#' @rdname get_palette
8983
reset_palettes<-function() {
9084
env_unbind(.known_palettes, palette_names())
9185
init_palettes()

‎_pkgdown.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ reference:
7171
-contains("col")
7272
-muted
7373
-alpha
74-
-get_palette
74+
-palette-recommendations
7575

7676
-title:Non-colour palette functions
7777
desc:>

‎man/get_palette.Rd‎

Lines changed: 0 additions & 53 deletions
This file was deleted.

‎man/new_continuous_palette.Rd‎

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

‎man/palette-recommendations.Rd‎

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

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp