rggplot2

Is there a minimum vertical spacing for legend keys?


Is there a minimum vertical spacing between items in ggplot2 legend?

I am trying to reduce the space between legend keys. This code seems to suggest there is an absolute minimum (i.e. 0.001 pt).

library(ggplot2)

ggplot(mtcars, aes(x = hp, y = mpg, col = as.factor(cyl))) + 
  geom_point() +
  theme(legend.key.spacing.y = unit(0.001, "pt"))

ggplot(mtcars, aes(x = hp, y = mpg, col = as.factor(cyl))) + 
  geom_point() +
  theme(legend.key.spacing.y = unit(10, "pt"))

Solution

  • library(ggplot2)
    
    ggplot(mtcars, aes(x = hp, y = mpg, col = as.factor(cyl))) + 
      geom_point() +
      theme(legend.key.spacing.y = unit(-6, "pt")) 
    

    Created on 2025-01-08 with reprex v2.0.2