Closed
Description
Sorry for the very long reprex, but I tried to make it complete (and easy to debug)
- When right/left legend, the
legend.key.height
does not work - 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
Labels
No labels