-
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
Changes from 7 commits
2ad149c
d6daf75
0bd94f2
fa13c5b
248a8b1
8d46949
2fbe125
675ac04
25fcd85
26ee180
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -324,12 +324,17 @@ guide_gengrob.colorbar <- function(guide, theme) { | |
# and to obtain the title fontsize. | ||
title.theme <- guide$title.theme %||% calc_element("legend.title", theme) | ||
|
||
title.hjust <- guide$title.hjust %||% theme$legend.title.align %||% title.theme$hjust %||% 0 | ||
title.vjust <- guide$title.vjust %||% title.theme$vjust %||% 0.5 | ||
|
||
grob.title <- ggname("guide.title", | ||
element_grob( | ||
title.theme, | ||
label = guide$title, | ||
hjust = guide$title.hjust %||% theme$legend.title.align %||% 0, | ||
vjust = guide$title.vjust %||% 0.5 | ||
hjust = title.hjust, | ||
vjust = title.vjust, | ||
margin_x = TRUE, | ||
margin_y = TRUE | ||
) | ||
) | ||
|
||
|
@@ -344,17 +349,48 @@ guide_gengrob.colorbar <- function(guide, theme) { | |
hgap <- width_cm(theme$legend.spacing.x %||% (0.5 * unit(title_fontsize, "pt"))) | ||
vgap <- height_cm(theme$legend.spacing.y %||% (0.5 * unit(title_fontsize, "pt"))) | ||
|
||
# label | ||
# Labels | ||
|
||
# get the defaults for label justification. The defaults are complicated and depend | ||
# on the direction of the legend and on label placement | ||
just_defaults <- label_just_defaults.colorbar(guide$direction, label.position) | ||
# don't set expressions left-justified | ||
if (just_defaults$hjust == 0 && any(is.expression(guide$key$.label))) just_defaults$hjust <- 1 | ||
|
||
# get the label theme | ||
label.theme <- guide$label.theme %||% calc_element("legend.text", theme) | ||
|
||
# We break inheritance for hjust and vjust, because that's more intuitive here; it still allows manual | ||
# setting of hjust and vjust if desired. The alternative is to ignore hjust and vjust altogether, which | ||
# seems worse | ||
if (is.null(guide$label.theme$hjust) && is.null(theme$legend.text$hjust)) label.theme$hjust <- NULL | ||
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 %||% | ||
just_defaults$hjust | ||
vjust <- guide$label.vjust %||% label.theme$vjust %||% | ||
just_defaults$vjust | ||
|
||
grob.label <- { | ||
if (!guide$label) | ||
zeroGrob() | ||
else { | ||
hjust <- x <- guide$label.hjust %||% theme$legend.text.align %||% | ||
if (any(is.expression(guide$key$.label))) 1 else switch(guide$direction, horizontal = 0.5, vertical = 0) | ||
vjust <- y <- guide$label.vjust %||% 0.5 | ||
switch(guide$direction, horizontal = {x <- label_pos; y <- vjust}, "vertical" = {x <- hjust; y <- label_pos}) | ||
|
||
switch( | ||
|
||
guide$direction, | ||
"horizontal" = { | ||
x <- label_pos | ||
|
||
y <- rep(vjust, length(label_pos)) | ||
margin_x = FALSE | ||
margin_y = TRUE | ||
}, | ||
"vertical" = { | ||
x <- rep(hjust, length(label_pos)) | ||
y <- label_pos | ||
margin_x = TRUE | ||
margin_y = FALSE | ||
} | ||
) | ||
label <- guide$key$.label | ||
|
||
# If any of the labels are quoted language objects, convert them | ||
|
@@ -366,8 +402,16 @@ guide_gengrob.colorbar <- function(guide, theme) { | |
}) | ||
label <- do.call(c, label) | ||
} | ||
g <- element_grob(element = label.theme, label = label, | ||
x = x, y = y, hjust = hjust, vjust = vjust) | ||
g <- element_grob( | ||
element = label.theme, | ||
label = label, | ||
x = x, | ||
y = y, | ||
hjust = hjust, | ||
vjust = vjust, | ||
margin_x = margin_x, | ||
margin_y = margin_y | ||
) | ||
ggname("guide.label", g) | ||
} | ||
} | ||
|
@@ -484,10 +528,18 @@ guide_gengrob.colorbar <- function(guide, theme) { | |
gt <- gtable_add_grob(gt, grob.bar, name = "bar", clip = "off", | ||
t = 1 + min(vps$bar.row), r = 1 + max(vps$bar.col), | ||
b = 1 + max(vps$bar.row), l = 1 + min(vps$bar.col)) | ||
gt <- gtable_add_grob(gt, grob.label, name = "label", clip = "off", | ||
gt <- gtable_add_grob( | ||
gt, | ||
grob.label, | ||
name = "label", | ||
clip = "off", | ||
t = 1 + min(vps$label.row), r = 1 + max(vps$label.col), | ||
b = 1 + max(vps$label.row), l = 1 + min(vps$label.col)) | ||
gt <- gtable_add_grob(gt, grob.title, name = "title", clip = "off", | ||
gt <- gtable_add_grob( | ||
gt, | ||
justify_grobs(grob.title, hjust = title.hjust, vjust = title.vjust, debug = title.theme$debug), | ||
name = "title", | ||
clip = "off", | ||
t = 1 + min(vps$title.row), r = 1 + max(vps$title.col), | ||
b = 1 + max(vps$title.row), l = 1 + min(vps$title.col)) | ||
gt <- gtable_add_grob(gt, grob.ticks, name = "ticks", clip = "off", | ||
|
@@ -500,3 +552,25 @@ guide_gengrob.colorbar <- function(guide, theme) { | |
#' @export | ||
#' @rdname guide_colourbar | ||
guide_colorbar <- guide_colourbar | ||
|
||
#' Calculate the default hjust and vjust settings depending on legend | ||
#' direction and position. | ||
#' | ||
#' @noRd | ||
label_just_defaults.colorbar <- function(direction, position) { | ||
if (direction == "horizontal") { | ||
switch( | ||
position, | ||
"top" = list(hjust = 0.5, vjust = 0), | ||
list(hjust = 0.5, vjust = 1) | ||
) | ||
} | ||
else { | ||
switch( | ||
position, | ||
"left" = list(hjust = 1, vjust = 0.5), | ||
list(hjust = 0, vjust = 0.5) | ||
) | ||
} | ||
} | ||
|
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
andlabel.theme$hjust
? Shouldn'tlabel.theme
inherit fromlegend.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
andlegend.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 fromlegend.text
.Did you also have a question about me overriding inheritance of
hjust
andvjust
forlabel.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 ofhjust = 0
andvjust = 1
instead ofvjust = 0.5
).