ggplot2 icon indicating copy to clipboard operation
ggplot2 copied to clipboard

Make `ScaleDiscrete$map()` compatible with vctrs-palettes

Open teunbrand opened this issue 1 year ago • 0 comments

This PR aims to fix #6117.

Briefly, ScaleDiscrete$map() has been made vctrs-aware and now works with 2D structures too.

A demonstration:

devtools::load_all("~/packages/ggplot2")
#> ℹ Loading ggplot2

rcrd <- vctrs::new_rcrd(list(foo = LETTERS[1:3], bar = 1:3), class = "foo")

# Because `alpha()` casts to character, we can show the 'rcrd' palette works
as.character.foo <- function(x) {
  x <- vec_match(x, rcrd)
  c("dodgerblue", "orchid", "limegreen")[x]
}

# For demonstration purposes
format.foo <- function(x, ...) format(vec_proxy(x), ...)

p <- ggplot(mpg, aes(displ, hwy, colour = drv)) +
  geom_point() +
  scale_colour_manual(
    values = rcrd,
    na.value = NA
  )
p


head(layer_data(p)$colour)
#> <foo[6]>
#>      
#> 1 B 2
#> 2 B 2
#> 3 B 2
#> 4 B 2
#> 5 B 2
#> 6 B 2

Created on 2024-09-20 with reprex v2.1.1

teunbrand avatar Sep 20 '24 10:09 teunbrand