21

This must have annoyed someone in the past so excuse me if this is a duplicate and I will remove it. Slashes across legends when using geom_bar can be annoying. e.g.:

x  <- c("a","b")
y  <- c(1,2)
df <- as.data.frame(cbind(x,y))
a  <- ggplot(df,aes(x=x,y=y,fill=x))
a + geom_bar(colour="black") + scale_fill_manual(values=c("white", "black"))

enter image description here

When I use coloured bars I use this work around, plotting bars without colours first e.g.

a + geom_bar() + geom_bar(colour="black",show_guide=FALSE) +
 scale_fill_manual(values=c("white", "black"))

However when the fill is white this leaves an unsatisfying empty white box in the legend without a border. e.g.

enter image description here

I have fixed this in the past manually using graphics software but now I think this must be of use to enough people to ask a question here. Can we make ggplot plot the legend with the black outline only but without the slash?

user1317221_G
  • 15,087
  • 3
  • 52
  • 78

2 Answers2

13

this,

 a + geom_bar() + geom_bar(colour="black",show_guide=FALSE) +
 scale_fill_manual(values=c("white", "black")) + 
 opts(legend.key = theme_rect(fill = 'black'))

gave me this, enter image description here thanks to this site.

Alos, you get the same result using colour instead of fill (it might be argued that one is better than).

a + geom_bar() + geom_bar(colour="black",show_guide=FALSE) + 
scale_fill_manual(values=c("white", "black")) + 
opts(legend.key = theme_rect(colour = 'black'))

Important note: In modern versions of ggplot2 opts has been deprecated and replaced with theme, and theme_rect has been replaced by element_rect.

joran
  • 169,992
  • 32
  • 429
  • 468
Eric Fail
  • 8,191
  • 8
  • 72
  • 128
3

No that's what gives it it's outline. If you use a gray instead of white (with your trick) it's more distinguishable. You could also add background color to the legend to make it more distinguishable and keep it white. See the bottom of this page for more detail:

http://wiki.stdout.org/rcookbook/Graphs/Legends%20(ggplot2)/

i wish there were a less hackish way to do this.

Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519
  • I believe the solution in the cookbook is more general. This question deals more with the issue of legend.key than removing the slashes. – marbel Aug 01 '13 at 13:26
  • 2
    @Martin you're responding to a post that is over a year old so a bit has changed since then including the cookbook. Also it has everything to do with removing the slashes. See the title of the question and the content. At the time all the fancy legend stuff didn't exist or was just introduced (ggplot2 0.9.0). – Tyler Rinker Aug 01 '13 at 17:26
  • 1
    I understand the post is old, the thing is I had to deal with this same issue and the cookbook gave me a more general answer. I mean, I followed your link and it helped me more than the accepted answer. – marbel Aug 02 '13 at 02:34
  • 1
    @Martin Gotcha. That is useful for future searchers. – Tyler Rinker Aug 02 '13 at 02:43