Skip to content
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# ggplot2 (development version)

* `scale_*_manual()` no longer displays extra legend keys, or changes their
order, when a named `values` argument has more items than the data. To display
all `values` on the legend instead, use
`scale_*_manual(values = vals, limits = names(vals))`. (@teunbrand, @banfai,
#4511, #4534)

# ggplot2 3.3.5
This is a very small release focusing on fixing a couple of untenable issues
that surfaced with the 3.3.4 release
Expand Down
5 changes: 3 additions & 2 deletions R/scale-manual.r
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,9 @@ manual_scale <- function(aesthetic, values = NULL, breaks = waiver(), ..., limit
force(values)
}

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

# order values according to breaks
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-scale-manual.r
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ test_that("fewer values (#3451)", {
test_that("limits and breaks (#4619)", {
# values don't change legend order
s1 <- scale_colour_manual(
values = c("8" = "c", "4" = "a", "6" = "b"),
values = c("8" = "c", "4" = "a", "6" = "b")
)
s1$train(c("8", "6", "4"))
expect_equal(s1$map(c("8", "6", "4")), c("c", "b", "a"))
Expand Down