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: Is it possible to use legend.justification.* adjust the legend position directly in guide_colorbar #6263

Open
Breeze-Hu opened this issue Dec 29, 2024 · 4 comments · May be fixed by #6268

Comments

@Breeze-Hu
Copy link

Happy New Year in advance!

The update in version 3.5 https://www.tidyverse.org/blog/2024/02/ggplot2-3-5-0-legends/#placement mentions the fine adjustment of the legend position, which can now be done in combination with guides and theme.

It was the expected result and worked out well:

library(ggplot2)

ggplot(mpg, aes(displ, hwy, shape = factor(cyl), colour = cty)) +
  geom_point()+
  guides(color = guide_colorbar(position = 'bottom'))+
  theme(legend.justification.bottom = 'left')

Image

Is it possible to do positional adjustments (top, bottom, left, right) in the guide in scale_*_* and guides in combination with the theme for the bottom-left position, etc.? Currently only adjusts to the bottom, but not to the right. legend.justification = 'left' or legend.justification.bottom = 'left' for both of the following does not work as expected:

ggplot(mpg, aes(displ, hwy, shape = factor(cyl), colour = cty)) +
  geom_point()+
  scale_color_continuous(
  guide =  guide_colorbar(position = 'bottom',
                        theme = theme(legend.justification.bottom = 'left')))
ggplot(mpg, aes(displ, hwy, shape = factor(cyl), colour = cty)) +
  geom_point()+
  guides(color = guide_colorbar(position = 'bottom',
                                theme =  theme(legend.justification.bottom = 'left')))
ggplot(mpg, aes(displ, hwy, shape = factor(cyl), colour = cty)) +
  geom_point()+
  scale_color_continuous(
    guide =  guide_colorbar(position = 'bottom',
                            theme = theme(legend.justification = 'left')))
ggplot(mpg, aes(displ, hwy, shape = factor(cyl), colour = cty)) +
  geom_point()+
  scale_color_continuous(
    guide =  guide_colorbar(position = 'bottom',
                            theme = theme(legend.justification = c(0,0))))

Image

@teunbrand
Copy link
Collaborator

The justification of legends are a property of the legend boxes, not individual guides. We cannot let guides determine, in a bottom-up fashion, what the properties of legend boxes are because it would result in contorted code. At best, we can document more clearly which theme elements are ignored in guide_*(theme).

@Breeze-Hu
Copy link
Author

I see. So isn't the difference roughly like the two boxes in the picture below?
Image

@teunbrand
Copy link
Collaborator

That is roughly correct, except that the legend box is fit to the guides. You can visualise it using the theme(legend.box.background). Come to think of it, it probably wouldn't hurt to specify this in the documentation about legend.justification.

@Breeze-Hu
Copy link
Author

At best, we can document more clearly which theme elements are ignored in guide_*(theme).

I'm in favor of this, users will have more clarity on what can be adjusted directly in guide_* and others need to go to the theme.

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.

2 participants