首页 > 解决方案 > 从“cowplot”中的组合图中提取单个图的详细信息以进行单元测试

问题描述

我正在尝试为用于cowplot::plot_grid()组合ggplot2绘图的函数编写单元测试。例如,

# setup
set.seed(123)
library(ggplot2)

# creating basic plots
p1 <- ggplot(aes(x = as.factor(am), y = wt), data = mtcars) + 
  geom_point() + 
  labs(title = "Dataset: mtcars", subtitle = "Source: `base` package")
p2 <- ggplot(aes(x = vore, y = brainwt), data = msleep) + 
  geom_point() + 
  labs(title = "Dataset: msleep", subtitle = "Source: `ggplot2` package")

# combined plot
(p <- cowplot::plot_grid(p1, p2, labels = c("(i)", "(ii)")))
#> Warning: Removed 27 rows containing missing values (geom_point).

reprex 包(v0.2.1)于 2019 年 1 月 5 日创建

对于ggplot2情节,我可以构建情节并提取这些细节。

# extracting title and subtitle (example with p1)
pb1 <- ggplot2::ggplot_build(p1)
pb1$plot$labels$title
#> [1] "Dataset: mtcars"
pb1$plot$labels$subtitle
#> [1] "Source: `base` package"

但是我怎样才能从组合图中提取有关各个图的详细信息-

建筑组合地块

pb <- ggplot2::ggplot_build(p)

那么,例如,如何p1从 from中提取字幕pb

标签: rggplot2testthatcowplot

解决方案


我们可以这样做:

p <- cowplot::plot_grid(p1, p2, labels = c("(i)", "(ii)"))

fun <- function(p, what) {
  unlist(sapply(p$layers, function(x) {
    idx <- which(x$geom_params$grob$layout$name == what)
    x$geom_params$grob$grobs[[idx]]$children[[1]]$label
  }))
}
fun(p, "title")
# [1] "Dataset: mtcars" "Dataset: msleep"
fun(p, "subtitle")
# [1] "Source: `base` package"    "Source: `ggplot2` package"

使用这种方法,额外使用不会有任何好处,ggplot_build因为我们仍然需要转到layers元素。

我相信没有更短的方法可以做到这一点。layers我们需要按原样深入研究pb$plot$labels$title整个情节是有道理的,而我们p是两者的结合。


如果你想提取一些其他元素,它应该是非常可行的,例如,在第一层的情况下,

p$layers[[1]]$geom_params$grob
# TableGrob (12 x 9) "layout": 18 grobs
#     z         cells       name                                          grob
# 1   0 ( 1-12, 1- 9) background       zeroGrob[plot.background..zeroGrob.151]
# 2   5 ( 6- 6, 4- 4)     spacer                                zeroGrob[NULL]
# 3   7 ( 7- 7, 4- 4)     axis-l           absoluteGrob[GRID.absoluteGrob.136]
# 4   3 ( 8- 8, 4- 4)     spacer                                zeroGrob[NULL]
# 5   6 ( 6- 6, 5- 5)     axis-t                                zeroGrob[NULL]
# 6   1 ( 7- 7, 5- 5)      panel                      gTree[panel-1.gTree.120]
# 7   9 ( 8- 8, 5- 5)     axis-b           absoluteGrob[GRID.absoluteGrob.128]
# 8   4 ( 6- 6, 6- 6)     spacer                                zeroGrob[NULL]
# 9   8 ( 7- 7, 6- 6)     axis-r                                zeroGrob[NULL]
# 10  2 ( 8- 8, 6- 6)     spacer                                zeroGrob[NULL]
# 11 10 ( 5- 5, 5- 5)     xlab-t                                zeroGrob[NULL]
# 12 11 ( 9- 9, 5- 5)     xlab-b titleGrob[axis.title.x.bottom..titleGrob.139]
# 13 12 ( 7- 7, 3- 3)     ylab-l   titleGrob[axis.title.y.left..titleGrob.142]
# 14 13 ( 7- 7, 7- 7)     ylab-r                                zeroGrob[NULL]
# 15 14 ( 4- 4, 5- 5)   subtitle       titleGrob[plot.subtitle..titleGrob.148]
# 16 15 ( 3- 3, 5- 5)      title          titleGrob[plot.title..titleGrob.145]
# 17 16 (10-10, 5- 5)    caption          zeroGrob[plot.caption..zeroGrob.150]
# 18 17 ( 2- 2, 2- 2)        tag              zeroGrob[plot.tag..zeroGrob.149]

然后grob根据它选择正确的name并看着,说,

str(p$layers[[1]]$geom_params$grob$grobs[[15]])

很容易找到我们想要的

p$layers[[1]]$geom_params$grob$grobs[[15]]$children[[1]]$label
# [1] "Source: `base` package"

推荐阅读