Making information available in compute_group for a custom ggplot2::Stat object

Hi,

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?

Thanks n advance for your suggestions

Provide for and mapping aes to the un-grouped object is what Iā€™d consider

@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?

Something along those lines, like you can overplot different objects within a ggplot object using different data = and aes() mapping

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))

Created on 2023-03-24 with reprex v2.0.2

The issue is with pacetting (especially with free scales), one cannot really rely on the original data object to determine data ranges within a panel.

Giving a scales geom a function return value based on range() won't work?

Actually, I found the answer. The scales argument of the compute_group already contains the data ranges.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.