Omar Wasow
Omar Wasow

Reputation: 2020

Build (or delete) panels of ggplot facet one at a time

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

Upvotes: 1

Views: 465

Answers (1)

Tung
Tung

Reputation: 28381

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)

Upvotes: 2

Related Questions