Search code examples
rggplot2facetfacet-wrap

Manually placing facets in facet plot ggplot


If, for example, you have 7 panels and you specify facet_wrap(~model, nrow = 3) ggplot will default to a 3x3x1 layout. Is it possible to get ggplot to do 3x2x2 (or 2x2x3, etc.)?


Solution

  • You can use ggh4x to specify the design of your facet:

    library(ggh4x)
    design = matrix(c(1, 1, 2, 2, 3, 3, 
                      4, 4, 4, 5, 5, 5, 
                      6, 6, 6, 7, 7, 7), 
                    3, 6, byrow = TRUE)
    ggplot(mpg, aes(displ, hwy, colour = as.factor(cyl))) + 
      geom_point() +
      facet_manual(vars(class), design = design)
    

    enter image description here