Extracts the complete history of a ggplot object's construction, providing a way to reproduce or inspect the plot. Designed to work with ggplot objects of class 'ggcall'.

ggcall(plot)

Arguments

plot

A ggplot object of class 'ggcall'.

Value

Depending on the value of 'call', either a callable expression or a list representing the history of the ggplot object.

Examples

library(ggplot2, exclude = "ggplot")
# Example: Create a function which combines a few ggplot layers
# Typically, it will be a function from your R package where you implemented ggcall
func <- function(data, x, y, bool = TRUE) {
  # layers have to be added with +
  gg <- ggplot(data, aes(x = !!as.name(x), y = !!as.name(y))) +
    geom_point(alpha = 0.4) +
    facet_grid(~gear)

  if (bool) {
    gg <- gg + theme(axis.title.x = element_blank())
  }

  func_internal <- function(gg) {
    gg + labs(x = "custom xlab")
  }

  func_internal(gg)
}
plot_call <- ggcall(func(mtcars, "wt", "mpg"))
# Optionally: Style the code with styler
# deparse1 is recommended and available in R>=4.0.0
if (FALSE) { # \dontrun{
styler::style_text(
  paste(deparse(plot_call), collapse = "\n")
)
} # }