【问题标题】:Overlay bar graphs in ggplot2 [duplicate]ggplot2中的叠加条形图[重复]
【发布时间】:2012-10-23 16:40:00
【问题描述】:

我正在尝试在ggplot2 中叠加条形图

我当前的代码会生成一个条形图,但它们是堆叠在一起的。我不想要这个,我希望将它们叠加起来,这样我就可以看到每个条形高度的差异。

代码:

library(ggplot2)
library(reshape)


x = c("Band 1", "Band 2", "Band 3")
y1 = c("1","2","3")
y2 = c("2","3","4")

to_plot <- data.frame(x=x,y1=y1,y2=y2)
melted<-melt(to_plot, id="x")

print(ggplot(melted,aes(x=x,y=value,fill=variable)) + geom_bar(stat="identity", alpha=.3))

堆叠输出:

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    尝试将position = "identity" 添加到您的geom_bar 通话中。您会从?geom_bar 注意到默认位置是stack,这是您在此处看到的行为。

    当我这样做时,我得到:

    print(ggplot(melted,aes(x=x,y=value,fill=variable)) + 
            geom_bar(stat="identity",position = "identity", alpha=.3))
    

    而且,如下所述,position = "dodge" 可能是一个更好的选择:

    【讨论】:

    • 也许位置应该是"dodge"?
    • @Seth 是的,这可能更可取(并使传说更容易理解),我只是按照 OP 似乎想要的方式进行。
    • 这么简单,不敢相信我错过了,谢谢
    猜你喜欢
    • 2016-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-17
    • 2014-12-08
    • 2018-09-30
    • 1970-01-01
    相关资源
    最近更新 更多