11

I'm trying to create a chart using facet_wrap with a facet_grid inside each of the wrapped facets but I'm unable to. Any suggestions?

For example, if I'm doing year-on-year comparisons for monthly averages of 2 quantities, I would like to have -

  • 2 facets, one for each quantity,
  • Each of the5 quantity facets has 12 facets inside of it for each of the months
  • Each month facet has two facets inside of it for each of the year

The closest I can come is this,

library(ggplot2)

# create dataset
df <- data.frame(
  Facet1 = rep(c(1,2,3),24),
  Facet2 = c(rep(1,24),rep(2,24)),
  Year = rep(c(rep(2012,12),rep(2013,12)),2),
  Month = rep(rep(1:12,2),2),
  ValueX = sample(0:5,144,replace = TRUE),
  ValueY = sample(0:5,144,replace = TRUE)
)

df <- df[!(df$Facet1 == 2 & df$Facet2 == 2),]

ggplot(df, aes(ValueX, ValueY)) + geom_point() +
    facet_grid(Facet2 + Year ~ Month)

enter image description here

While, what I would ideally like, is something along the lines of this (In my mind, analogous to ggplot() ... + facet_grid(Year ~ Month) + facet_wrap(Facet2~.)) -

enter image description here

PS: I think the facets in the latter are much more distinguishable and neater to go through. Comments? Any alternatives?

TheComeOnMan
  • 12,535
  • 8
  • 39
  • 54
  • 1
    ggplot2 does not support this directly but someone with fancy grid skills could do this. You could also export the graphic and the the same thing in < minute with inkscape. – Tyler Rinker Nov 23 '13 at 05:14
  • So, `facet_grid` gives you the plot you want, but you are not happy with the distances between facets and with the facet strips on the right and want to change them? – Roland Nov 23 '13 at 11:14
  • @Roland - `facet_grid(Year ~ Month) + facet_wrap(Facet2~.)` is what I'm trying to achieve, so there are 2 facets for `Facet2`, and then each of those facets are then broken down into the 24 `Year¬Month` facets. The current methodology of `facet_grid(Facet2 + Year ~ Month)` accomplishes that as far as the eventual 48 facets are concerned, but lookwise, I think the second picture is a clearer faceting than the first one and allows for better readability. – TheComeOnMan Nov 23 '13 at 12:12
  • 1
    I can only reiterate what Tyler said: ggplot2 doesn't support this. You can only change distances and facet strips of the plot with grid functionality. – Roland Nov 23 '13 at 12:22
  • Thanks. Do you guys agree that this is cleaner though? Would it be useful to the general community if this were filed as a feature request? – TheComeOnMan Nov 26 '13 at 03:02

4 Answers4

3

Maybe I'm misunderstanding what you're trying to do, but does this not achieve what you want?

ggplot(df, aes(ValueX, ValueY)) + geom_point() +
  facet_grid(Facet2 ~ Facet1)

If you want to change the facet titles to match your example have a look at the labeller argument of facet_grid().

Ciarán Tobin
  • 7,306
  • 1
  • 29
  • 45
  • Thanks but nope. I'm trying to implement multiple levels of faceting, not edit the one level of faceting itself. – TheComeOnMan Nov 22 '13 at 17:23
  • If this doesn't achieve what you want, you need to improve your question. This exactly produces what you show in your mock-up. – Roland Nov 22 '13 at 20:12
  • Thanks a lot for your input guys, I have edited the question and shown something I hacked together something that looks like what I'm trying to get to. I'm sorry if the question wasn't clear earlier, hopefully it is clear now. – TheComeOnMan Nov 23 '13 at 03:55
2

This can easily be done using cowplot:

plot1<-ggplot(df[df$Facet2==1,], aes(ValueX, ValueY)) + geom_point() +
  facet_grid(Year ~ Month)+
  theme_bw()

plot2<-ggplot(df[df$Facet2==2,], aes(ValueX, ValueY)) + geom_point() +
  facet_grid(Year ~ Month)+
  theme_bw()

plot_grid(plot1, plot2, labels = c("1", "2"), nrow = 2)

enter image description here

Nakx
  • 1,460
  • 1
  • 23
  • 32
0

I am not sure to understand what you want to do , But I think it easier to get what you want using lattice` here:

library(latticeExtra)
xyplot(ValueY~ValueX|Facet1+Facet2,data=df, 
             between=list(x=2,y=0),
    par.settings = ggplot2like(),axis=axis.grid)

enter image description here

agstudy
  • 119,832
  • 17
  • 199
  • 261
  • Thanks a lot for your input @agstudy , I have edited the question and shown something I hacked together something that looks like what I'm trying to get to. I haven't used `lattice` before so I couldn't manipulate your suggestion too much, but I tried playing with the `Facet1+Facet2` part, that didn't get my anywhere. – TheComeOnMan Nov 23 '13 at 03:59
0

For this kind of task, the best solution - IMOHO - is to use the ggpubr package - see here for examples:

http://www.sthda.com/english/articles/24-ggpubr-publication-ready-plots/81-ggplot2-easy-way-to-mix-multiple-graphs-on-the-same-page/

You will not be able to use nested facets like you mention in your question (I don't think the support for this kind of facets exists at the moment), but could come up with something pretty close with some customization.

julio514
  • 181
  • 1
  • 1
  • 13
  • This would be a **much** better answer if you demonstrated how `ggpubr` can fix OP's problem, like Nakx does for `cowplot`, rather than just link to a very long piece of documentation. – Gregor Thomas Dec 13 '21 at 03:44