Space between gpplot2 horizontal legend elements

前端 未结 6 1087
一生所求
一生所求 2020-12-17 18:17

I have a ggplot2 plot as follows:

library(ggplot2)

ggplot(mtcars, aes(factor(cyl), fill=factor(cyl))) + 
    geom_bar() +
    coord_flip() +
    theme(legen         


        
6条回答
  •  抹茶落季
    2020-12-17 18:49

    Not a hack here, this is the way to do it:

    Use theme(legend.text = element_text(margin = margin(r = 2, unit = 'cm')))

    ggplot(mtcars, aes(factor(cyl), fill = factor(cyl))) +
      geom_bar() +
      coord_flip() +
      theme(
        legend.position = 'top',
        legend.title = element_blank(),
        legend.text = element_text(margin = margin(r = 2, unit = 'cm'))
      )
    

    Will do it.

提交回复
热议问题