1

How can I format a faceted, multi-grouped box plot's x axes so that I get something that looks like this (dodgy paint, but shows the idea)... What I hope for

Here's the code so far.

# Make the dataset
data<-data.frame(cbind(runif(10,1,10), 
                       sample(1:5, 10, replace=TRUE), 
                       sample(1:5, 10, replace=TRUE),
                       sample(1:2, 10, replace=TRUE),
                       sample(1:2, 10, replace=TRUE)))

names(data)<-c("DV","Grouping_1", "Grouping_2", "Grouping_3", "Grouping_4")

data$Grouping_1<-as.factor(data$Grouping_1)
data$Grouping_2<-as.factor(data$Grouping_2)
data$Grouping_3<-as.factor(data$Grouping_3)
data$Grouping_4<-as.factor(data$Grouping_4)

# grab the interaction
data$groups<-interaction(data$Grouping_1,data$Grouping_2)

# Sort it (to make things neat)
data$groups<-factor(data$groups, levels = sort(levels(data$group)))

# Plot it
ggplot(data = data, aes(x =groups, y = DV, fill = Grouping_3)) + 
  geom_bar(stat = "identity", position = position_dodge())  + facet_grid(Grouping_4 ~ .)

Which gives...

This is what I end up with...

user3144759
  • 95
  • 3
  • 7
  • 1
    You can't. ggplot2 doesn't support multiple axis (intentionally). If you must have them, you'd probably need to edit the grob at the grid level (or create a second grob and arrange them). – Roland Jan 18 '15 at 10:23
  • Hmm, it's not using the faceting but maybe you can adapt the labeling of the xaxis that I'm using for the my bar chart here http://stackoverflow.com/questions/25690208/layered-axes-in-ggplot/25702640?noredirect=1#comment44443800_25702640 – Tom Martens Jan 20 '15 at 18:57

1 Answers1

0

This doesn't really work well in ggplot2. You can possibly try something like the code below. It's really not pretty, but it kind of works.

gr <- as.numeric(as.character(data$groups))
# additional data for annotation
df_a <- data.frame(y=-Inf, 
                   xmin = factor(sapply(1:5, function(x) min(gr[gr > x]))), 
                   xmax = factor(sapply(2:6, function(x) max(gr[gr < x]))), 
                   nr = -(sapply(1:5, function(x) sum(gr > x & gr < x+1))-1)*2.5+0.5, # change here to get horizontal adjustment right...
                   Grouping_4 = 2, 
                   text = 1:5)

# Plot it
p <- ggplot(data = data, aes(x =groups, y = DV, fill = Grouping_3)) + 
  geom_bar(stat = "identity", position = position_dodge())  + facet_grid(Grouping_4 ~ .) +
  geom_segment(data = df_a, aes(x=xmin, xend=xmax, y=y, yend=y, fill=NULL)) +
  geom_text(data = df_a, aes(x=xmin, y=y+2, label=text, fill=NULL, hjust=nr), vjust = 1.5) + 
  theme(plot.margin = unit(c(1,1,2,1), "lines")) + 
  scale_x_discrete(name = "\ngroups", labels=paste0("\n\n", round(10 * (sort(gr)-round(sort(gr), 0)), 0)))

require(gridExtra)
# turns clipping off
gt <- ggplot_gtable(ggplot_build(p))
gt$layout$clip[gt$layout$name == "panel"] <- "off"
grid.draw(gt)

Another option might be ggvis, a package similar to ggplot2 that supports multiple axes. Or you could manually add the grob with grid directly.

shadow
  • 21,823
  • 4
  • 63
  • 77