Intro Thoughts

Status Quo

library(tidyverse)

Experiment

library(ggtrace)
library(ggplot2)

as.list(body(ggplot2:::ggplot_build.ggplot)) ->
  build

sapply(build, deparse1) |>
  grep(x = _, "l\\$\\w+", value = TRUE) |>
  gsub(x = _, ".*l(\\$\\w+).*", "ggplot2:::Layer\\1") ->
layer_methods

gsub(x = layer_methods, ".*\\$", "") ->
  names(layer_methods)

ggplot(mtcars, aes(cyl)) +
  stat_count() ->
p

stat_count_Layer <- lapply(
  layer_methods,
  \(x) {
    rlang::inject(
      capture_fn(p, !!rlang::parse_expr(x), cond = 1)
    )
  }
)

list(setup = capture_fn(p, Layout$setup)) ->
  p_Layout

layer_data(p, 1)
##    y count    prop x flipped_aes PANEL group ymin ymax xmin xmax colour   fill
## 1 11    11 0.34375 4       FALSE     1    -1    0   11  3.1  4.9     NA grey35
## 2  7     7 0.21875 6       FALSE     1    -1    0    7  5.1  6.9     NA grey35
## 3 14    14 0.43750 8       FALSE     1    -1    0   14  7.1  8.9     NA grey35
##   linewidth linetype alpha
## 1       0.5        1    NA
## 2       0.5        1    NA
## 3       0.5        1    NA
mtcars |>
  stat_count_Layer$layer_data(plot_data = _) |>
  stat_count_Layer$setup_layer(data = _) |>
    # Detour around Layout$setup()
    list() |>
    p_Layout$setup(data = _) |>
    el(1) |>
  stat_count_Layer$compute_aesthetics(data = _) |>
  stat_count_Layer$compute_statistic(data = _) |>
  stat_count_Layer$map_statistic(data = _) |>
  stat_count_Layer$compute_geom_1(data = _) |>
  stat_count_Layer$compute_geom_2(data = _) |>
  stat_count_Layer$finish_statistics(data = _)
##    y count    prop x flipped_aes PANEL group ymin ymax xmin xmax colour   fill
## 1 11    11 0.34375 4       FALSE     1    -1    0   11  3.1  4.9     NA grey35
## 2  7     7 0.21875 6       FALSE     1    -1    0    7  5.1  6.9     NA grey35
## 3 14    14 0.43750 8       FALSE     1    -1    0   14  7.1  8.9     NA grey35
##   linewidth linetype alpha
## 1       0.5        1    NA
## 2       0.5        1    NA
## 3       0.5        1    NA

Closing remarks, Other Relevant Work, Caveats