Could colourbar key height and width theme elements be provided?
Opened this issue · 4 comments
Currently, you can't always set the legend key height/width as values that will work well as either legend or colourbars - particularly when the legend/colourbar is placed on top. It'd be great, if colourbar.key.height and colourbar.key.width elements could be provided to help here. These elements could inherit from legend.key.*
library(tidyverse)
library(palmerpenguins)
#>
#> Attaching package: 'palmerpenguins'
#> The following objects are masked from 'package:datasets':
#>
#> penguins, penguins_raw
library(patchwork)
set_theme(
theme_grey() +
theme(legend.key.height = rel(1)) +
theme(legend.key.width = rel(0.5)) +
theme(legend.position = "top") +
theme(legend.title.position = "top")
)
p1 <- penguins |>
tidyr::drop_na(sex) |>
ggplot() +
geom_bar(
aes(x = species,
fill = species),
)
p2 <- penguins |>
tidyr::drop_na(sex) |>
ggplot() +
geom_point(
aes(x = flipper_length_mm,
y = body_mass_g,
colour = bill_length_mm),
)
p1 + p2Created on 2025-08-25 with reprex v2.1.1
Thanks for the suggestion. I think this'd set the wrong precedent in that it invites tailored theme settings for every guide. That'll inflate the number of theme settings and the already considerable cognitive burden for users to identify the right ones.
That's a valid concern.
But these legend.key.* elements are the only ones that mean that is is impossible to design a complete theme with legend on top that works for numeric as well as categorical colour variable.
I think this increases the cognitive load for users, as they are forced to make changes to the plot theme themselves, depending on the plot they are making
Thanks for considering
Thanks for the discussion.
A workaround I’ve used is to keep legend.key.height/width in the theme for categorical guides, and then override continuous guides directly with guide_colourbar(barwidth, barheight).
scale_colour_continuous(
guide = guide_colourbar(barwidth = 6, barheight = 0.5)
)
maybe playing around with:
scale_colour_continuous(
guide = guide_colourbar(barwidth = 6, barheight = 0.5)
)
This keeps the theme mostly reusable, even if it’s not fully self-contained.
Yeah, that's similar to my way as well @AyeshaKhalid-ar It'd be cool to be able to avoid this repetitive step if possible.
You could name these with to avoid the suggestion that all guide theme properties will be provided @teunbrand
You could prefix the elements with legend to suggest that in terms of themes, colourbars/binned guides are still just legends - and therefore all guide specific elements are not going to be required.
E.g. legend.bar.height / legend.bar.width or legend.key.height.bar / legend.key.width.bar
