微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

一次构建或删除ggplot facet 的一个面板

如何解决一次构建或删除ggplot facet 的一个面板

我正在制作一个演示文稿,其中“构建”分面 ggplot 对象的元素会很有帮助。例如,对于双面板分面图,我希望能够显示幻灯片中的第一个左侧面板,仍然格式化为分面,然后前进到左侧和右侧的第二张幻灯片面板,以完全相同的方式格式化。另一种思考方式是我想创建一个两个方面的图,然后“消隐”右侧的图,但保持所有其他元素(如布局)完全相同。我可以在图形包中手动执行此操作,但更喜欢在代码中执行此操作。

# 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"))

带有左侧面板的第一张幻灯片上的图像(在图形软件中手动删除了右侧面板):

example two panel facetted plot with only left panel

带有两个面板的第二张幻灯片上的图像:

example two panel facetted plot with both panels

解决方法

也许您可以使用 gtable 包 (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]

显示绘图布局

gtable_show_names(gt)

删除与 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)

检查修改后的图

grid.newpage()
grid.draw(gt)

reprex package (v1.0.0) 于 2021 年 3 月 21 日创建

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。