-
Notifications
You must be signed in to change notification settings - Fork 2k
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
guide_legend ignores legend.spacing.(x/y) depending on byrow #4352
Comments
100% agree. So far as I know, There's loads of questions about this on Stackoverflow and all the answers there about introducing spacing between legend keys using
As I understand it, by default It's uncommon, but occasionally I find need to use
It's probably not getting ignored. What's probably happening (and what I've seen in earlier versions of ggplot but haven't checked recently) is the spacing is applied between a horizontal legend and the plot. It's just harder to see because a horizontal legend has the title to the side and only one row of keys. In general, my experience is it's cryptic which legend settings adjust sizing where under which circumstances. If there were figures in the documentation diagramming out how settings apply across different legend configurations what the default spacings and units are that would probably help considerably. But, if these exist, I don't really know where they're included. You can figure some of it out from the examples in various parts of the documentation (and answers to Stackoverflow questions) but these are at the level of individual settings rather than a master diagram. |
Intuitively, I'd look for the
Similarly, I would expect this to be controlled by the
I agree with that this would be nice to have somewhere. |
I've thought about this some more, and have some more concrete suggestions (that can lead to a visual change). Consider the following plots: library(ggplot2)
plt <- ggplot(mpg, aes(displ, hwy, colour = as.factor(cyl))) +
geom_point() +
theme(
legend.spacing.x = unit(0.5, "cm"),
legend.spacing.y = unit(0.5, "cm")
)
p1 <- plt + guides(colour = guide_legend(byrow = TRUE, ncol = 2)) +
ggtitle("byrow = TRUE")
p2 <- plt + guides(colour = guide_legend(byrow = FALSE, ncol = 2)) +
ggtitle("byrow = FALSE")
patchwork::wrap_plots(list(p1, p2)) Created on 2022-05-19 by the reprex package (v2.0.1) Foremost, my issue is that we cannot control the (vertical) spacing between keys when In the diagram I've placed some suggestions on how the situation could be improved:
Lines 331 to 338 in d6f5bf4
If this seems like a good idea to the ggplot2 maintainers, I can try to draft a PR to review. |
I was faced with this problem of needing to adjust my vertical space when using This SO answer actually worked very well for me, so it is maybe a solution? guides(colour=guide_legend(ncol=2,byrow=FALSE,
keywidth=0.5,
keyheight=0.1,
default.unit="cm")) |
This was fixed by #5456. |
Hello there,
I apologise in advance if I submit issues too frequently; I do really appreciate all the good work you put into ggplot2.
By default, a legend appears to ignore the
legend.spacing.y
theme setting.However, when
byrow = TRUE
in the guide, thelegend.spacing.y
is applied as expected.Created on 2021-02-22 by the reprex package (v1.0.0)
For horizontal legend layouts,
legend.spacing.x
is ignored whenbyrow = TRUE
, which is opposite to the vertical case.I was surprised by this as I would have expected the
legend.spacing
to be applied regardless of the layout order of the keys, but I'm likely not aware of the considerations going in to this. I believe the code controlling the legend spacing and gaps sometimesinterleave()
s the label/key sizes with spacings, and sometimes doesn't, conditional on thebyrow
argument.In an earlier comment (#3587 (comment)), it was proposed to get rid of the legend spacing, but I think there is a use-case for the spacing for the keys (the text margins maybe less so). Would it be a good idea to always interleave the spacing regardless of the
byrow
argument?The text was updated successfully, but these errors were encountered: