Space between gpplot2 horizontal legend elements

ggplot2, r

Solution

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.

Problem

I have a ggplot2 plot as follows: ``` library(ggplot2) ggplot(mtcars, aes(factor(cyl), fill=factor(cyl))) + geom_bar() + coord_flip() + theme(legend.position = 'top') + guides(fill = guide_legend(title=NULL)) ``` I'd like add spacing between the fill elements as follows:

Original source

Related problems