Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# ggplot2 (development version)

* Fixed bug where `na.value` was incorrectly mapped to non-`NA` values
(@teunbrand, #5756).
* Fixed bug in `guide_custom()` that would throw error with `theme_void()`
(@teunbrand, #5856).
* New helper function `ggpar()` to translate ggplot2's interpretation of
Expand Down
3 changes: 2 additions & 1 deletion R/scale-.R
Original file line number Diff line number Diff line change
Expand Up @@ -947,7 +947,8 @@ ScaleDiscrete <- ggproto("ScaleDiscrete", Scale,
transform = identity,

map = function(self, x, limits = self$get_limits()) {
n <- sum(!is.na(limits))
limits <- limits[!is.na(limits)]
n <- length(limits)
if (n < 1) {
return(rep(self$na.value, length(x)))
}
Expand Down
25 changes: 25 additions & 0 deletions tests/testthat/test-scales.R
Original file line number Diff line number Diff line change
Expand Up @@ -730,3 +730,28 @@ test_that("Discrete scales with only NAs return `na.value`", {
sc$train(x)
expect_equal(sc$map(x), c(NA_real_, NA_real_))
})

test_that("discrete scales work with NAs in arbitrary positions", {
# Prevents intermediate caching of palettes
map <- function(x, limits) {
sc <- scale_colour_manual(
values = c("red", "green", "blue"),
na.value = "gray"
)
sc$map(x, limits)
}

# All inputs should yield output regardless of where NA is
input <- c("A", "B", "C", NA)
output <- c("red", "green", "blue", "gray")

test <- map(input, limits = c("A", "B", "C", NA))
expect_equal(test, output)

test <- map(input, limits = c("A", NA, "B", "C"))
expect_equal(test, output)

test <- map(input, limits = c(NA, "A", "B", "C"))
expect_equal(test, output)

})