如何制作三个因素的交错条形图?

时间:2017-01-18 13:03:41

标签: r bar-chart

我想知道如何在R中制作一个每年会有三个不同因素的条形图?我想把这三个因素重叠“重叠”。但交错了一点,并添加百分比指标到酒吧。我在互联网上搜索了答案,但我找不到答案。

这就是我正在寻找的: enter image description here

我希望图表的图片有助于澄清我所追求的目标。

2 个答案:

答案 0 :(得分:0)

library(ggplot2)
df <- data.frame(year = c(2014, 2014, 2014, 2015, 2015, 2015), gr = c("x", "y", "z", "x", "y", "z"), val = c(100, 300, 400, 10, 20, 30))
ggplot(aes(x = factor(year), group = gr, y = val, fill = gr), data = df) + geom_bar(stat = "identity", position = "dodge") 

答案 1 :(得分:0)

我迟到了,但因为它很快......

library(ggplot2)
df <- data.frame(year = rep(2010:2014, 3), group = rep(LETTERS[24:26], 5), value = rep(c(34, 41, 59), 5))

ggplot(df, aes(x = factor(year), y = value, fill = group)) +
    geom_col(position = position_dodge(width = -0.5)) + 
    geom_text(aes(y = value - 2, label = value), position = position_dodge(w = -0.5))