Skip to content
Open
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
7 changes: 5 additions & 2 deletions R/scale-.R
Original file line number Diff line number Diff line change
Expand Up @@ -1339,8 +1339,11 @@ ScaleDiscrete <- ggproto("ScaleDiscrete", Scale,
pal_match <-
vec_slice(pal, match(as.character(x), limits, nomatch = vec_size(pal)))

if (!is.na(na_value)) {
vec_slice(pal_match, vec_detect_missing(x)) <- na_value
if (!is.na(na_value) && vec_any_missing(x)) {
pal_match <- vec_assign(
pal_match, vec_detect_missing(x), na_value,
x_arg = "palette", value_arg = "na.value"
)
}
pal_match
},
Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/_snaps/scale-.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@

The `scale_name` argument of `binned_scale()` is deprecated as of ggplot2 3.5.0.

# mismatch between `na.value` and `palette` throws error

Code
sc$map(x)
Condition
Error in `vec_assign()`:
! Can't convert `na.value` <double> to match type of `palette` <character>.

# continuous scales warn about faulty `limits`

Code
Expand Down
10 changes: 10 additions & 0 deletions tests/testthat/test-scale-.R
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,16 @@ test_that("Using `scale_name` prompts deprecation message", {

})

test_that("mismatch between `na.value` and `palette` throws error", {
x <- c("A", "B", "C", NA)
sc <- scale_shape_manual(
values = c("circle", "triangle", "square"),
na.value = 4
)
sc$train(x)
expect_snapshot(sc$map(x), error = TRUE)
})

# Continuous scales -------------------------------------------------------

test_that("limits with NA are replaced with the min/max of the data for continuous scales", {
Expand Down