0

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")

stacked chart for 1-dimensional x-axis

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)

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

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.

enter image description here

Triamus
  • 2,415
  • 5
  • 27
  • 37
  • Can you find an example or draw a picture or mock-up of the desired output? – JasonAizkalns Mar 31 '15 at 13:04
  • 3
    You can use `x = interaction(case, day)`. See e.g. [**here**](http://stackoverflow.com/questions/20060949/ggplot2-multiple-sub-groups-of-a-bar-chart/20073020#20073020) – Henrik Mar 31 '15 at 13:25
  • That is a way but too cumbersome for my application. thanks! – Triamus Mar 31 '15 at 14:21
  • 1
    Facet strips are easy to move around. See [here](http://stackoverflow.com/questions/10058839/how-to-display-strip-labels-below-the-plot-when-faceting/29336396#29336396), or [here](http://stackoverflow.com/questions/18065319/flip-facet-label-and-x-axis-with-ggplot2?lq=1) or [here](http://stackoverflow.com/questions/28853786/how-do-i-plot-charts-with-nested-categories-axes/28868462#28868462) – Sandy Muspratt Mar 31 '15 at 20:10

0 Answers0