I want to create a stacked bar plot with two dimensions on the x axis. So far I could only find examples with one dimension (which I show below as example). But I need the following set-up: main x-axis = case, sub-x-axis = day, fill = category, y = var. So I am looking for a comparison of two stacked bar charts next to each other for "M" and "F".
Example data
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)))
Example with 1-dimension x-axis
ggplot(foo, aes(x=case, y=c(var),
fill=category)) +
geom_bar(stat="identity")
However, what I really would like to do is something like this (will give error)
ggplot(foo, aes(x=c(case, day), y=c(var),
fill=category)) +
geom_bar(stat="identity")
I looket at facet_wrap as second dimension but it seems I cannot put it on the x-axis?
ggplot(foo, aes(x=day, y=c(var),
fill=category)) +
geom_bar(stat="identity") +
facet_wrap(~ case)
I hope it is clear what I am looking for, if not I'm happy to edit to clarify.
EDIT: Example of how it should roughly look like.