-
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() label.position = "bottom" mess up the theme() legend.key.size option #5082
Comments
With regards to your points;
The size of key is the maximum of the text label and what you give as theme/legend setting.
I suspect this also due to the text.
Likely because your middle label is longer than the left/right labels. However, I suspect the true issue here lies elsewhere, namely that the boxplot flipping doesn't work 100% as it should. A more minimal reprex: library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 4.2.2
ggplot(mtcars, aes(mpg, factor(cyl), colour = factor(cyl))) +
geom_boxplot() +
scale_colour_discrete(
labels = paste0("A longer label than the key width ", 1:3)
) +
guides(
colour = guide_legend(label.position = "bottom")
) +
theme(
legend.position = "bottom"
) I suppose it is the viewport rotation of the boxplot key drawing function that is misbehaving here. To fix it, one could write a new key drawing function. library(grid)
library(rlang)
draw_key_boxplot2 <- function(data, params, size) {
gp <- gpar(
col = data$colour %||% "grey20",
fill = alpha(data$fill %||% "white", data$alpha),
lwd = (data$linewidth %||% 0.5) * .pt,
lty = data$linetype %||% 1,
lineend = params$lineend %||% "butt",
linejoin = params$linejoin %||% "mitre"
)
if (isTRUE(params$flipped_aes)) {
grobTree(
linesGrob(c(0.1, 0.25), 0.5),
linesGrob(c(0.75, 0.9), 0.5),
rectGrob(width = 0.5, height = 0.75),
linesGrob(0.5, c(0.125, 0.875)),
gp = gp
)
} else {
grobTree(
linesGrob(0.5, c(0.1, 0.25)),
linesGrob(0.5, c(0.75, 0.9)),
rectGrob(height = 0.5, width = 0.75),
linesGrob(c(0.125, 0.875), 0.5),
gp = gp
)
}
}
ggplot(mtcars, aes(mpg, factor(cyl), colour = factor(cyl))) +
geom_boxplot(key_glyph = draw_key_boxplot2) +
scale_colour_discrete(
labels = paste0("A longer label than the key width ", 1:3)
) +
guides(
colour = guide_legend(label.position = "bottom")
) +
theme(
legend.position = "bottom"
) Created on 2022-12-03 by the reprex package (v2.0.1) |
The same applies to the crossbar legend too: library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 4.2.2
ggplot(mtcars, aes(mpg, factor(cyl), colour = factor(cyl))) +
geom_crossbar(
stat = "summary"
) +
scale_colour_discrete(
labels = paste0("A longer label than the key width ", 1:3)
) +
guides(
colour = guide_legend(label.position = "bottom")
) +
theme(
legend.position = "bottom"
)
#> No summary function supplied, defaulting to `mean_se()` Created on 2022-12-03 by the reprex package (v2.0.1) |
@teunbrand Thank you very much for your explanations ! It makes already a lot more sense now. |
@teunbrand This is very elegantly written with rlang. I like it, I am not using rlang so often so I am not used to the operator you are using. Apparently ggplot2 has rlang as a dependency but it doesn't load the package when I library(ggplot2)... maybe because I am not using the entire Tidyverse. Should I close the issue then ?
|
I've opened up a pull request to fix the boxplot legend keys in #5083, where I've mentioned that it would fix this issue. Through GitHub voodoo-witchcraft, merging that PR would automatically close this issue. Until then, I think this issue serves a function as a reminder before the next release, so you're free to leave this issue open. |
I recently updated ggplot2 to version 3.4.0. I can't remember which version I was using before.
And since so the keys from a legend of one of my plot have disproportionated height. Also the
theme()
parameterslegend.key.height
andlegend.key.width
are not working as supposed (haven't really figured out how it is working in this specific configuration). Theguide_legend()
label.position = "bottom"
parameter is causing the problem. When I remove it, labels stay on the left of keys, and key dimensions parameter provided intheme()
work as intended.Here is the ggplot2 code I use causing the issue:
Here is an example of the legend I was getting before my update to 3.4.0 (which was fine for my use):
Here is an example of the legend I get after updating ggplot2 to 3.4.0:
I realize only since I am encountering this issue that actually the middle key (The yellow boxplot) is slightly bigger than the 2 others on both screen capture... probably another issue to have a look at.
So in total there are 3 issues related to
guide_legend()
label.position = "bottom"
:legend.key.height
andlegend.key.width
not working as expected.For now I will just remove
label.position = "bottom"
from my plot to get something working.So no emergency on my side, but I guess this issue is of interest to fix.
Thank you.
The text was updated successfully, but these errors were encountered: