Skip to content

Commit 2cdd629

Browse files
committed
fallbacks for fallbacks
1 parent eef7ef7 commit 2cdd629

File tree

1 file changed

+55
-8
lines changed

1 file changed

+55
-8
lines changed

R/scale-.R

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,10 @@ continuous_scale <- function(aesthetics, scale_name = deprecated(), palette, nam
124124
aesthetics <- standardise_aes_names(aesthetics)
125125

126126
check_breaks_labels(breaks, labels, call = call)
127-
check_fallback_palette(palette, fallback.palette, call = call)
127+
fallback.palette <- validate_fallback_palette(
128+
palette, fallback.palette, aesthetics, discrete = FALSE,
129+
call = call
130+
)
128131

129132
position <- arg_match0(position, c("left", "right", "top", "bottom"))
130133

@@ -227,7 +230,10 @@ discrete_scale <- function(aesthetics, scale_name = deprecated(), palette, name
227230
aesthetics <- standardise_aes_names(aesthetics)
228231

229232
check_breaks_labels(breaks, labels, call = call)
230-
check_fallback_palette(palette, fallback.palette, call = call)
233+
fallback.palette <- validate_fallback_palette(
234+
palette, fallback.palette, aesthetics, discrete = TRUE,
235+
call = call
236+
)
231237

232238
# Convert formula input to function if appropriate
233239
limits <- allow_lambda(limits)
@@ -327,7 +333,10 @@ binned_scale <- function(aesthetics, scale_name = deprecated(), palette, name =
327333
aesthetics <- standardise_aes_names(aesthetics)
328334

329335
check_breaks_labels(breaks, labels, call = call)
330-
check_fallback_palette(palette, fallback.palette, call = call)
336+
fallback.palette <- validate_fallback_palette(
337+
palette, fallback.palette, aesthetics, discrete = FALSE,
338+
call = call
339+
)
331340

332341
position <- arg_match0(position, c("left", "right", "top", "bottom"))
333342

@@ -1786,16 +1795,54 @@ check_continuous_limits <- function(limits, ...,
17861795
check_length(limits, 2L, arg = arg, call = call)
17871796
}
17881797

1789-
check_fallback_palette <- function(pal, fallback, call = caller_env()) {
1798+
allow_lambda <- function(x) {
1799+
# we check the 'call' class to prevent interpreting `bquote()` calls as a function
1800+
if (is_formula(x, lhs = FALSE) && !inherits(x, "call")) as_function(x) else x
1801+
}
1802+
1803+
validate_fallback_palette <- function(pal, fallback, aesthetic = "x",
1804+
discrete = FALSE, call = caller_env()) {
17901805
if (!is.null(pal) || is.function(fallback)) {
1791-
return(invisible())
1806+
return(pal %||% fallback)
1807+
}
1808+
aesthetic <- standardise_aes_names(aesthetic[1])
1809+
if (discrete) {
1810+
pal <- fallback_palette_discrete(aesthetic)
1811+
} else {
1812+
pal <- fallback_palette_continuous(aesthetic)
1813+
}
1814+
if (!is.null(pal)) {
1815+
return(pal)
17921816
}
17931817
cli::cli_abort(
17941818
"When {.code palette = NULL}, the {.arg fallback.palette} must be defined."
17951819
)
17961820
}
17971821

1798-
allow_lambda <- function(x) {
1799-
# we check the 'call' class to prevent interpreting `bquote()` calls as a function
1800-
if (is_formula(x, lhs = FALSE) && !inherits(x, "call")) as_function(x) else x
1822+
fallback_palette_discrete <- function(aesthetic) {
1823+
switch(
1824+
aesthetic,
1825+
colour = ,
1826+
fill = pal_hue(),
1827+
alpha = function(n) seq(0.1, 1, length.out = n),
1828+
linewidth = function(n) seq(2, 6, length.out = n),
1829+
linetype = pal_linetype(),
1830+
shape = pal_shape(),
1831+
size = function(n) sqrt(seq(4, 36, length.out = n)),
1832+
ggplot_global$theme_default[[paste0("palette.", aesthetic, ".discrete")]]
1833+
)
1834+
}
1835+
1836+
fallback_palette_continuous <- function(aesthetic) {
1837+
switch(
1838+
aesthetic,
1839+
colour = ,
1840+
fill = pal_seq_gradient("#132B43", "#56B1F7"),
1841+
alpha = pal_rescale(c(0.1, 1)),
1842+
linewidth = pal_rescale(c(1, 6)),
1843+
linetype = pal_binned(pal_linetype()),
1844+
shape = pal_binned(pal_shape()),
1845+
size = pal_area(),
1846+
ggplot_global$theme_default[[paste0("palette.", aes, ".continuous")]]
1847+
)
18011848
}

0 commit comments

Comments
 (0)