I am building a ggplot visualization in which some fill aesthetics have very long variable names, while other variable names are short. Adding long names changes the size of the legend key corresponding to the long text - lengthening it to match the text. I am wondering if there is a way to standardize the legend key height across all varibles, and change the spaces between the legend items.
I tried modifying theme(legend.key.height()) and theme(legend.key.width()) but that didn't solve the problem.
Here is example code:
#load neccesary package
library('ggplot2')
#create the dataframe
df <- data.frame(year = as.integer(c(1, 1, 1, 1, 1, 2, 2, 2, 2, 2)),
class = c('A', 'B', 'C', 'D', 'E'),
value = c(50, 50))
#Create plot
g <- ggplot(df, aes(x = year, y = value, fill = class)) +
geom_col(position = 'stack') +
scale_fill_discrete(labels = c('This is an\nextremely\nlong label\nname', 'short label1', 'Another\nlong\nlabel\nname', 'short label3', 'short label4'))
Plots:
What I want is to have the same key size for all variables, with the white space between keys changing to accommodate the long text. So something that looks like this:
Trying g + theme(legend.key.height = unit(3, 'mm'), legend.key.width = unit(3, 'mm'))
Does not resolve the problem.
Any thoughts?



