ggpmisc icon indicating copy to clipboard operation
ggpmisc copied to clipboard

Shared computation between plot layers

Open aphalo opened this issue 3 months ago • 2 comments

I can imagine that two stats both have a full compute but store the intermediate results (e.g. the model in geom_smooth()) in a cache. The cache can then be read out by the second stat, which then doesn't have to do the full compute step. You can then use the Stat$finish_layer, which is executed after all stat computation have completed, to clear the cache for the next round of computation.

Originally posted by @teunbrand in https://github.com/ggplot2-extenders/ggplot-extension-club/discussions/117#discussioncomment-14390503

aphalo avatar Sep 29 '25 20:09 aphalo

A cache is something I have considered, but have not got around to implement in 'ggpmisc'. Two UI are possible: checking that the new layer call has the same arguments as the cached one, or a lot simpler, let the user explicitly request to reuse the cached computation results. Even the request to cache the results from the first layer could be explicit. The most intuitive approach would be to extend the grammar using an operator to indicate the layers that share computations. The code could then look something like:

ggplot(data, aes()) +
    geom_point() +
    stat_poly_line(formula, method) > stat_poly_eq()

aphalo avatar Sep 29 '25 20:09 aphalo

|> could possibly be used instead of >, similarly as it is used in 'ggplender'. But in 'ggblender' |> works across multiple layers, not just a pair of layers.

aphalo avatar Oct 07 '25 22:10 aphalo