Skip to content

scale_manual warning "no shared levels" could be improved #6429

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

Open
tdhock opened this issue Apr 15, 2025 · 1 comment
Open

scale_manual warning "no shared levels" could be improved #6429

tdhock opened this issue Apr 15, 2025 · 1 comment
Labels
messages requests for improvements to error, warning, or feedback messages

Comments

@tdhock
Copy link
Contributor

tdhock commented Apr 15, 2025

Related to #5669 which discusses this warning.

I tried adding aes(size) an a geom which already had size defined as a parameter, and I got the following warning:

library(ggplot2)
ggplot()+
  scale_size_manual(values=c(a=2,b=4))+
  geom_point(aes(
    x,x,size=x),
    size=3,
    data=data.frame(x=c('a','b')))
#> Warning: No shared levels found between `names(values)` of the manual scale and the
#> data's size values.

Created on 2025-04-15 with reprex v2.1.1

It took me some time before I realized that the solution was to remove size=3.

I would suggest improving the warning to say something like "size has been specified in aes and as a parameter in geom_point; please remove one or the other" which would be much more helpful than the current warning.

Thanks for maintaining ggplot2!

@teunbrand
Copy link
Collaborator

teunbrand commented Apr 15, 2025

Thanks for the suggestion! This is a warning thrown by the scale. Scales are ignorant about layers, so they don't know what mappings or fixed aesthetics layers have set. For this reason, the exact warning you point out cannot (within reason) be updated to include the specifics about the layer.

Alternatively, we could have layers warn when simultaneously using a fixed parameter and re-use the aesthetic in the mapping. However, I don't currently know how common it is for people's code to declare both but the code also works as intended.

A second option is to have the scale warning spell out the two levels, so it is easier to spot what the mismatch is.

@teunbrand teunbrand added the messages requests for improvements to error, warning, or feedback messages label Apr 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
messages requests for improvements to error, warning, or feedback messages
Projects
None yet
Development

No branches or pull requests

2 participants