Great tutorial! quick question on after_stat.
Paul-Yuchao-Dong opened this issue · 0 comments
Paul-Yuchao-Dong commented
ggplot2_workshop/exercises.Rmd
Line 193 in ef59259
Just trying to stretch the new functionalities of after_stat. It is already much clear than its predecessors. Yet maybe I am mistaken, after_stat currently do not seem to be able to take named functions. I tried to look into the error message and source code but tidy evaluation is a bit hard for me to pin down. Is that a current limitation? Wonder if I can help?
library(ggplot2)
percentage <- function() count / sum(count)
ggplot(mpg) +
geom_bar(aes(x = class, y = after_stat((function() count / sum(count))())))
ggplot(mpg) +
geom_bar(aes(x = class, y = after_stat((percentage)())))
#> Error in (percentage)(): object 'count' not found
Created on 2020-04-04 by the reprex package (v0.3.0)
library(ggplot2)
library(rlang)
percentage <- function() {
count <- enquo(count)
expr(!!count / sum(!!count))
}
ggplot(mpg) +
geom_bar(aes(x = class, y = after_stat(eval_tidy(percentage()))))
#> Error in (function (x) : object 'count' not found
Created on 2020-04-04 by the reprex package (v0.3.0)