Skip to content

Commit 759c63c

Browse files
authored
Manual scale limits default to present names of values (#4619)
1 parent daed593 commit 759c63c

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

NEWS.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# ggplot2 (development version)
22

3+
* `scale_*_manual()` no longer displays extra legend keys, or changes their
4+
order, when a named `values` argument has more items than the data. To display
5+
all `values` on the legend instead, use
6+
`scale_*_manual(values = vals, limits = names(vals))`. (@teunbrand, @banfai,
7+
#4511, #4534)
8+
39
# ggplot2 3.3.5
410
This is a very small release focusing on fixing a couple of untenable issues
511
that surfaced with the 3.3.4 release

R/scale-manual.r

+3-2
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,9 @@ manual_scale <- function(aesthetic, values = NULL, breaks = waiver(), ..., limit
132132
force(values)
133133
}
134134

135-
if (is.null(limits)) {
136-
limits <- names(values)
135+
if (is.null(limits) && !is.null(names(values))) {
136+
# Limits as function to access `values` names later on (#4619)
137+
limits <- function(x) intersect(x, names(values))
137138
}
138139

139140
# order values according to breaks

tests/testthat/test-scale-manual.r

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ test_that("fewer values (#3451)", {
117117
test_that("limits and breaks (#4619)", {
118118
# values don't change legend order
119119
s1 <- scale_colour_manual(
120-
values = c("8" = "c", "4" = "a", "6" = "b"),
120+
values = c("8" = "c", "4" = "a", "6" = "b")
121121
)
122122
s1$train(c("8", "6", "4"))
123123
expect_equal(s1$map(c("8", "6", "4")), c("c", "b", "a"))

0 commit comments

Comments
 (0)