4

I have been searching on WEB for removing the crossbars in the legend of the below ggplot barplot. But, no success. Could you please help me to fix this. Please see below for the data "temp" and the code I am using. Could you also let me know how can use patterns on the bars? Thank you.

temp:
    type    var value
    A   k1  20
    A   l1  30
    B   k1  10
    B   l1  15

    ggplot(temp,aes(type, value)) + geom_bar(stat="identity", aes(group=var, fill=type, facets=var),colour="blue1", position="identity") + facet_grid(.~var) + theme_bw()

enter image description here

samarasa
  • 2,025
  • 2
  • 16
  • 22
  • 1
    see the bottom of [this page](http://wiki.stdout.org/rcookbook/Graphs/Legends%20(ggplot2)/) . In genreal this is a useful resource. – user1317221_G Jan 27 '13 at 00:24
  • This seems to be the same question than the so called duplicate but it isn´t. It´s just the names that are similar. The ggplot legend slashes questions deals more with adding a rectangular content to the legend.key. This one is more general and at the moment seems to be the best answer. – marbel Aug 01 '13 at 13:24

1 Answers1

7

The only way I know of to do this is to do two geom_bar layers, one with the blue, but no legend, and one without the blue but with a legend:

ggplot(temp,aes(type, value)) + 
    geom_bar(stat="identity", aes(group=var, fill=type, facets=var),color = "blue1", position="identity",legend = "none") +
    geom_bar(stat="identity", aes(group=var, fill=type, facets=var), position="identity") +
    facet_grid(.~var) + 
    theme_bw()

enter image description here

Speculating a bit, I suspect the reason this isn't easier is that the package author, as a design decision, wants the legends to match what's in the layer exactly. Most of the time, you're probably pretty happy about this behavior, but with great convenience comes the occasional awkwardness.

joran
  • 169,992
  • 32
  • 429
  • 468
  • Thank you. Is there way, we can use patterns on the bars here as we do using barplot() method? – samarasa Jan 25 '13 at 22:58
  • 3
    @kkp No. See [here](http://stackoverflow.com/q/7604785/324364) and also Greg Snow's very interesting comment [here](https://groups.google.com/forum/?fromgroups=#!searchin/ggplot2/geom_bar$20pattern/ggplot2/62T0RYtWz38/LVJYXx5QgTQJ) for an explanation of the history of that type of shading, and why most people don't do it anymore. – joran Jan 25 '13 at 23:04
  • 1
    @kkp I should amend my "no" to say that pretty much anything would be possible, depending on how deeply you want to delve into doing things manually with **grid** functions, but it won't exactly be trivial. – joran Jan 25 '13 at 23:21