19.7 The gtable step
Again, still working with our plot p
p <- ggplot(mpg, aes(displ, hwy, color = drv)) +
geom_point(position = position_jitter(seed = 2022)) +
geom_smooth(method = "lm", formula = y ~ x) +
facet_wrap(vars(year)) +
ggtitle("A plot for expository purposes")
The return value of ggplot_build()
contains the computed data associated with each layer and a Layout
ggproto object which holds information about data other than the layers, including the scales, coordinate system, facets, etc.
[1] "data" "layout" "plot"
[1] "Layout" "ggproto" "gg"
The output of ggplot_build()
is then passed to ggplot_gtable()
to be converted into graphical elements before being drawn:
function (x, newpage = is.null(vp), vp = NULL, ...)
{
set_last_plot(x)
if (newpage)
grid.newpage()
grDevices::recordGraphics(requireNamespace("ggplot2", quietly = TRUE),
list(), getNamespace("ggplot2"))
data <- ggplot_build(x)
gtable <- ggplot_gtable(data)
if (is.null(vp)) {
grid.draw(gtable)
}
else {
if (is.character(vp))
seekViewport(vp)
else pushViewport(vp)
grid.draw(gtable)
upViewport()
}
if (isTRUE(getOption("BrailleR.VI")) && rlang::is_installed("BrailleR")) {
print(asNamespace("BrailleR")$VI(x))
}
invisible(x)
}
<bytecode: 0x55a50675de40>
<environment: namespace:ggplot2>