Skip to content

Commit e78f4eb

Browse files
authored
Fix regression in colour/fill scale name redirection (#6642)
* new colour scales feed first argument to `name` * add test * add news bullet * fix mismerged lines * account for interaction with #6691
1 parent c358000 commit e78f4eb

File tree

3 files changed

+36
-9
lines changed

3 files changed

+36
-9
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
* Improved palette fallback mechanism in scales (@teunbrand, #6669).
1616
* Allow `stat` in `geom_hline`, `geom_vline`, and `geom_abline`. (@sierrajohnson, #6559)
1717
* `stat_boxplot()` treats `width` as an optional aesthetic (@Yunuuuu, #6575)
18+
* Fixed regression where the first (unnamed) argument to colour/fill scales was
19+
not passed as the `name` argument (@teunbrand, #6623)
1820

1921
# ggplot2 4.0.0
2022

R/scale-colour.R

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
#' * a single string naming a palette.
2727
#' * a palette function that when called with a numeric vector with values
2828
#' between 0 and 1 returns the corresponding output values.
29-
#' @inheritDotParams continuous_scale -scale_name -trans -minor_breaks -expand
30-
#' @inheritDotParams binned_scale -scale_name -trans -expand
29+
#' @inheritDotParams continuous_scale -scale_name -trans -minor_breaks -expand -fallback.palette
30+
#' @inheritDotParams binned_scale -scale_name -trans -expand -fallback.palette
3131
#' @param type `r lifecycle::badge("superseded")` The preferred mechanism for
3232
#' setting the default palette is by using the theme. For example:
3333
#' `theme(palette.colour.discrete = "viridis")`.
@@ -91,7 +91,8 @@ scale_colour_continuous <- function(..., palette = NULL, aesthetics = "colour",
9191
}
9292
palette <- if (!is.null(palette)) as_continuous_pal(palette)
9393
continuous_scale(
94-
aesthetics, palette = palette, guide = guide, na.value = na.value,
94+
aesthetics = aesthetics, palette = palette, guide = guide,
95+
na.value = na.value, scale_name = deprecated(),
9596
fallback.palette = pal_seq_gradient("#132B43", "#56B1F7"),
9697
...
9798
)
@@ -114,7 +115,8 @@ scale_fill_continuous <- function(..., palette = NULL, aesthetics = "fill", guid
114115
}
115116
palette <- if (!is.null(palette)) as_continuous_pal(palette)
116117
continuous_scale(
117-
aesthetics, palette = palette, guide = guide, na.value = na.value,
118+
aesthetics = aesthetics, palette = palette, guide = guide,
119+
na.value = na.value, scale_name = deprecated(),
118120
fallback.palette = pal_seq_gradient("#132B43", "#56B1F7"),
119121
...
120122
)
@@ -137,7 +139,8 @@ scale_colour_binned <- function(..., palette = NULL, aesthetics = "colour", guid
137139
}
138140
palette <- if (!is.null(palette)) pal_binned(as_discrete_pal(palette))
139141
binned_scale(
140-
aesthetics, palette = palette, guide = guide, na.value = na.value,
142+
aesthetics = aesthetics, palette = palette, guide = guide,
143+
na.value = na.value, scale_name = deprecated(),
141144
fallback.palette = pal_seq_gradient("#132B43", "#56B1F7"),
142145
...
143146
)
@@ -159,7 +162,8 @@ scale_fill_binned <- function(..., palette = NULL, aesthetics = "fill", guide =
159162
}
160163
palette <- if (!is.null(palette)) pal_binned(as_discrete_pal(palette))
161164
binned_scale(
162-
aesthetics, palette = palette, guide = guide, na.value = na.value,
165+
aesthetics = aesthetics, palette = palette, guide = guide,
166+
na.value = na.value, scale_name = deprecated(),
163167
fallback.palette = pal_seq_gradient("#132B43", "#56B1F7"),
164168
...
165169
)
@@ -175,7 +179,7 @@ scale_fill_binned <- function(..., palette = NULL, aesthetics = "fill", guide =
175179
#' * a single string naming a palette.
176180
#' * a palette function that when called with a single integer argument (the
177181
#' number of levels in the scale) returns the values that they should take.
178-
#' @inheritDotParams discrete_scale -scale_name -expand -position -minor_breaks
182+
#' @inheritDotParams discrete_scale -scale_name -expand -position -minor_breaks -fallback.palette
179183
#' @inheritParams discrete_scale
180184
#' @param type `r lifecycle::badge("superseded")` The preferred mechanism for
181185
#' setting the default palette is by using the theme. For example:
@@ -218,7 +222,8 @@ scale_colour_discrete <- function(..., palette = NULL, aesthetics = "colour", na
218222
}
219223
palette <- if (!is.null(palette)) as_discrete_pal(palette)
220224
discrete_scale(
221-
aesthetics, palette = palette, na.value = na.value,
225+
aesthetics = aesthetics, palette = palette, na.value = na.value,
226+
scale_name = deprecated(),
222227
fallback.palette = pal_hue(),
223228
...
224229
)
@@ -240,7 +245,8 @@ scale_fill_discrete <- function(..., palette = NULL, aesthetics = "fill", na.val
240245
}
241246
palette <- if (!is.null(palette)) as_discrete_pal(palette)
242247
discrete_scale(
243-
aesthetics, palette = palette, na.value = na.value,
248+
aesthetics = aesthetics, palette = palette, na.value = na.value,
249+
scale_name = deprecated(),
244250
fallback.palette = pal_hue(),
245251
...
246252
)

tests/testthat/test-scale-colour.R

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,22 @@ test_that("palette arguments can take alternative input", {
4949
expect_equal(alpha(test, 1), hex)
5050

5151
})
52+
53+
test_that("`name` is directed correctly (#6623)", {
54+
# The desired behaviour is that the first argument passed to scales is the
55+
# 'name' argument.
56+
57+
scales <- list(
58+
scale_colour_continuous,
59+
scale_colour_discrete,
60+
scale_colour_binned,
61+
scale_fill_continuous,
62+
scale_fill_discrete,
63+
scale_fill_binned
64+
)
65+
66+
for (scale in scales) {
67+
p <- scale("foobar")
68+
expect_equal(p$name, "foobar")
69+
}
70+
})

0 commit comments

Comments
 (0)