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

scale_colour_manual(palette=foo): cannot coerce type 'closure' to vector of type 'character' #3182

Closed
sjackman opened this issue Mar 10, 2019 · 5 comments

Comments

@sjackman
Copy link

Using the palette argument of scale_colour_manual yields the error:
Error in as.vector(x, "character"): cannot coerce type 'closure' to vector of type 'character'

My apologies in advance if it's my error.

@th4ckl on Twitter says…

I don't think this works at the moment. palette is passed to manual_scale, but it looks like manual_scale always defines a new pal that is then passed on to discrete_scale. Looks like a bug to me.

https://twitter.com/th4ckl/status/1104207569034199040

Reprex

library(ggplot2)
library(Polychrome)

# Does not work.
alphabet_colors <- function(n) as.vector(Polychrome::alphabet.colors(n))
ggplot(diamonds, aes(x = carat, y = price, colour = clarity)) +
    geom_point() +
    scale_colour_manual(palette = alphabet_colors)
#> Error in as.vector(x, "character"): cannot coerce type 'closure' to vector of type 'character'

# Works!
ggplot(diamonds, aes(x = carat, y = price, colour = clarity)) +
    geom_point() +
    scale_colour_manual(values = as.vector(Polychrome::alphabet.colors(8)))

Created on 2019-03-09 by the reprex package (v0.2.1)

@clauswilke
Copy link
Member

I don't think scale_colour_manual() was ever meant to be used this way but I see that the documentation creates this impression. It would be fairly easy to fix this for the manual scales, but now I'm worrying that the problem exists for many other scales also. The fundamental problem is overly eager forwarding of arguments to discrete_scale().

library(ggplot2)
library(Polychrome)

# Does not work.
alphabet_colors <- function(n) as.vector(Polychrome::alphabet.colors(n))
ggplot(diamonds, aes(x = carat, y = price, colour = clarity)) +
  geom_point() +
  scale_colour_viridis_d(palette = alphabet_colors)
#> Error in as.vector(x, "character"): cannot coerce type 'closure' to vector of type 'character'

Created on 2019-03-10 by the reprex package (v0.2.1)

@sjackman
Copy link
Author

sjackman commented Mar 10, 2019

Settings values rather than palette works well enough for me. How is the palette argument meant to be used; is there an example in the documentation?

@clauswilke
Copy link
Member

It's not meant to be used at all. The bug is that palette is accessible and mentioned in the documentation.

@sjackman
Copy link
Author

Great. Thanks for the clarification. Feel free to close this issue if you like.

@lock
Copy link

lock bot commented Oct 8, 2019

This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/

@lock lock bot locked and limited conversation to collaborators Oct 8, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants