Skip to content

Feature request: implement ggplot_build.ggplot_built S3 method. #5800

Closed
@teunbrand

Description

@teunbrand

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions