0

I have made a cowplot using

gg <- cowplot::plot_grid(p1, p2)

I now want to add a ggplot2::geom_point() layer to p1 but I only have access to gg. How can I pull p1 and p2 back out, add a layer to p1 and then make a new cowplot, cowplot::plot_grid(p1_new, p2)?

I tried

cowplot::plot_grid(ggplot2::ggplot() + 
  gg$layers[[1]]$setup_layer + 
  ggplot2::geom_point(ggplot2::aes(x = x, y = y)), 
  ggplot2::ggplot() + gg$layers[[2]])

but that does not work. I don't really understand how ggplot2 (and cowplot) store the plots, grobs, layers, etc.

jtd
  • 179
  • 7
  • 1
    I'm not sure if that is possible, as it would be better to have `p1` and `p2` in a list, so that you can edit them before calling `plot_grid`. See comment by Claus at https://stackoverflow.com/questions/50472415/cowplot-extracting-subplot-after-calling-plot-grid – AndrewGB Apr 27 '22 at 19:49
  • 1
    @AndrewGB Thanks! I was trying to avoid the list solution. Also, I could not find that question you linked when I was searching--so this is a duplicate I think. :( – jtd Apr 27 '22 at 19:54

0 Answers0