Skip to content

Could colourbar key height and width theme elements be provided?Β #6579

@davidhodge931

Description

@davidhodge931

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 + p2

Created on 2025-08-25 with reprex v2.1.1

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions