Skip to content

legend.key.size/legend.key.width/legend.key.height bug #6433

Closed
@kylebutts

Description

@kylebutts

Sorry for the very long reprex, but I tried to make it complete (and easy to debug)

  1. When right/left legend, the legend.key.height does not work
  2. When top/bottom legend, the legend.key.width does not work
library(ggplot2)
ggplot() +
  geom_point(aes(x = mpg, y = hp, color = hp), mtcars) +
  theme(
    legend.title.position = "top",
    legend.key.width = unit(12, "pt"),
    legend.key.height = unit(12, "pt")
  )

# Height does not work
ggplot() +
  geom_point(aes(x = mpg, y = hp, color = hp), mtcars) +
  theme(
    legend.title.position = "top",
    legend.key.width = unit(12, "pt"),
    legend.key.height = unit(48, "pt")
  )

# Width works
ggplot() +
  geom_point(aes(x = mpg, y = hp, color = hp), mtcars) +
  theme(
    legend.title.position = "top",
    legend.key.width = unit(48, "pt"),
    legend.key.height = unit(12, "pt")
  )

# size changes height
ggplot() +
  geom_point(aes(x = mpg, y = hp, color = hp), mtcars) +
  theme(
    legend.title.position = "top",
    legend.key.size = unit(48, "pt"),
    legend.key.width = unit(12, "pt"),
  )

# can override with width
# can not override with height
ggplot() +
  geom_point(aes(x = mpg, y = hp, color = hp), mtcars) +
  theme(
    legend.title.position = "top",
    legend.key.size = unit(48, "pt"),
    legend.key.width = unit(12, "pt"),
    legend.key.height = unit(12, "pt")
  )

# when using `legend.position = "bottom"`, width now causes the problems
ggplot() +
  geom_point(aes(x = mpg, y = hp, color = hp), mtcars) +
  theme(
    legend.position = "bottom",
    legend.title.position = "top",
    legend.key.width = unit(12, "pt"),
    legend.key.height = unit(12, "pt")
  )

# Height works
ggplot() +
  geom_point(aes(x = mpg, y = hp, color = hp), mtcars) +
  theme(
    legend.position = "bottom",
    legend.title.position = "top",
    legend.key.width = unit(12, "pt"),
    legend.key.height = unit(48, "pt")
  )

# Width does not work
ggplot() +
  geom_point(aes(x = mpg, y = hp, color = hp), mtcars) +
  theme(
    legend.position = "bottom",
    legend.title.position = "top",
    legend.key.width = unit(48, "pt"),
    legend.key.height = unit(12, "pt")
  )

# size changes width
ggplot() +
  geom_point(aes(x = mpg, y = hp, color = hp), mtcars) +
  theme(
    legend.position = "bottom",
    legend.title.position = "top",
    legend.key.size = unit(48, "pt"),
    legend.key.height = unit(12, "pt"),
  )

# can override with height
# can not override with width
ggplot() +
  geom_point(aes(x = mpg, y = hp, color = hp), mtcars) +
  theme(
    legend.position = "bottom",
    legend.title.position = "top",
    legend.key.size = unit(48, "pt"),
    legend.key.width = unit(12, "pt"),
    legend.key.height = unit(12, "pt")
  )

Created on 2025-04-16 with reprex v2.1.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions