在ggplot2中x轴上具有多个尺寸的堆积条形图

时间:2015-03-31 13:00:42

标签: r ggplot2 axis stacked-chart

我想在x轴上创建一个带有两个维度的堆积条形图。到目前为止,我只能找到一个维度的例子(我在下面作为例子展示)。但我需要以下设置:主x轴= case,sub-x-axis = day,fill = category,y = var。所以我正在寻找两个相互叠加的条形图的比较,以便" M"和" F"。

示例数据

foo <- data.frame(case=c("A","A","A","A","A","A",
                         "B","B","B","B","B","B"),
                  category=c("A","A","B","B","C","C",
                             "A","A","B","B","C","C"),
                  day=c("M","F","M","F","M","F",
                        "M","F","M","F","M","F"),
                  var=c(rnorm(n=12,mean=10)))

一维x轴

的示例
ggplot(foo, aes(x=case, y=c(var),
                fill=category)) +
  geom_bar(stat="identity")

stacked chart for 1-dimensional x-axis

然而,我真正想做的是这样的事情(会给出错误)

ggplot(foo, aes(x=c(case, day), y=c(var),
                fill=category)) +
  geom_bar(stat="identity")

我将facet_wrap看作第二维,但似乎我不能把它放在x轴上?

ggplot(foo, aes(x=day, y=c(var),
                fill=category)) +
  geom_bar(stat="identity") +
  facet_wrap(~ case)

stacked chart for 1-dimensional x-axis and facet_wrap for second dimension

我希望很清楚我在寻找什么,如果不是,我很乐意编辑澄清。

编辑:示例应该大致如下。

enter image description here

0 个答案:

没有答案