Skip to content

Commit cc3951c

Browse files
authored
Improve color scale documentation (#4450)
* Improve color scale documentation * more detailed documentation * manual text wrap
1 parent b8cc043 commit cc3951c

11 files changed

+112
-17
lines changed

R/scale-colour.r

+38-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,31 @@
11
#' Continuous and binned colour scales
22
#'
3-
#' Colour scales for continuous data default to the values of the
4-
#' `ggplot2.continuous.colour` and `ggplot2.continuous.fill` options. These
5-
#' [options()] default to `"gradient"` (i.e., [scale_colour_gradient()] and
6-
#' [scale_fill_gradient()])
3+
#' The scales `scale_colour_continuous()` and `scale_fill_continuous()` are
4+
#' the default colour scales ggplot2 uses when continuous data values are
5+
#' mapped onto the `colour` or `fill` aesthetics, respectively. The scales
6+
#' `scale_colour_binned()` and `scale_fill_binned()` are equivalent scale
7+
#' functions that assign discrete color bins to the continuous values
8+
#' instead of using a continuous color spectrum.
9+
#'
10+
#' All these colour scales use the [options()] mechanism to determine
11+
#' default settings. Continuous colour scales default to the values of the
12+
#' `ggplot2.continuous.colour` and `ggplot2.continuous.fill` options, and
13+
#' binned colour scales default to the values of the `ggplot2.binned.colour`
14+
#' and `ggplot2.binned.fill` options. These option values default to
15+
#' `"gradient"`, which means that the scale functions actually used are
16+
#' [scale_colour_gradient()]/[scale_fill_gradient()] for continuous scales and
17+
#' [scale_colour_steps()]/[scale_fill_steps()] for binned scales.
18+
#' Alternative option values are `"viridis"` or a different scale function.
19+
#' See description of the `type` argument for details.
20+
#'
21+
#' Note that the binned colour scales will use the settings of
22+
#' `ggplot2.continuous.colour` and `ggplot2.continuous.fill` as fallback,
23+
#' respectively, if `ggplot2.binned.colour` or `ggplot2.binned.fill` are
24+
#' not set.
25+
#'
26+
#' These scale functions are meant to provide simple defaults. If
27+
#' you want to manually set the colors of a scale, consider using
28+
#' [scale_colour_gradient()] or [scale_colour_steps()].
729
#'
830
#' @param ... Additional parameters passed on to the scale type
931
#' @param type One of the following:
@@ -13,7 +35,7 @@
1335
#' @seealso [scale_colour_gradient()], [scale_colour_viridis_c()],
1436
#' [scale_colour_steps()], [scale_colour_viridis_b()], [scale_fill_gradient()],
1537
#' [scale_fill_viridis_c()], [scale_fill_steps()], and [scale_fill_viridis_b()]
16-
#' @export
38+
#' @family colour scales
1739
#' @rdname scale_colour_continuous
1840
#' @section Color Blindness:
1941
#' Many color palettes derived from RGB combinations (like the "rainbow" color
@@ -40,6 +62,17 @@
4062
#' # The above are equivalent to
4163
#' v + scale_fill_gradient()
4264
#' v + scale_fill_viridis_c()
65+
#'
66+
#' # To make a binned version of this plot
67+
#' v + scale_fill_binned(type = "viridis")
68+
#'
69+
#' # Set a different default scale using the options
70+
#' # mechanism
71+
#' tmp <- getOption("ggplot2.continuous.fill") # store current setting
72+
#' options(ggplot2.continuous.fill = scale_fill_distiller)
73+
#' v
74+
#' options(ggplot2.continuous.fill = tmp) # restore previous setting
75+
#' @export
4376
scale_colour_continuous <- function(...,
4477
type = getOption("ggplot2.continuous.colour", default = "gradient")) {
4578
if (is.function(type)) {
@@ -70,7 +103,6 @@ scale_fill_continuous <- function(...,
70103

71104
#' @export
72105
#' @rdname scale_colour_continuous
73-
#' @usage NULL
74106
scale_colour_binned <- function(...,
75107
type = getOption("ggplot2.binned.colour", default = getOption("ggplot2.continuous.colour", default = "gradient"))) {
76108
if (is.function(type)) {
@@ -86,7 +118,6 @@ scale_colour_binned <- function(...,
86118

87119
#' @export
88120
#' @rdname scale_colour_continuous
89-
#' @usage NULL
90121
scale_fill_binned <- function(...,
91122
type = getOption("ggplot2.binned.fill", default = getOption("ggplot2.continuous.fill", default = "gradient"))) {
92123
if (is.function(type)) {

R/scale-gradient.r

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
#'
33
#' `scale_*_gradient` creates a two colour gradient (low-high),
44
#' `scale_*_gradient2` creates a diverging colour gradient (low-mid-high),
5-
#' `scale_*_gradientn` creates a n-colour gradient.
5+
#' `scale_*_gradientn` creates a n-colour gradient. For binned variants of
6+
#' these scales, see the [color steps][scale_colour_steps] scales.
67
#'
78
#' Default colours are generated with \pkg{munsell} and
89
#' `mnsl(c("2.5PB 2/4", "2.5PB 7/10"))`. Generally, for continuous
@@ -17,7 +18,7 @@
1718
#' colour bar, or `"legend"` for discrete colour legend.
1819
#' @inheritDotParams continuous_scale -na.value -guide -aesthetics
1920
#' @seealso [scales::seq_gradient_pal()] for details on underlying
20-
#' palette
21+
#' palette, [scale_colour_steps()] for binned variants of these scales.
2122
#' @family colour scales
2223
#' @rdname scale_gradient
2324
#' @export

R/scale-steps.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#' @inheritDotParams binned_scale -aesthetics -scale_name -palette -na.value -guide -rescaler
1717
#'
1818
#' @seealso [scales::seq_gradient_pal()] for details on underlying
19-
#' palette
19+
#' palette, [scale_colour_gradient()] for continuous scales without binning.
2020
#' @family colour scales
2121
#' @export
2222
#' @examples

man/scale_alpha.Rd

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/scale_brewer.Rd

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/scale_colour_continuous.Rd

+59-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/scale_gradient.Rd

+4-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/scale_grey.Rd

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/scale_hue.Rd

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/scale_steps.Rd

+2-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/scale_viridis.Rd

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)