Skip to content

Commit 70986e6

Browse files
authored
Don't switch scale type when using defaults (#4456)
1 parent 3867a49 commit 70986e6

File tree

3 files changed

+85
-36
lines changed

3 files changed

+85
-36
lines changed

R/scale-colour.r

Lines changed: 61 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,11 @@
7474
#' options(ggplot2.continuous.fill = tmp) # restore previous setting
7575
#' @export
7676
scale_colour_continuous <- function(...,
77-
type = getOption("ggplot2.continuous.colour", default = "gradient")) {
77+
type = getOption("ggplot2.continuous.colour")) {
78+
type <- type %||% "gradient"
79+
7880
if (is.function(type)) {
79-
type(...)
81+
check_scale_type(type(...), "scale_colour_continuous", "colour")
8082
} else if (identical(type, "gradient")) {
8183
scale_colour_gradient(...)
8284
} else if (identical(type, "viridis")) {
@@ -89,9 +91,11 @@ scale_colour_continuous <- function(...,
8991
#' @rdname scale_colour_continuous
9092
#' @export
9193
scale_fill_continuous <- function(...,
92-
type = getOption("ggplot2.continuous.fill", default = "gradient")) {
94+
type = getOption("ggplot2.continuous.fill")) {
95+
type <- type %||% "gradient"
96+
9397
if (is.function(type)) {
94-
type(...)
98+
check_scale_type(type(...), "scale_fill_continuous", "fill")
9599
} else if (identical(type, "gradient")) {
96100
scale_fill_gradient(...)
97101
} else if (identical(type, "viridis")) {
@@ -104,29 +108,68 @@ scale_fill_continuous <- function(...,
104108
#' @export
105109
#' @rdname scale_colour_continuous
106110
scale_colour_binned <- function(...,
107-
type = getOption("ggplot2.binned.colour", default = getOption("ggplot2.continuous.colour", default = "gradient"))) {
111+
type = getOption("ggplot2.binned.colour")) {
108112
if (is.function(type)) {
109-
type(...)
110-
} else if (identical(type, "gradient")) {
111-
scale_colour_steps(...)
112-
} else if (identical(type, "viridis")) {
113-
scale_colour_viridis_b(...)
113+
check_scale_type(type(...), "scale_colour_binned", "colour")
114114
} else {
115-
abort("Unknown scale type")
115+
type_fallback <- getOption("ggplot2.continuous.colour", default = "gradient")
116+
# don't use fallback from scale_colour_continuous() if it is
117+
# a function, since that would change the type of the color
118+
# scale from binned to continuous
119+
if (is.function(type_fallback)) {
120+
type_fallback <- "gradient"
121+
}
122+
type <- type %||% type_fallback
123+
124+
if (identical(type, "gradient")) {
125+
scale_colour_steps(...)
126+
} else if (identical(type, "viridis")) {
127+
scale_colour_viridis_b(...)
128+
} else {
129+
abort("Unknown scale type")
130+
}
116131
}
117132
}
118133

119134
#' @export
120135
#' @rdname scale_colour_continuous
121136
scale_fill_binned <- function(...,
122-
type = getOption("ggplot2.binned.fill", default = getOption("ggplot2.continuous.fill", default = "gradient"))) {
137+
type = getOption("ggplot2.binned.fill")) {
123138
if (is.function(type)) {
124-
type(...)
125-
} else if (identical(type, "gradient")) {
126-
scale_fill_steps(...)
127-
} else if (identical(type, "viridis")) {
128-
scale_fill_viridis_b(...)
139+
check_scale_type(type(...), "scale_fill_binned", "fill")
129140
} else {
130-
abort("Unknown scale type")
141+
type_fallback <- getOption("ggplot2.continuous.fill", default = "gradient")
142+
# don't use fallback from scale_colour_continuous() if it is
143+
# a function, since that would change the type of the color
144+
# scale from binned to continuous
145+
if (is.function(type_fallback)) {
146+
type_fallback <- "gradient"
147+
}
148+
type <- type %||% type_fallback
149+
150+
if (identical(type, "gradient")) {
151+
scale_fill_steps(...)
152+
} else if (identical(type, "viridis")) {
153+
scale_fill_viridis_b(...)
154+
} else {
155+
abort("Unknown scale type")
156+
}
157+
}
158+
}
159+
160+
161+
# helper function to make sure that the provided scale is of the correct
162+
# type (i.e., is continuous and works with the provided aesthetic)
163+
check_scale_type <- function(scale, name, aesthetic) {
164+
if (!is.ggproto(scale) || !inherits(scale, "Scale")) {
165+
abort(glue("The `type` argument of `{name}()` must return a continuous scale for the {aesthetic} aesthetic. The provided object is not a scale function."))
166+
}
167+
if (!isTRUE(aesthetic %in% scale$aesthetics)) {
168+
abort(glue("The `type` argument of `{name}()` must return a continuous scale for the {aesthetic} aesthetic. The provided scale works with the following aesthetics: {glue_collapse(scale$aesthetics, sep = ', ')}"))
131169
}
170+
if (isTRUE(scale$is_discrete())) {
171+
abort(glue("The `type` argument of `{name}()` must return a continuous scale for the {aesthetic} aesthetic, but the provided scale is discrete."))
172+
}
173+
174+
scale
132175
}

man/scale_colour_continuous.Rd

Lines changed: 4 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
context("test-scale-colour-continuous.R")
2+
3+
test_that("type argument is checked for proper input", {
4+
expect_error(
5+
scale_colour_continuous(type = function() "abc"),
6+
"is not a scale function"
7+
)
8+
expect_error(
9+
scale_fill_continuous(type = geom_point),
10+
"is not a scale function"
11+
)
12+
expect_error(
13+
scale_colour_binned(type = function(...) scale_colour_binned(aesthetics = c("fill", "point_colour"))),
14+
"works with the following aesthetics: fill, point_colour"
15+
)
16+
expect_error(
17+
scale_fill_binned(type = scale_fill_brewer),
18+
"provided scale is discrete"
19+
)
20+
})

0 commit comments

Comments
 (0)