Search code examples
rggplot2facet-wrap

R ggplot2 facet_wrap: how to create different number of columns in different rows


I have 14 groups total, i want to present 4 columns in the first row, 5 in the 2nd and 3rd row. 1


Solution

  • As said in the comments you can use the package ggh4x where you can create your own layout. I would suggest having a look at the link mentioned in the comments, but I can give you an example where you have the layout you want. The important part is in the design where # means empty cell. You can make a layout to whatever you want:

    df <- data.frame(x = sample(1:14, 14),
                     y = sample(1:14, 14),
                     group = LETTERS[1:14])
    design <- "
      ABCD#
      EFGHI
      #JKL#
    "
    library(ggplot2)
    library(ggh4x)
    p <- ggplot(df, aes(x = x, y = y)) + geom_point()
    p + facet_manual(vars(group), design = design)
    #> Warning: Found more facetting levels than designed. The following levels are
    #> dropped: M, N
    

    Created on 2022-07-05 by the reprex package (v2.0.1)