Space between gpplot2 horizontal legend elements

前端 未结 6 1099
一生所求
一生所求 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 19:04

    It really seems something like theme(legend.text = element_text(margin = margin(r = 2, unit = 'in'))) would be the right way to accomplish the task, but that doesn't do anything at all.

    Instead, (and not for the first time) I fall back on the Microsoft Word style of alignment-hacking, i.e. just add spaces:

    ggplot(mtcars, aes(factor(cyl), fill=factor(paste(cyl, '                    ')))) + 
        geom_bar() +
        coord_flip() +
        theme(legend.position = 'top') +
        guides(fill = guide_legend(title=NULL))
    

    Because there's spaces on the 8 as well, it's a little off-center, but if you just paste them onto the previous labels you can nudge them around as you like.

    Apologies for any nightmares caused to graphic designers.

提交回复
热议问题