'Remove unused facet combinations in 2-way facet_grid

I have two factors and two continuous variables, and I use this to create a two-way facet plot using ggplot2. However, not all of my factor combinations have data, so I end up with dummy facets. Here's some dummy code to produce an equivalent output:

library(ggplot2)
dummy<-data.frame(x=rnorm(60),y=rnorm(60),
                  col=rep(c("A","B","C","B","C","C"),each=10),
                  row=rep(c("a","a","a","b","b","c"),each=10))
ggplot(data=dummy,aes(x=x,y=y))+
       geom_point()+
       facet_grid(row~col)

This produces this figure

Is there any way to remove the facets that don't plot any data? And, ideally, move the x and y axis labels up or right to the remaining plots? As shown in this GIMPed version

I've searched here and elsewhere and unless my search terms just aren't good enough, I can't find the same problem anywhere. Similar issues are often with unused factor levels, but here no factor level is unused, just factor level combinations. So facet_grid(drop=TRUE) or ggplot(data=droplevel(dummy)) doesn't help here. Combining the factors into a single factor and dropping unused levels of the new factor can only produce a 1-dimensional facet grid, which isn't what I want.

Note: my actual data has a third factor level which I represent by different point colours. Thus a single-plot solution allowing me to retain a legend would be ideal.



Solution 1:[1]

Maurits Evers solution worked great, but is quite cumbersome to modify. An alternative solution is to use facet_manual from {ggh4x}.

This is not equivalent though as it uses facet_wrap, but allows appropriate placement of the facets.

# devtools::install_github("teunbrand/ggh4x")
library(ggplot2)

dummy<-data.frame(x=rnorm(60),y=rnorm(60),
                  col=rep(c("A","B","C","B","C","C"),each=10),
                  row=rep(c("a","a","a","b","b","c"),each=10))

design <- "
  ABC
  #DE
  ##F
"

ggplot(data=dummy,aes(x=x,y=y))+
  geom_point()+
  ggh4x::facet_manual(vars(row,col), design = design, labeller = label_both)

Created on 2022-02-25 by the reprex package (v2.0.0)

Solution 2:[2]

One possible solution, of course, would be to create a plot for each factor combination separately and then combine them using grid.arrange() from gridExtra. This would probably lose my legend and would be an all around pain, would love to hear if anyone has any better suggestions.

Solution 3:[3]

This particular case looks like a job for ggpairs (link to a SO example). I haven't used it myself, but for paired plots this seems like the best tool for the job.

In a more general case, where you're not looking for pairs, you could try creating a column with a composite (pasted) factor and facet_grid or facet_wrap by that variable (example)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 JohannesNE
Solution 2 TJC
Solution 3 Punintended