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

Feature request: vctrs-friendly scale mapping #6117

Open
teunbrand opened this issue Sep 20, 2024 · 0 comments · May be fixed by #6118
Open

Feature request: vctrs-friendly scale mapping #6117

teunbrand opened this issue Sep 20, 2024 · 0 comments · May be fixed by #6118

Comments

@teunbrand
Copy link
Collaborator

Essentially, the code below should return a <foo> class rcrd vector, not a mangled list.

library(ggplot2)

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

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

options(max.print = 10)
head(layer_data(p)$colour)
#> Warning in ans[npos] <- rep(no, length.out = len)[npos]: number of items to
#> replace is not a multiple of replacement length
#> [[1]]
#>  [1] "B" "B" "B" "B" "B" "B" "B" "A" "A" "A"
#>  [ reached getOption("max.print") -- omitted 224 entries ]
#> 
#> [[2]]
#>  [1] 2 2 2 2 2 2 2 1 1 1
#>  [ reached getOption("max.print") -- omitted 224 entries ]
#> 
#> [[3]]
#>  [1] "B" "B" "B" "B" "B" "B" "B" "A" "A" "A"
#>  [ reached getOption("max.print") -- omitted 224 entries ]
#> 
#> [[4]]
#>  [1] 2 2 2 2 2 2 2 1 1 1
#>  [ reached getOption("max.print") -- omitted 224 entries ]
#> 
#> [[5]]
#>  [1] "B" "B" "B" "B" "B" "B" "B" "A" "A" "A"
#>  [ reached getOption("max.print") -- omitted 224 entries ]
#> 
#> [[6]]
#>  [1] 2 2 2 2 2 2 2 1 1 1
#>  [ reached getOption("max.print") -- omitted 224 entries ]

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

EDIT: It should work with a plain data.frame even.

library(ggplot2)

pal_dataframe <- function(values) {
  force(values)
  function(n) {
    n_values <- vctrs::vec_size(values)
    if (n > n_values) {
      cli::cli_warn("Don't do this")
    }
    vctrs::vec_slice(values, seq_len(n))
  }
}


df <- data.frame(foo = LETTERS[1:3], bar = 1:3)
rownames(df) <- c("r", "4", "f")

p <- ggplot(mpg, aes(displ, hwy, colour = drv)) +
  geom_point() +
  discrete_scale(
    "colour", palette = pal_dataframe(df)
  )

options(max.print = 10)
head(layer_data(p)$colour)
#> Error in `[.data.frame`(pal, match(as.character(x), names(pal))): undefined columns selected

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant