If I place the legend below a plot made with ggplot2
via theme(legend.position = "bottom")
I can't fit all the legend labels on the plot if they have around 20 to 25 characters. For some reason I can't reduce the space between the legend items, even if I use theme(legend.spacing.x = unit(0.00001, "cm"))
(also below some value the space between legend items is not reduced any further).
Why does the white space between legend items increase so much with the length of the legend labels? See the three examples I provide, with very short, normal and long legend label lengths. I know you can make use of two or more rows for the legend items, however, with the long labels the white space is so large that you can only fit two per row.
data <-
tibble(x = as.numeric(rep(c(1991:2020), each = 5)),
y = as.numeric(rep(rep(20:11, each = 3), each = 5)),
longlab = factor(paste0("thisisaverylonglegendlabel", rep(c(1:5), 30))),
shortlab = factor(paste0("shortlab", rep(c(1:5), 30))),
supershortlab = factor(paste0("l", rep(c(1:5), 30))))
data %>%
ggplot(aes(x = x, y = y, fill = longlab)) +
geom_area() +
theme(legend.position = "bottom")
https://i.stack.imgur.com/Qhs4A.jpg
data %>%
ggplot(aes(x = x, y = y, fill = normallab)) +
geom_area() +
theme(legend.position = "bottom")
https://i.stack.imgur.com/OSEwE.jpg
data %>%
ggplot(aes(x = x, y = y, fill = supershortlab)) +
geom_area() +
theme(legend.position = "bottom")
https://i.stack.imgur.com/vmB5x.jpg
Is there a way to reduce the legend spacing to a minimum (similar to the spacing in the plot with the super short labels)?