0

It's my understanding that one of the big drawbacks of facet_grid is that you cannot specify the axis limits of groups of panels. Thus, I created four graphs, I added the axis limits I wanted and manually added them together using grid.arrange in the gridExtra package. However, I'd like if these plots match with the other ones I've created using face_grid. I think the bar labels that are auto-generated in facet_grid are controlled in theme() under the strip. related functions but I have no idea how to go about "recreating" these strip labels for my individual plots. Anyone know?

Here's the graph I've created with grid.arrange() enter image description here

And here's the graph I've created with facet.grid()

enter image description here

I'd like the gray shaded labels from facet.grid() in my grid.arrange() generated plot.

Thanks!

Jade131621
  • 316
  • 1
  • 13
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input that can be used to test and verify possible solutions. – MrFlick Mar 23 '22 at 21:05
  • With a little bit of work (largely supported by https://stackoverflow.com/q/63550588/3358272), you _can_ apply different axis limits to each facet/group of `facet_grid`. – r2evans Mar 23 '22 at 21:29

2 Answers2

0

Well, a simple approach would be to facet each of your four subplots by adding facet_grid. Doing so will automatically add strip text boxes to each of the subplots. In my code below I make use of a custom function and set the specs of each facet_grid conditionally using an if-else.

Using some fake example data and making use of patchwork instead of grid.arrange:

df <- data.frame(
  row = c("K", "r"),
  col = rep(c("Female", "Male"), each = 2),
  x = rep(1:2, each = 4),
  y = rep(1:2, each = 4)
)

# Split by facet row and col
df_split <- split(df, ~row + col)
# Order
df_split <- df_split[c("K.Female", "K.Male", "r.Female", "r.Male")]

plot_fun <- function(x, y) {
  facet_layer <- if (grepl("Female$", y) && !grepl("^r", y)) 
    facet_grid(.~col) 
  else if (grepl("Male$", y) && !grepl("^r", y)) 
    facet_grid(row~col)
  else if (grepl("Male$", y) && grepl("^r", y)) 
    facet_grid(row~.)
  
  ggplot(x, aes(x, y)) +
    geom_point() +
    facet_layer
}

library(purrr)
library(ggplot2)
library(patchwork)

purrr::imap(df_split, plot_fun) %>% 
  wrap_plots() 

stefan
  • 90,330
  • 6
  • 25
  • 51
0

as you asked for a manual addition of strips (and I looked for the same, not able to facet my data), following workaround: just add manually text with grouping/factor name above a white rectangle

  annotate("rect", xmin = 0.8, xmax = 3.5, ymin = 29, ymax = 33,
           color="black", fill = "white")+ 
  annotate(label = "something", x= 2.15, y=31, geom="text") +
  annotate("rect", xmin = 3.9, xmax = 6.5, ymin = 29, ymax = 33,
           color="black", fill = "white")+ 
  annotate(label = "something different", x= 5.25, y=31, geom="text")

You have to fiddle a little with the positioning. Whenever possible I go also with the automated faceting mentioned before.

Clem Snide
  • 483
  • 6
  • 13