Skip to content

Support formula notation in data argument ? #3138

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

Closed
moodymudskipper opened this issue Feb 13, 2019 · 2 comments · Fixed by #3245
Closed

Support formula notation in data argument ? #3138

moodymudskipper opened this issue Feb 13, 2019 · 2 comments · Fixed by #3245
Labels
feature a feature request or enhancement
Milestone

Comments

@moodymudskipper
Copy link

The data argument can be used to pass a function to apply on the plot's data. It would be nice to be able to use the formula notation as well.

# works
ggplot(iris,aes(Sepal.Length,Sepal.Width)) + 
  geom_point(data = head)
# works as well
ggplot(iris,aes(Sepal.Length,Sepal.Width)) + 
  geom_point(data = function(x) head(x,10))
# doesn't
ggplot(iris,aes(Sepal.Length,Sepal.Width)) + 
  geom_point(data = ~head(.,10))

It should be a simple fix, either in layer with something like if(inherits(data,"formula")) data <- rlang::as_function(data) or in later in ggproto after defining fortify.formula <- function(model, data, ...) model to return the input as is done with fortify.function, with the same but also testing for missing with something like if(!missing(data) && inherits(data,"formula")) data <- rlang::as_function(data)

trace(ggplot2:::layer,at =2,quote(
  if(inherits(data,"formula")) data <- rlang::as_function(data)))   

# now works
ggplot(iris,aes(Sepal.Length,Sepal.Width)) + 
  geom_point(data = ~head(.,10))
@yutannihilation
Copy link
Member

I'm going to make this happen after #3038.

@lock
Copy link

lock bot commented Oct 21, 2019

This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/

@lock lock bot locked and limited conversation to collaborators Oct 21, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature a feature request or enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants