我想在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")
然而,我真正想做的是这样的事情(会给出错误)
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)
我希望很清楚我在寻找什么,如果不是,我很乐意编辑澄清。
编辑:示例应该大致如下。