如何删除ggplot geom_bar图例的横杆?

时间:2013-01-25 22:34:42

标签: r legend ggplot2

我一直在网上搜索删除下面ggplot条形图的图例中的横杆。但是,没有成功。你能帮我解决这个问题。请参阅下面的数据“temp”和我正在使用的代码。 你能告诉我如何在酒吧使用模式吗?谢谢。

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

1 个答案:

答案 0 :(得分:7)

我知道这样做的唯一方法是做两个geom_bar图层,一个用蓝色,但没有图例,一个没有蓝色,但有一个图例:

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

推测一点,我怀疑这不容易的原因是作为一个设计决策,包作者希望图例完全匹配图层中的内容。大多数时候,你可能对这种行为感到非常高兴,但偶尔会有很多方便的尴尬。