0

I would like to know how can I group plots like the following ggplot (edited in paint): Plot

It looks like facet_wrap() and facet_grid() functions cannot be used to arrange the columns and sub-columns.

Is there any R package that I can use?

Thanks

pogibas
  • 27,303
  • 19
  • 84
  • 117
candle786
  • 41
  • 7
  • Maybe you can take something from [here](https://stackoverflow.com/questions/40732543/seeking-workaround-for-gtable-add-grob-code-broken-by-ggplot-2-2-0/40986592#40986592) or [here](https://stackoverflow.com/questions/39801041/ggplot2-have-common-facet-bar-in-outer-facet-panel-in-3-way-plot/41094539#41094539). – Sandy Muspratt Mar 18 '18 at 18:37
  • Thanks@SandyMuspratt for the links! This means there is no direct way to do it using facet_wrap or facet_grid. – candle786 Mar 19 '18 at 03:25
  • That's correct. The closest you'll get is Jack Brookes' answer. – Sandy Muspratt Mar 19 '18 at 06:28

1 Answers1

0

You can multiply the factors used for columns and subcolumns.

library(ggplot2)

ggplot(mtcars, aes(disp, mpg)) + 
  geom_point() + 
  facet_wrap(~ cyl * gear, labeller = label_both)

enter image description here

The labeller label_both here also is useful as it adds the variable name before the value in the facet header.

Jack Brookes
  • 3,720
  • 2
  • 11
  • 22
  • Thanks @Jack Brookes for the suggestion! In the top row, is it possible to write cyl:4 only once? Right now it's written 3 times, across three columns. In my Plot, there are two columns of Medial and Final under Pair1. Can I do something similar using the facet_wrap () or facet_grid()? – candle786 Mar 18 '18 at 18:15
  • Unfortunately not, I imagine the way ggplot2 is coded means facets exist in this grid, and elements can't span between them. Of course, you can always do this manually, but it would require some work – Jack Brookes Mar 18 '18 at 18:23
  • I came across with GGally package but it requires manipulation of the data. – candle786 Mar 19 '18 at 03:21
  • You can always read their source code and see how they did it. There's this also https://stackoverflow.com/q/40316169/5024009 – Jack Brookes Mar 19 '18 at 08:33