Skip to content

Great tutorial! quick question on after_stat. #5

Description

@Paul-Yuchao-Dong

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)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions