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

Make ScaleDiscrete$map() compatible with vctrs-palettes #6118

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
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
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# ggplot2 (development version)

* More stability for vctrs-based palettes (@teunbrand, #6117).
* Built-in `theme_*()` functions now have `ink` and `paper` arguments to control
foreground and background colours respectively (@teunbrand)
* The `summary()` method for ggplots is now more terse about facets
Expand Down
2 changes: 1 addition & 1 deletion R/guide-.R
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ Guide <- ggproto(
labels <- as.list(labels)
}

key <- data_frame(mapped, .name_repair = ~ aesthetic)
key <- data_frame(!!aesthetic := mapped)
key$.value <- breaks
key$.label <- labels

Expand Down
29 changes: 19 additions & 10 deletions R/scale-.R
Original file line number Diff line number Diff line change
Expand Up @@ -954,10 +954,10 @@ ScaleDiscrete <- ggproto("ScaleDiscrete", Scale,
transform = identity,

map = function(self, x, limits = self$get_limits()) {
limits <- limits[!is.na(limits)]
n <- length(limits)
limits <- vec_slice(limits, !is.na(limits))
n <- vec_size(limits)
if (n < 1) {
return(rep(self$na.value, length(x)))
return(vec_rep(self$na.value, vec_size(x)))
}
if (!is.null(self$n.breaks.cache) && self$n.breaks.cache == n) {
pal <- self$palette.cache
Expand All @@ -973,21 +973,30 @@ ScaleDiscrete <- ggproto("ScaleDiscrete", Scale,
self$n.breaks.cache <- n
}

na_value <- if (self$na.translate) self$na.value else NA
pal_names <- names(pal)
na_value <- NA
if (self$na.translate) {
na_value <- self$na.value
if (obj_is_list(pal) && !obj_is_list(na_value)) {
# We prevent a casting error that occurs when mapping grid patterns
na_value <- list(na_value)
}
Comment on lines +979 to +982
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't love this, but I also don't know how else to solve this

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it is too bad

}

pal_names <- vec_names(pal)

if (!is_null(pal_names)) {
# if pal is named, limit the pal by the names first,
# then limit the values by the pal
pal[is.na(match(pal_names, limits))] <- na_value
pal <- unname(pal)
vec_slice(pal, is.na(match(pal_names, limits))) <- na_value
pal <- vec_set_names(pal, NULL)
limits <- pal_names
}
pal <- c(pal, na_value)
pal_match <- pal[match(as.character(x), limits, nomatch = length(pal))]
pal <- vec_c(pal, na_value)
pal_match <-
vec_slice(pal, match(as.character(x), limits, nomatch = vec_size(pal)))

if (!is.na(na_value)) {
pal_match[is.na(x)] <- na_value
vec_slice(pal_match, is.na(x)) <- na_value
}
pal_match
},
Expand Down
29 changes: 29 additions & 0 deletions tests/testthat/test-scales.R
Original file line number Diff line number Diff line change
Expand Up @@ -755,3 +755,32 @@ test_that("discrete scales work with NAs in arbitrary positions", {
expect_equal(test, output)

})

test_that("discrete scales can map to 2D structures", {

p <- ggplot(mtcars, aes(disp, mpg, colour = factor(cyl))) +
geom_point()

# Test it can map to a vctrs rcrd class
rcrd <- new_rcrd(list(a = LETTERS[1:3], b = 3:1))

ld <- layer_data(p + scale_colour_manual(values = rcrd, na.value = NA))
expect_s3_class(ld$colour, "vctrs_rcrd")
expect_length(ld$colour, nrow(mtcars))

# Test it can map to data.frames
df <- data_frame0(a = LETTERS[1:3], b = 3:1)
my_pal <- function(n) vec_slice(df, seq_len(n))

ld <- layer_data(p + discrete_scale("colour", palette = my_pal))
expect_s3_class(ld$colour, "data.frame")
expect_equal(dim(ld$colour), c(nrow(mtcars), ncol(df)))

# Test it can map to matrices
mtx <- cbind(a = LETTERS[1:3], b = LETTERS[4:6])
my_pal <- function(n) vec_slice(mtx, seq_len(n))

ld <- layer_data(p + discrete_scale("colour", palette = my_pal))
expect_true(is.matrix(ld$colour))
expect_equal(dim(ld$colour), c(nrow(mtcars), ncol(df)))
})
Loading