Skip to content

Nothing plotted or lines and dots are Grey with manual_scale when a named vector is used as the input. #5669

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

Closed
AlexanderCasper opened this issue Jan 30, 2024 · 3 comments

Comments

@AlexanderCasper
Copy link

Ref to: #4087

Try:

colors_named_vector <- c(
  red = "red",
  blue = "blue",
  green = "green"
)


library(ggplot2)

ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, color=Species)) +
  geom_point() %>% 
  scale_color_manual(values=colors_named_vector)

it seems as if it has not been fixed

@thomasp85
Copy link
Member

As stated in the documentation, if the input to values is a named vector the names will be used to match the values to the levels of the scale. Since iris$Species doesn't contain any values matching the names of your vector they will all be set to na.value

@teunbrand
Copy link
Collaborator

In addition to Thomas' comments, the next version will warn you that there are no names/values in common. Not quite sure why it warns twice though.

colors_named_vector <- c(
  red = "red",
  blue = "blue",
  green = "green"
)

library(ggplot2)

ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, color=Species)) +
  geom_point() +
  scale_color_manual(values=colors_named_vector)
#> Warning: No shared levels found between `names(values)` of the manual scale and the
#> data's colour values.
#> No shared levels found between `names(values)` of the manual scale and the
#> data's colour values.

Created on 2024-01-30 with reprex v2.1.0

@thomasp85
Copy link
Member

Also, you are using %>% instead of + when adding the scale. This is a working version of your code

colors_named_vector <- c(
    setosa = "red",
    versicolor = "blue",
    virginica = "green"
)


library(ggplot2)

ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, color=Species)) +
    geom_point() +
    scale_color_manual(values=colors_named_vector)

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

No branches or pull requests

3 participants