Skip to content

Commit c2f2441

Browse files
authored
Fix discrete map bug (#5624)
* map all-NA values * add test * add news bullet * move bullet
1 parent 50b8cb1 commit c2f2441

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
12
# ggplot2 (development version)
23

4+
* Fixed bug where discrete scales could not map aesthetics only consisting of
5+
`NA`s (#5623)
6+
37
# ggplot2 3.5.0
48

59
This is a minor release that turned out quite beefy. It is focused on

R/scale-.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -938,6 +938,9 @@ ScaleDiscrete <- ggproto("ScaleDiscrete", Scale,
938938

939939
map = function(self, x, limits = self$get_limits()) {
940940
n <- sum(!is.na(limits))
941+
if (n < 1) {
942+
return(rep(self$na.value, length(x)))
943+
}
941944
if (!is.null(self$n.breaks.cache) && self$n.breaks.cache == n) {
942945
pal <- self$palette.cache
943946
} else {

tests/testthat/test-scales.R

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -716,3 +716,17 @@ test_that("Using `scale_name` prompts deprecation message", {
716716
expect_snapshot_warning(binned_scale("x", "foobar", pal_identity()))
717717

718718
})
719+
720+
# From #5623
721+
test_that("Discrete scales with only NAs return `na.value`", {
722+
723+
x <- c(NA, NA)
724+
725+
sc <- scale_colour_discrete(na.value = "red")
726+
sc$train(x)
727+
expect_equal(sc$map(x), c("red", "red"))
728+
729+
sc <- scale_shape(na.value = NA_real_)
730+
sc$train(x)
731+
expect_equal(sc$map(x), c(NA_real_, NA_real_))
732+
})

0 commit comments

Comments
 (0)