Would like to be able to add some bonus aes by adding defaults.

We see, adding ‘known’ (anticipated?) aes works nicely (i.e. label)

library(tidyverse)
diamonds %>% 
  ggplot() + 
  aes(y = cut) + 
  geom_bar() + 
  stat_count(geom = "text", 
             aes(label = after_stat(count)))

StatCount$default_aes
## Aesthetic mapping: 
## * `x`      -> `after_stat(count)`
## * `y`      -> `after_stat(count)`
## * `weight` -> 1
StatCount$default_aes <- aes(x = after_stat(count),
                             y = after_stat(count),
                             weight = 1,
                             label = after_stat(count),
                             percent = after_stat(100*count/sum(count)) %>% 
                               round(1) %>% 
                               paste0("%"),
                             another_aes = after_stat("bonus aes"))

geom_barlab <- function(...){stat_count(geom = "text", ...)}

diamonds %>% 
  ggplot() + 
  aes(y = cut) + 
  geom_bar() + 
  geom_barlab()

looks like we should be able to use it, inspecting w/ layer_data()

layer_data()  # wow!  percent is here.  (but we can't seem to access it)
##       x label percent another_aes count prop y flipped_aes PANEL group xmin
## 1  1610  1610      3%   bonus aes  1610    1 1        TRUE     1     1    0
## 2  4906  4906    9.1%   bonus aes  4906    1 2        TRUE     1     2    0
## 3 12082 12082   22.4%   bonus aes 12082    1 3        TRUE     1     3    0
## 4 13791 13791   25.6%   bonus aes 13791    1 4        TRUE     1     4    0
## 5 21551 21551     40%   bonus aes 21551    1 5        TRUE     1     5    0
##    xmax ymin ymax colour   fill linewidth linetype alpha
## 1  1610 0.55 1.45     NA grey35       0.5        1    NA
## 2  4906 1.55 2.45     NA grey35       0.5        1    NA
## 3 12082 2.55 3.45     NA grey35       0.5        1    NA
## 4 13791 3.55 4.45     NA grey35       0.5        1    NA
## 5 21551 4.55 5.45     NA grey35       0.5        1    NA

let’s use ggtrace ‘layer_all_stages’ :-) to see when columns should be available?

layer_all_stages <- function(){

a <- ggtrace::layer_before_stat() %>% head(3)
b <- ggtrace::layer_after_stat() %>% head(3)
c <- ggtrace::layer_before_geom() %>% head(3)
d <- ggtrace::layer_after_scale() %>% head(3)

list(before_stat = a, after_stat = b, 
     before_geom = c, after_scale = d)

}



layer_all_stages()
## ✔ Executed `ggtrace_inspect_args(last_plot(), ggplot2:::Layer$compute_statistic)$data`
## ✔ Executed `ggtrace_inspect_return(last_plot(), ggplot2:::Layer$compute_statistic)`
## ✔ Executed `ggtrace_inspect_args(last_plot(), ggplot2:::Layer$compute_geom_1)$data`
## ✔ Executed `ggtrace_inspect_return(last_plot(), ggplot2:::Layer$compute_geom_2)`
## $before_stat
## # A tibble: 3 × 3
##   y          PANEL group
##   <mppd_dsc> <fct> <int>
## 1 5          1         5
## 2 4          1         4
## 3 2          1         2
## 
## $after_stat
## # A tibble: 3 × 7
##   count  prop y          width flipped_aes PANEL group
##   <dbl> <dbl> <mppd_dsc> <dbl> <lgl>       <fct> <int>
## 1  1610     1 1            0.9 TRUE        1         1
## 2  4906     1 2            0.9 TRUE        1         2
## 3 12082     1 3            0.9 TRUE        1         3
## 
## $before_geom
## # A tibble: 3 × 11
##       x label percent another_aes count  prop y          width flipped_aes PANEL
##   <dbl> <dbl> <chr>   <chr>       <dbl> <dbl> <mppd_dsc> <dbl> <lgl>       <fct>
## 1  1610  1610 3%      bonus aes    1610     1 1            0.9 TRUE        1    
## 2  4906  4906 9.1%    bonus aes    4906     1 2            0.9 TRUE        1    
## 3 12082 12082 22.4%   bonus aes   12082     1 3            0.9 TRUE        1    
## # ℹ 1 more variable: group <int>
## 
## $after_scale
## # A tibble: 3 × 19
##       x label percent another_aes count  prop y          flipped_aes PANEL group
##   <dbl> <dbl> <chr>   <chr>       <dbl> <dbl> <mppd_dsc> <lgl>       <fct> <int>
## 1  1610  1610 3%      bonus aes    1610     1 1          TRUE        1         1
## 2  4906  4906 9.1%    bonus aes    4906     1 2          TRUE        1         2
## 3 12082 12082 22.4%   bonus aes   12082     1 3          TRUE        1         3
## # ℹ 9 more variables: xmin <dbl>, xmax <dbl>, ymin <mppd_dsc>, ymax <mppd_dsc>,
## #   colour <lgl>, fill <chr>, linewidth <dbl>, linetype <dbl>, alpha <lgl>

but… throws error when we actually try ‘Caused by error in compute_geom_1() geom_text() requires the following missing aesthetics: label’

# but
diamonds %>% 
  ggplot() + 
  aes(y = cut) + 
  geom_bar() + 
  stat_count(geom = "text", aes(label = after_scale(percent))) 
## Error in `stat_count()`:
## ! Problem while setting up geom.
## ℹ Error occurred in the 2nd layer.
## Caused by error in `compute_geom_1()`:
## ! `geom_text()` requires the following missing aesthetics: label.

Closing remarks, Other Relevant Work, Caveats