Q:

How to add extra title component to nested plots based on groups

I'm trying to plot some forestplots using values that fall into two variables and four separate groups. Unfortunately when it comes to titling I'm able to use only component of one variable. I've checked multiple solutions and other topics on stack/google but I can't find any sufficient answer. Here's my data:
                        X1 X2   X3   X4           X5          X6       X7        X8       X9
1                  Average  7    A Gamma 0.0029603231 0.001384471 1.002965 1.0002468 1.005690
2                  Average  7    A Theta 0.0065349553 0.001550613 1.006556 1.0035019 1.009620
3                  Average  7    B Gamma 0.0016303931 0.001553930 1.001632 0.9985857 1.004687
4                  Average  7    B Theta 0.0075093350 0.002020529 1.007538 1.0035554 1.011536
5  Variable_1               7    A Gamma 0.0039225073 0.001286516 1.003930 1.0014020 1.006465
6  Variable_1               7    A Theta 0.0069761147 0.001626288 1.007001 1.0037958 1.010215
7  Variable_1               7    B Gamma 0.0018554406 0.001802965 1.001857 0.9983231 1.005404
8  Variable_1               7    B Theta 0.0077743420 0.002102507 1.007805 1.0036602 1.011966
9  Variable_2               7    A Gamma 0.0009682313 0.002167431 1.000969 0.9967255 1.005230
10 Variable_2               7    A Theta 0.0021228624 0.005143065 1.002125 0.9920742 1.012278
11 Variable_2               7    B Gamma 0.0009803548 0.003064218 1.000981 0.9949872 1.007011
12 Variable_2               7    B Theta 0.0043084374 0.007307093 1.004318 0.9900368 1.018805

Here's my code:
library(dplyr)
library(purrr)
library(tidyr)
library(ggplot2)

plots <- my_data %>%
  group_by(X3, X4) %>%
  nest() %>%
  mutate(plot = map2(data, X3,  
                     ~ggplot(data = ., aes(x=X1, y=X7, ymin=X8, ymax=X9)) +
                           geom_linerange( size=8, colour="#a6d8f0") +
                           geom_hline(aes(yintercept=1), lty=1) +
                           geom_point(size=3, shape=21, fill="#008fd5", colour = "white", stroke = 1) +
                           scale_y_continuous(limits = c(0.95, 1.05)) +
                           coord_flip() +
                           ggtitle(.y) +
                           theme_minimal()))

Which gives me corresponding title only for A/B groups if I map X3. Gamma/Theta groups title if I map X4. I'd like to obtain plots with all title components combination A/Gamma, A/Theta, B/Gamma, B/Theta. Adding anything extra in my case gives me an error or static title component for all plots. Or i should just join X3 and X4 into one so i result with one variable and four groups?

A:

You can use purrr::pmap() to iterate over more than 2 elements:
plots <- dat %>%
  group_by(X3, X4) %>%
  nest() %>%
  mutate(plot = pmap(list(data, X3, X4), 
                     ~ggplot(data = ..1, aes(x=X1, y=X7, ymin=X8, ymax=X9)) +
                       geom_linerange( size=8, colour="#a6d8f0") +
                       geom_hline(aes(yintercept=1), lty=1) +
                       geom_point(size=3, shape=21, fill="#008fd5", colour = "white", stroke = 1) +
                       scale_y_continuous(limits = c(0.95, 1.05)) +
                       coord_flip() +
                       ggtitle(paste0(..2, "/", ..3)) +
                       theme_minimal()))

The ..index references the position of a element in the list passed to pmap()

