Skip to content

Commit 5ec9f96

Browse files
committed
Remove extraneous limit values with drop=TRUE
* issue introduced in tidyverse#4471 * potential solution for tidyverse#4534 and tidyverse#4511 * add extra tests
1 parent acaa98e commit 5ec9f96

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# ggplot2 (development version)
22

3+
* Fixes issue introduced in #4471 with extra values supplied to manual scale
4+
showing up in legend (@banfai, #4511, #4534)
5+
36
# ggplot2 3.3.5
47
This is a very small release focusing on fixing a couple of untenable issues
58
that surfaced with the 3.3.4 release

R/scale-.r

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,11 @@ Scale <- ggproto("Scale", NULL,
496496
} else if (is.function(self$limits)) {
497497
self$limits(self$range$range)
498498
} else {
499-
self$limits
499+
if (!is.null(self$drop) && self$drop && !is.null(self$range$range)) {
500+
intersect(self$range$range, self$limits)
501+
} else {
502+
self$limits
503+
}
500504
}
501505
},
502506

tests/testthat/test-scale-manual.r

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
context("scale_manual")
22

33
test_that("names of values used in manual scales", {
4-
s <- scale_colour_manual(values = c("8" = "c","4" = "a","6" = "b"))
5-
s$train(c("4", "6", "8"))
6-
expect_equal(s$map(c("4", "6", "8")), c("a", "b", "c"))
4+
s1 <- scale_colour_manual(values = c("8" = "c", "4" = "a", "6" = "b"))
5+
s1$train(c("4", "6", "8"))
6+
expect_equal(s1$map(c("4", "6", "8")), c("a", "b", "c"))
7+
8+
s2 <- scale_colour_manual(values = c("8" = "c", "4" = "a", "6" = "b"), na.value = NA)
9+
s2$train(c("4", "8"))
10+
expect_equal(s2$map(c("4", "6", "8")), c("a", NA, "c"))
711
})
812

913

@@ -87,13 +91,25 @@ test_that("unnamed values match breaks in manual scales", {
8791
})
8892

8993
test_that("limits works (#3262)", {
90-
# named charachter vector
94+
# named character vector
9195
s1 <- scale_colour_manual(values = c("8" = "c", "4" = "a", "6" = "b"), limits = c("4", "8"), na.value = NA)
9296
s1$train(c("4", "6", "8"))
9397
expect_equal(s1$map(c("4", "6", "8")), c("a", NA, "c"))
9498

95-
# named charachter vector
99+
# unnamed character vector
96100
s2 <- scale_colour_manual(values = c("c", "a", "b"), limits = c("4", "8"), na.value = NA)
97101
s2$train(c("4", "6", "8"))
98102
expect_equal(s2$map(c("4", "6", "8")), c("c", NA, "a"))
99103
})
104+
105+
test_that("fewer values (#3451)", {
106+
# named character vector
107+
s1 <- scale_colour_manual(values = c("4" = "a", "8" = "c"), na.value = NA)
108+
s1$train(c("4", "6", "8"))
109+
expect_equal(s1$map(c("4", "6", "8")), c("a", NA, "c"))
110+
111+
# unnamed character vector
112+
s2 <- scale_colour_manual(values = c("4", "8"), na.value = NA)
113+
s2$train(c("4", "6", "8"))
114+
expect_error(s2$map(c("4", "6", "8")), "Insufficient values")
115+
})

0 commit comments

Comments
 (0)