ggplot2 increase space between legend keys

前端 未结 2 1144
说谎
说谎 2020-12-05 17:15

How can I increase the space between the keys of the legend of ggplot2 plot?

library(ggplot2)
ggplot(aes(mpg, wt, colour = factor(cyl)),
                


        
2条回答
  •  生来不讨喜
    2020-12-05 17:35

    An alternative (and probably easier) solution is using legend.key and legend.key.size in the theme part of your code:

    ggplot(data = mtcars, aes(mpg, wt, colour = factor(cyl))) +
      geom_point() +
      guides(color = guide_legend(nrow = 2)) +
      theme(legend.direction = 'horizontal', 
            legend.position = 'bottom',
            legend.key = element_rect(size = 5),
            legend.key.size = unit(1.5, 'lines'))
    

    this gives:


    In case you are calling theme_bw or theme_classic before manipulating the legend, you should set the color of the legend rectangle:

    legend.key = element_rect(size = 5, color = 'white') #or: color = NA
    

提交回复
热议问题