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

Wrong evaluation environment for expressions in aes() #743

wch opened this Issue Dec 20, 2012 · 2 comments


None yet
3 participants
Copy link

wch commented Dec 20, 2012

Expressions in aes() are evaluated in the global environment, but they should be evaluated in the calling environment. Example:


foo <- 4
ggplot(mtcars, aes(x = wt + foo, y = mpg)) +
# Works

f <- function() {
  foo2 <- 4
  ggplot(mtcars, aes(x = wt + foo2, y = mpg)) +

# Error in eval(expr, envir, enclos) : object 'foo2' not found

It can be worked around by using environment=environment():

g <- function() {
  foo3 <- 4
  ggplot(mtcars, aes(x = wt + foo3, y = mpg),
         environment = environment()) +

# Works

I think that should be the default behavior. @hadley, is the current behavior a bug or a feature?

The problem is the definition of in plot.r: <- function(data, mapping=aes(), ...,
  environment = globalenv())

Changing it to use parent.env() fixes the problem: <- function(data, mapping=aes(), ...,
  environment = parent.env())

This comment has been minimized.

Copy link

hadley commented Feb 24, 2014

It's a bug, but I don't think we can fix it now.

@hadley hadley closed this Feb 24, 2014


This comment has been minimized.

Copy link

tomhopper commented Feb 25, 2014

Hadley, is there a way to note the work-around in the documentation for aes()?

tsibley added a commit to tsibley/ggplot2 that referenced this issue Jul 1, 2014

tonyfujs pushed a commit to thelayc/laycEnrollment that referenced this issue Mar 15, 2015

@lock lock bot locked as resolved and limited conversation to collaborators Jun 20, 2018

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