I am trying to write unit tests for functions where I use cowplot::plot_grid()
to combine ggplot2
plots. For example,
# 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).
Created on 2019-01-05 by the reprex package (v0.2.1)
For ggplot2
plots, I can build the plot and extract these details.
# 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"
But how can I extract such details about the individual plots from the combined plot-
pb <- ggplot2::ggplot_build(p)
So, for example, how can I extract subtitle for p1
from pb
?
We may do as follows:
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"
With this approach there would be no gains from additionally using ggplot_build
as we would still need to go to the layers
element.
I believe there is no shorter way to do this. It kind of makes sense that we need to delve into layers
as pb$plot$labels$title
is about the whole plot, while our p
is a combination of two.
In case you want to extract some other elements, it should be pretty doable by looking at, say, in the case of the first layer,
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]
then picking the right grob
according to its name
and looking at, say,
str(p$layers[[1]]$geom_params$grob$grobs[[15]])
where it's quite easy to find that we want
p$layers[[1]]$geom_params$grob$grobs[[15]]$children[[1]]$label
# [1] "Source: `base` package"