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

Symbol not shown for manually added values in the legend #6318

Closed
julou opened this issue Feb 4, 2025 · 4 comments
Closed

Symbol not shown for manually added values in the legend #6318

julou opened this issue Feb 4, 2025 · 4 comments

Comments

@julou
Copy link

julou commented Feb 4, 2025

Hello,

This might be related to #3372.
I need to combine several plots in a grid and make one shared legend. For this, I used to set manually the legend values and limits in the colour scale. But it doesn't work anymore

Here is a reprex:

ggplot(mpg, aes(class, hwy, col=class)) + 
  geom_boxplot() +
  scale_color_manual(values=c("suv"="blue", "compact"="red", "pickup"="green", "foo"="orange", "bar"="pink"), 
                     limits=c("suv", "compact", "pickup", "foo", "bar") )

Image

The levels are added to the legend ("foo" and "bar") but the corresponding symbols are not shown.

I tried (man many other things) adding a blank geom but it didn't help (same output).

ggplot(mpg, aes(class, hwy, col=class)) + 
  geom_boxplot() +
  geom_blank(data=~mutate(., class=fct_recode(class, "foo"="compact", "bar"="pickup"))) +
  scale_color_manual(values=c("suv"="blue", "compact"="red", "pickup"="green", "foo"="orange", "bar"="pink"), 
                     limits=c("suv", "compact", "pickup", "foo", "bar") )

This used to be with previous version of ggplot2 (3.4.1 on top of my head), but does not anymore with 3.5.1.
Thank you in advance for your guidance.

@teunbrand
Copy link
Collaborator

teunbrand commented Feb 4, 2025

Thanks for the report! You're right that this is a recent change, but it is a deliberate one. The keys are not shown is because no layer is representing the foo/bar levels. The way to force these non existing levels to show up with little boxplot keys is to use geom_boxplot(show.legend = TRUE). This has been reported before in #5728 and we have no intention of reverting the change, so I'm going to close this issue.

@julou
Copy link
Author

julou commented Feb 4, 2025

Thanks a lot for the quick answer! I'm glad to ready that this case is covered. I realise that I should have filed it as a support request.

If someone reads the initial post and wonders, here is the working example:

ggplot(mpg, aes(class, hwy, col=class)) + 
  geom_boxplot(show.legend=TRUE) +
  scale_color_manual(values=c("suv"="blue", "compact"="red", "pickup"="green", "foo"="orange", "bar"="pink"), 
                     limits=c("suv", "compact", "pickup", "foo", "bar") )

@teunbrand
Copy link
Collaborator

I realise that I should have filed it as a support request

We'd prefer it if people go to venues specialised in providing support, like stack overflow or the Posit community. This here issue tracker is used to report bugs and file feature requests.

@julou
Copy link
Author

julou commented Feb 4, 2025

I understand that, but you probably also realise that this is not always obvious to a user whether a breaking change is a bug or a design choice. And my google fu was not strong enough to spot any of the link you mention in the other issue (https://github.com/tidyverse/ggplot2/issues/5728#issuecomment-1970036164)…
Apologies… and thanks again in any case!

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

2 participants