Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix discrete map bug #5624

Merged
merged 5 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@

* Fixed bug where discrete scales could not map aesthetics only consisting of
`NA`s (#5623)

# ggplot2 (development version)

This is a minor release that turned out quite beefy. It is focused on
Expand Down
3 changes: 3 additions & 0 deletions R/scale-.R
Original file line number Diff line number Diff line change
Expand Up @@ -926,6 +926,9 @@ ScaleDiscrete <- ggproto("ScaleDiscrete", Scale,

map = function(self, x, limits = self$get_limits()) {
n <- sum(!is.na(limits))
if (n < 1) {
return(rep(self$na.value, length(x)))
}
if (!is.null(self$n.breaks.cache) && self$n.breaks.cache == n) {
pal <- self$palette.cache
} else {
Expand Down
14 changes: 14 additions & 0 deletions tests/testthat/test-scales.R
Original file line number Diff line number Diff line change
Expand Up @@ -716,3 +716,17 @@ test_that("Using `scale_name` prompts deprecation message", {
expect_snapshot_warning(binned_scale("x", "foobar", pal_identity()))

})

# From #5623
test_that("Discrete scales with only NAs return `na.value`", {

x <- c(NA, NA)

sc <- scale_colour_discrete(na.value = "red")
sc$train(x)
expect_equal(sc$map(x), c("red", "red"))

sc <- scale_shape(na.value = NA_real_)
sc$train(x)
expect_equal(sc$map(x), c(NA_real_, NA_real_))
})
Loading