1

I am working on a presentation in which it would be helpful to "build" up the elements of a facetted ggplot object. For example, with a two panel facetted plot, I would like to be able to show the first, left-side panel in a slide, still formatted as facets, and then advance to a second slide with both the left and the right-side panels, formatted the exact same way. Another way of thinking of this is I'd like to create a two facet plot and then "blank out" the right side plot but keep all other elements like the layout exactly the same. I can do this by hand in a graphics package but would prefer to do it in code.

# example of 1x2 facetted plot_model output
library(sjPlot)

mtcars %>% 
    mutate(vs_fct = as.factor(vs)) %>% 
    lm(mpg ~ wt * cyl * vs_fct, data = .) %>% 
    plot_model(type = "pred", terms = c("wt", "cyl", "vs_fct"))

Image on first slide with left panel (right panel deleted by hand in graphics software):

example two panel facetted plot with only left panel

Image on second slide with both panels:

example two panel facetted plot with both panels

Tung
  • 26,371
  • 7
  • 91
  • 115
Omar Wasow
  • 1,870
  • 24
  • 24
  • 3
    I would suggest you take a look at the {flipbookr} package https://github.com/EvaMaeRey/flipbookr – EmilHvitfeldt Mar 21 '21 at 22:53
  • Thank you @EmilHvitfeldt! I'm a big fan of Gina Reynolds / EvaMaeRey's work with flipbookr but I don't think it would solve this problem because showing one facet in a two-panel layout is not a standard format in ggplot (that I'm aware of). What I think will need to happen is that the two panel facet is created and then the right panel is manually deleted while keeping all the other elements. I tried to `View(plot_object)` but it was more than I could decipher. Separately thanks for all your work on paletteer! – Omar Wasow Mar 22 '21 at 00:41

1 Answers1

2

Maybe you can use the gtable package (Ref)?

# example of 1x2 facetted plot_model output
library(tidyverse)
library(sjPlot)

plt <- mtcars %>% 
  mutate(vs_fct = as.factor(vs)) %>% 
  lm(mpg ~ wt * cyl * vs_fct, data = .) %>% 
  plot_model(type = "pred", terms = c("wt", "cyl", "vs_fct"))
library(grid)
library(gtable)
library(lemon)

# create gtable object
gt = ggplot_gtable(ggplot_build(plt))
print(gt)
#> TableGrob (13 x 15) "layout": 22 grobs
#>     z         cells        name                                          grob
#> 1   0 ( 1-13, 1-15)  background               rect[plot.background..rect.349]
#> 2   1 ( 8- 8, 5- 5)   panel-1-1                      gTree[panel-1.gTree.226]
#> 3   1 ( 8- 8, 9- 9)   panel-2-1                      gTree[panel-2.gTree.241]
#> 4   3 ( 6- 6, 5- 5)  axis-t-1-1                                zeroGrob[NULL]
#> 5   3 ( 6- 6, 9- 9)  axis-t-2-1                                zeroGrob[NULL]
#> 6   3 ( 9- 9, 5- 5)  axis-b-1-1           absoluteGrob[GRID.absoluteGrob.245]
#> 7   3 ( 9- 9, 9- 9)  axis-b-2-1           absoluteGrob[GRID.absoluteGrob.249]
#> 8   3 ( 8- 8, 8- 8)  axis-l-1-2                                zeroGrob[NULL]
#> 9   3 ( 8- 8, 4- 4)  axis-l-1-1           absoluteGrob[GRID.absoluteGrob.253]
#> 10  3 ( 8- 8,10-10)  axis-r-1-2                                zeroGrob[NULL]
#> 11  3 ( 8- 8, 6- 6)  axis-r-1-1                                zeroGrob[NULL]
#> 12  2 ( 7- 7, 5- 5) strip-t-1-1                                 gtable[strip]
#> 13  2 ( 7- 7, 9- 9) strip-t-2-1                                 gtable[strip]
#> 14  4 ( 5- 5, 5- 9)      xlab-t                                zeroGrob[NULL]
#> 15  5 (10-10, 5- 9)      xlab-b titleGrob[axis.title.x.bottom..titleGrob.308]
#> 16  6 ( 8- 8, 3- 3)      ylab-l   titleGrob[axis.title.y.left..titleGrob.311]
#> 17  7 ( 8- 8,11-11)      ylab-r                                zeroGrob[NULL]
#> 18  8 ( 8- 8,13-13)   guide-box                             gtable[guide-box]
#> 19  9 ( 4- 4, 5- 9)    subtitle         zeroGrob[plot.subtitle..zeroGrob.345]
#> 20 10 ( 3- 3, 5- 9)       title          titleGrob[plot.title..titleGrob.344]
#> 21 11 (11-11, 5- 9)     caption          zeroGrob[plot.caption..zeroGrob.347]
#> 22 12 ( 2- 2, 2- 2)         tag              zeroGrob[plot.tag..zeroGrob.346]

Show plot layout

gtable_show_names(gt)

Remove everything related to panel-2-

rm_grobs <- gt$layout$name %in% c("panel-2-1", "strip-t-2-1", 
                                  "axis-t-2-1", "axis-b-2-1",
                                  "axis-l-1-2", "axis-r-1-2", "ylab-r")
# remove grobs
gt$grobs[rm_grobs] <- NULL
gt$layout <- gt$layout[!rm_grobs, ]

# check result
gtable_show_names(gt)

Check the modified plot

grid.newpage()
grid.draw(gt)

Created on 2021-03-21 by the reprex package (v1.0.0)

Tung
  • 26,371
  • 7
  • 91
  • 115
  • 1
    Like magic! This is an elegant solution. The steps are so clear, particularly the use of gtable_show_names, that I can see how to apply it to other modifications of other plots / grobs. Much obliged. – Omar Wasow Mar 22 '21 at 15:17
  • 1
    Glad that it was helpful. You might be interested in these answers using `gtable` too https://stackoverflow.com/questions/52208430/how-to-change-one-specific-facet-in-ggplot/52210907#52210907 & https://stackoverflow.com/questions/25640114/remove-some-of-the-axis-labels-in-ggplot-faceted-plots/52028670#52028670 – Tung Mar 22 '21 at 16:07
  • 1
    @Tung your response is amazing. I was looking for the same thing. I wanted to add, is it possible to convert the output to ggplot again? I want to save it using ggsave and although it's supposed to save the last plot, it saves all the panels and not the "edited" plot. – eggrandio Jan 26 '22 at 18:15
  • @eggrandio: Thank you! you may have to follow this method https://stackoverflow.com/a/49118535/786542. – Tung Jan 28 '22 at 18:42