By default, on a faceted ggplot (facet_grid), the y-axis facet labels are on the right and the y-axis breaks and labels are on the left.
Is it possible to switch them?
To remove the label from facet plot, we need to use “strip. text. x” argument inside the theme() layer with argument 'element_blank()'.
The facet_grid() function will produce a grid of plots for each combination of variables that you specify, even if some plots are empty. The facet_wrap() function will only produce plots for the combinations of variables that have values, which means it won't produce any empty plots.
geom_blank.Rd. The blank geom draws nothing, but can be a useful way of ensuring common scales between different plots. See expand_limits() for more details.
To hide or remove X-axis labels, use set(xlabel=None). To display the figure, use show() method.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With