0

I have a simple bar graph in ggplot, with two factor variables on the x axis:

library(ggplot2)
dat <- data.frame(group1= c(1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4),
                  group2= rep(1:4,4),
                  val = 1:16)

ggplot(dat, aes(x=group1,y=val,group=group2))+
  geom_bar(stat="identity", position="dodge")

What is the simplest way to add a second x axis label (for group2)? There is a more complex version of this question here, but I don't see how to apply this logic to this simple case.

Community
  • 1
  • 1
tvg
  • 388
  • 2
  • 13
  • 1
    Do you mean this: [Multi-row x-axis labels](http://stackoverflow.com/questions/20571306/multi-row-x-axis-labels-in-ggplot-line-chart) – Roman Apr 19 '16 at 11:56

1 Answers1

0

As suggested at the question posted by Jimbou, one solution is:

ggplot(dat, aes(y=val,x=group2))+
  geom_bar(stat="identity")+
  facet_grid(.~group1,scales="free")

I'd be curious to know whether there is another solution using annotate, as also suggested in that question, that works in the case in which the grouping variables are two factors.

Community
  • 1
  • 1
tvg
  • 388
  • 2
  • 13
  • There is `switch` parameter in `facet_grid` since `ggplot2` version >= 2.0, using `facet_grid(.~group1,scales="free", switch = "x")` would look like adding second x-axis – inscaven Apr 19 '16 at 13:04