-
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
strange behavior with the margin of legend.text for R3.5 #2943
Comments
Here is a reprex. The problem is that the default library(ggplot2)
df <- reshape2::melt(outer(1:4, 1:4), varnames = c("X1", "X2"))
p1 <- ggplot(df, aes(X1, X2)) + geom_tile(aes(fill = value))
p1+scale_fill_continuous(guide = guide_legend()) p1+scale_fill_continuous(guide = guide_legend())+
theme(legend.title=element_blank()) p1+scale_fill_continuous(guide = guide_legend())+
theme(legend.title=element_blank(),
legend.spacing.x = unit(5.5, "pt")) Created on 2018-10-15 by the reprex package (v0.2.1) |
Thanks, this is exactly what I am looking for. |
My feeling is that it would be better to make it dependent on the legend text size rather than the legend title... that would probably slightly change a lot of visualisations... @clauswilke what is your feeling about the best fix? I personally don't think we should be too afraid of slight visual changes for the greater good |
I wouldn't object to minor visual changes, however I think your proposed solution doesn't get to the heart of the problem, and similar issues could continue to pop up (e.g., if somebody sets the legend text to The problem is that Note a broadly related issue we have forgotten but maybe should tackle at the same time: #2728 |
I think setting legend text to element_blank would be a non-issue as the spacing would then be invisible (the reason for proposing that approach) |
No, it's a little more complicated here, because we need to guess the title font size to calculate the default legend spacing: Lines 330 to 336 in f79712f
One simple option would be to replace the 0 at the end of line 330 with some positive value, e.g. the legend title font size in |
I may be getting tired, but I still don’t see the benefit of using the title size as opposed to the label size |
My point was that we're dealing with two separate issues. One is which theme element to draw the font size from. It's currently the legend title, but it could also be the legend text. I have no strong opinion about that. The other is what happens if that element is blank. We still need a default then either way. Currently the default is 0, and that was probably a bad choice when I made it. |
Ok, then my original point was that the default value is ok to be zero if we rely on the legend text, because any spacing will be invisible no matter what (as the text is not there to mark the other side of the spacing) |
Unfortunately the spacing variables are used in a couple of different contexts (this really needs to be disentangled at some point) and I don't think we can rely on this assumption. See also this comment: Lines 333 to 334 in f79712f
I think using a non-zero hardcoded default value is likely going to be the safest option, regardless of where we take the fontsize from otherwise. |
ok, I trust you on this (and most other things tbf :-) ) |
This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/ |
I have updated my R to version 3.5. There is too small margin for legend.text when using theme(legend.title=element_blank()), while it is normal without adding this line code.
The text was updated successfully, but these errors were encountered: