Change order margin is presented in facet_grid ggplot2?

254 Views Asked by At

I've seen several answers on how to change the order of facets by reordering the factor levels. But I would like to change the order in which the margin is presented. So basically have (all) displayed on the left panel before 4, 6, 8. Thanks in advance!

library(ggplot2)
qplot(mpg, wt, data=mtcars) + facet_grid(. ~ cyl, margins=TRUE)

enter image description here

2

There are 2 best solutions below

0
Allan Cameron On BEST ANSWER

How about

mtcars2 <- rbind(mtcars, within(mtcars, cyl <- "(All)"))

qplot(mpg, wt, data = mtcars2) + facet_grid(. ~ cyl)

enter image description here

1
polkas On

I have a very sophisticated solution, you always could edit your grobs structure:

library(ggplot2)

gg = qplot(mpg, wt, data=mtcars) + facet_grid(. ~ cyl, margins=TRUE)

ggg = ggplotGrob(gg)

ggg$grobs = ggg$grobs[c(1,5,2:4,9,6:8,10:15,19,16:18,20:27)]

grid::grid.newpage()

grid::grid.draw(ggg)

enter image description here