Skip to content

Commit 5d0dfcb

Browse files
committed
Use global options to control default continuous colour/fill scales
1 parent bad0d01 commit 5d0dfcb

File tree

6 files changed

+98
-12
lines changed

6 files changed

+98
-12
lines changed

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ Collate:
170170
'scale-.r'
171171
'scale-alpha.r'
172172
'scale-brewer.r'
173+
'scale-colour.r'
173174
'scale-continuous.r'
174175
'scale-date.r'
175176
'scale-discrete-.r'

R/scale-colour.r

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#' Continuous colour scales
2+
#'
3+
#' Colour scales for continuous data default to the values of the
4+
#' `ggplot2.continuous.colour` and `ggplot2.continuous.fill` options. If these
5+
#' options are not present, ggplot2 will set them to `"gradient"`. See
6+
#' [options()] for more information.
7+
#'
8+
#' @param ... Additional parameters passed on to the scale type
9+
#' @param type One of "gradient" (the default) or "viridis" indicating the
10+
#' colour scale to use
11+
#' @seealso [scale_colour_gradient()], [scale_colour_viridis_c()],
12+
#' [scale_fill_gradient()], and [scale_fill_viridis_c()]
13+
#' @export
14+
#' @rdname scale_colour_continuous
15+
#' @examples
16+
#' v <- ggplot(faithfuld, aes(waiting, eruptions, fill = density)) +
17+
#' geom_tile()
18+
#' v
19+
#'
20+
#' v + scale_fill_continuous(type = "gradient")
21+
#' v + scale_fill_continuous(type = "viridis")
22+
#'
23+
#' # The above are equivalent to
24+
#' v + scale_fill_gradient()
25+
#' v + scale_fill_viridis_c()
26+
scale_colour_continuous <- function(...,
27+
type = getOption("ggplot2.continuous.colour")) {
28+
switch(
29+
type,
30+
gradient = scale_colour_gradient(...),
31+
viridis = scale_colour_viridis_c(...),
32+
stop("Unknown scale type", call. = FALSE)
33+
)
34+
}
35+
36+
#' @rdname scale_colour_continuous
37+
#' @export
38+
scale_fill_continuous <- function(...,
39+
type = getOption("ggplot2.continuous.fill")) {
40+
switch(
41+
type,
42+
gradient = scale_fill_gradient(...),
43+
viridis = scale_fill_viridis_c(...),
44+
stop("Unknown scale type", call. = FALSE)
45+
)
46+
}

R/zxx.r

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@
55
#' @usage NULL
66
scale_colour_discrete <- scale_colour_hue
77

8-
#' @export
9-
#' @rdname scale_gradient
10-
#' @usage NULL
11-
scale_colour_continuous <- scale_colour_gradient
12-
138
#' @export
149
#' @rdname scale_gradient
1510
#' @usage NULL
@@ -29,11 +24,6 @@ scale_colour_date <- function() {
2924
#' @usage NULL
3025
scale_fill_discrete <- scale_fill_hue
3126

32-
#' @export
33-
#' @rdname scale_gradient
34-
#' @usage NULL
35-
scale_fill_continuous <- scale_fill_gradient
36-
3727
#' @export
3828
#' @rdname scale_gradient
3929
#' @usage NULL

R/zzz.r

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,15 @@ release_questions <- function() {
1919
"Have you built the book?"
2020
)
2121
}
22+
23+
.onLoad <- function(libname, pkgname) {
24+
op <- options()
25+
op.ggplot2 <- list(
26+
ggplot2.continuous.colour = "gradient",
27+
ggplot2.continuous.fill = "gradient"
28+
)
29+
toset <- !(names(op.ggplot2) %in% names(op))
30+
if(any(toset)) options(op.ggplot2[toset])
31+
32+
invisible()
33+
}

man/scale_colour_continuous.Rd

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

man/scale_gradient.Rd

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

0 commit comments

Comments
 (0)