Is it possible to switch the side of y-axis breaks and labels on a faceted plot? Is it possible to switch the side of y-axis breaks and labels on a faceted plot? r r

Is it possible to switch the side of y-axis breaks and labels on a faceted plot?


Koshke wrote this a while back, half as a joke on the ggplot2 mailing list: http://groups.google.com/group/ggplot2/browse_thread/thread/5c4658aceea9daf1

d <- data.frame(expand.grid(a=1:2,b=1:2,c=1:2),x=rnorm(8), y=rnorm(8)) p <- ggplot(d, aes(x, y)) + facet_grid(a~b) + geom_point() + coord_trans(x="reverse", y="reverse") + opts(strip.text.x=theme_text(angle=180),      strip.text.y=theme_text(angle=90),      axis.text.x=theme_text(angle=180),      axis.text.y=theme_text(angle=180),      axis.title.x=theme_text(angle=180),      axis.title.y=theme_text(angle=180)) print(p, vp=viewport(angle=180)) 

You would obviously have to mirror it or "flip vertically" for the desired effect, but I'm not sure how or if you can do that with modern image software.

There's also ... + coord_flip() which puts the y-axis at bottom and x-axis at left.


I wrote this for my needs. If you use it with switch = "y", you're halfway done.

You still need to modify it to switch the axis, which may take some more work because the axis and breaks form one column in a gtable, so you'll need to decompose it further.

switch_facet_strip <- function(p, switch = c("x", "y")) {  require(gtable)  rbind_gtable <- gtable:::rbind_gtable  cbind_gtable <- gtable:::cbind_gtable  if ("y" %in% switch)    p <- p + theme(strip.text.y = element_text(vjust = 0.5, angle = 90))  g <- ggplotGrob(p)  gdim <- as.numeric(g$layout[g$layout$name == "background", c("b", "r")])  tpos <- g$layout[g$layout$name == "strip-top", "b"][1]  rpos <- g$layout[g$layout$name == "strip-right", "r"][1]  new_tpos <- g$layout[g$layout$name == "axis-b", "b"][1] + 1  new_rpos <- g$layout[g$layout$name == "axis-l", "r"][1] - 1  if ("x" %in% switch) {    g <- rbind_gtable(           rbind_gtable(               gtable_add_rows(                 rbind_gtable(g[1:tpos-1, ] , g[(tpos+1):(new_tpos-1), ], "first"),                 unit(5, units = "mm")),               g[tpos, ], "first"),           g[new_tpos:gdim[1], ], "first")  }  if ("y" %in% switch) {    g <- cbind_gtable(           cbind_gtable(             gtable_add_cols(               cbind_gtable(g[, 1:new_rpos], g[, rpos], "first"),               unit(5, units = "mm")),             g[, (new_rpos+2):rpos-1], "first"),           g[, (rpos+1):gdim[2]], "first")  }  grid.newpage()  grid.draw(g)}

Note: This hack allows me to switch both strips next to their respective axis label. It only makes sense if you are using a theme in which strip have no backgrounds. I think this is the right way to display facet labels because it allows to read the plot like a multiway table. Moreover, the facet labels are next to the axes breaks, which makes it much more obvious what the axes represent. Anyone else thinks this makes sense? I wish it were an option in ggplot.

The right way to display facet grids


As ggplot2 is based on grid graphics, using the gridExtra package you can "swich" side for the facet strips in ggplot. The only painful about this approach is manually determining the coordinates. for the text.

# Some datad <- data.frame(expand.grid(a=1:2,b=1:2,c=1:2),x=rnorm(8), y=rnorm(8))# ggplotp <- ggplot(d, aes(x, y)) +  geom_point() +  facet_grid(a~b) +  xlab("") +  ylab("") +  theme(strip.background = element_blank(),        strip.text = element_blank())# Add x and y labelsp <- arrangeGrob(p, sub = textGrob(label = c("1", "2"), x = c(0.29, 0.73),  hjust = -0.1, vjust = -0.8, gp = gpar(fontsize = 10)), left = textGrob(label =  c("1", "2"), y = c(0.27, 0.70), hjust = 0, vjust = 1.8,  gp = gpar(fontsize = 10, lineheight = 0.5), rot = 90))# Viewp