Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Great tutorial! quick question on after_stat. #5

Open
Paul-Yuchao-Dong opened this issue Apr 4, 2020 · 0 comments
Open

Great tutorial! quick question on after_stat. #5

Paul-Yuchao-Dong opened this issue Apr 4, 2020 · 0 comments

Comments

@Paul-Yuchao-Dong
Copy link

ggplot(mpg) +

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant