I am working on creating a custom geom::Stat and am looking for advises. This new stat needs to operate at the group level, so my ggproto object has a compute_group level. However, in some circumstances, the compute_group function would need to know the range of x and y data in the whole panel (not just the group). What would be the best way to pass this information onto the compute_group function?
@technocrat , are you suggesting to pre-calculate the range of data in each panel and store the information in the data object passed on the ggplot call?
The purpose of this new stat is to perform the statistical computations internally during the ggplot call... so the suggested approach would defeat the purpose of this function.
I'm having a difficult time envisioning what the new geom is doing. My thought was that because the ungrouped data frame is in .Global, it can be brought into a geom as the data argument and variables in it subjected to functions and plotted, like this
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library(ggplot2)
mtcars$cyl <- as.factor(mtcars$cyl)
mtcars |>
group_by(cyl,mpg) |>
ggplot(aes(mpg,cyl)) +
geom_point() +
geom_line(data = mtcars, aes(y = mean(drat)/drat^2))