I would like to use a vertical legend in my plot. My legend labels are quite long and differ in the number of lines they take up. However, I would like the legend to show consistent heights for each key. So, in the plot below, I would like all legend keys to be the same height as the first (green) key. I can make all legend keys the same height if I increase the legend.key.height, but not if I decrease it.
I would rather a solution that works for multiple plots - I am outputting several hundred plots that each have different legends/text lengths.
library(ggplot2)
ggplot(iris, aes(Sepal.Width, Sepal.Length, fill = Species)) +
geom_col() +
scale_fill_manual(values = c("darkgreen", "blue", "orange"),
labels = c("label with one line",
"label with\ntwo lines",
"label\nwith\nthree lines")) +
theme_bw() +
theme(legend.position = "top",
legend.direction = "vertical",
legend.key.height = unit(0.1, "in"),
legend.text = element_text(size = 9,
margin = margin(t = 5, b = 5, unit = "pt")))
