ggplot2 icon indicating copy to clipboard operation
ggplot2 copied to clipboard

Feature request: implement `ggplot_build.ggplot_built` S3 method.

Open teunbrand opened this issue 3 months ago • 0 comments

Several getter functions, like layer_data(), call ggplot_build() on a plot. For testing purposes, it might be more convenient to build a plot once and then extract relevant data/grobs/scales without having to build the plot every time. Therefore, it can be convenient to have a ggplot_build.ggplot_built S3 method that just returns the input unaltered. That way, it should be safe to use layer_data() or somesuch with a pre-build plot as input.

To express in code, I would like the following to work:

library(ggplot2)
p <- ggplot(mpg, aes(displ, hwy)) + geom_point()
build <- ggplot_build(p)

layer_data(build)
#> Error in UseMethod("ggplot_build"): no applicable method for 'ggplot_build' applied to an object of class "ggplot_built"
layer_grob(build)
#> Error in UseMethod("ggplot_build"): no applicable method for 'ggplot_build' applied to an object of class "ggplot_built"
layer_scales(build)
#> Error in UseMethod("ggplot_build"): no applicable method for 'ggplot_build' applied to an object of class "ggplot_built"

Created on 2024-03-25 with reprex v2.1.0

teunbrand avatar Mar 25 '24 10:03 teunbrand