-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Enable margins settings for guide titles #2556
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
Conversation
…sts don't fail just because the legends jump around.
I have updated the pull request to cover legend text margins as well. I realize some visual tests still fail. Those are related to the default hjust setting of legend text and I will fix the issue. |
Examples with both types of legends:
|
My main concern with enabling this is that if we do later fix the alignment of titles to align "correctly" by default, then all these smaller fixes will break. OTOH, it's not totally clear what "correct" means, because when I think about aligning the title only with the keys, not the text, that's clearly not correct for a vertical legend. |
The way I see it, |
…and legend.text theme elements.
Margins and alignment work now for labels, as far as I can tell. I'm quite happy with the results. First, manual specification of justification and margins:
Second, defaults (for labels) make sense in all arrangements:
Two issues remain. None are regressions:
|
The results of this look really good @clauswilke! This issue with legend title/text margins has plagued me for some time 😅 The desired behavior we're aiming for is so similar to facet strip labels that it seems like there should be a way to fold the placement calculations into existing functions rather than having the legend titles go through |
R/guide-legend.r
Outdated
@@ -677,7 +693,7 @@ guide_gengrob.legend <- function(guide, theme) { | |||
) | |||
gt <- gtable_add_grob( | |||
gt, | |||
grob.title, | |||
justify_grobs(grob.title, hjust = title.hjust, vjust = title.vjust), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
justify_grobs()
takes a debugging argument, should that get passed in here and elsewhere when justify_grobs()
is called, or is that not intended to be a user-facing debugging option?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think passing it in makes sense. I'll update my patch. I ran into a problem with switching debugging on for labels and I wasn't sure whether it was my fault or a preexisting problem. I'll see if I can resolve that issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was a preexisting problem but it's fixed now.
@karawoo I think the problem arises because you don't know ahead of time whether the legend title and text will be integrated in a horizontal or vertical setting. In cases where you do know it, Also, I'd like to point out that |
if (is.null(guide$label.theme$vjust) && is.null(theme$legend.text$vjust)) label.theme$vjust <- NULL | ||
|
||
# label.theme in param of guide_legend() > theme$legend.text.align > default | ||
hjust <- guide$label.hjust %||% theme$legend.text.align %||% label.theme$hjust %||% |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it necessary to have both of theme$legend.text.align
and label.theme$hjust
? Shouldn't label.theme
inherit from legend.text
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the current (released) ggplot2, there are two special theme elements for legend alignment, legend.text.align
and legend.title.align
. I think both are obsolete and have issues, for example that they don't apply in a vertical context. I only left them in for backwards compatibility, but I'm happy to take them out. label.theme
does inherit from legend.text
.
Did you also have a question about me overriding inheritance of hjust
and vjust
for label.theme
? I'm happy to explain more. It's needed to make guides behave intuitively under default settings, so that, e.g., a horizontal guide with labels underneath has the correct label alignments (hjust = 0.5
instead of hjust = 0
and vjust = 1
instead of vjust = 0.5
).
R/guide-colorbar.r
Outdated
vjust <- y <- guide$label.vjust %||% 0.5 | ||
switch(guide$direction, horizontal = {x <- label_pos; y <- vjust}, "vertical" = {x <- hjust; y <- label_pos}) | ||
|
||
switch( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since you're rewriting this, do you mind moving the assignment into the individual blocks of the if statement? I think that will make it easier to read.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just so I understand what you're requesting. Patterns such as:
switch(
guide$direction,
"horizontal" = {
...
},
"vertical" = {
}
)
are used all over the place in this code. Do you want me to replace those with the following?
if (guide$direction == "horizontal") {
...
}
else {
...
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes please!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had originally not understood the "move assignment" part of the request, but I think I got it now. The code is easier to read now.
R/guide-colorbar.r
Outdated
switch( | ||
guide$direction, | ||
"horizontal" = { | ||
x <- label_pos |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similarly, it would be great to move these assignments out.
Thanks! |
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/ |
This pull request, while not completely solving #2465, makes it much easier to work around it, by enabling
margin
,hjust
, andvjust
for the theme elementlegend.title
. This enables fine-grained justification control over the location of the legend title, which is needed for plots with more than one legend.Examples:
For comparison, with the default settings but legend on top, this plot looks as follows:

(Unfortunately, there is no good way at this time to adjust the default settings depending on whether the legend sits on top or at the side. This will have to wait for later.)Note that none of the visual tests except one had to be changed. Things look exactly as they always did if the default settings are not changed. The visual test that had to be changed had the problem that the two legends in that plot switched order. I have now fixed the order, so this won't happen again.