Skip to content

Support formula notation in data argument ? #3138

Closed
@moodymudskipper

Description

@moodymudskipper

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))

Metadata

Metadata

Assignees

No one assigned

    Labels

    featurea feature request or enhancement

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions