Skip to content

Mapped variable not found when it is in a zero row data.frame #332

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
BrianDiggs opened this issue Dec 30, 2011 · 3 comments · Fixed by #499
Closed

Mapped variable not found when it is in a zero row data.frame #332

BrianDiggs opened this issue Dec 30, 2011 · 3 comments · Fixed by #499
Labels
bug an unexpected problem or unintended behavior
Milestone

Comments

@BrianDiggs
Copy link
Contributor

Summary

When an aesthetic is mapped to a variable in a data.frame which has that variable, but does not have any rows of data, an error is thrown.

Reproducible example

empty <- data.frame(x=numeric(0), y=numeric(0))
notempty <- data.frame(x=1, y=1)

ggplot(mtcars, aes(x=mpg, y=wt)) +
    geom_point() +
    geom_point(data=empty, mapping=aes(x=x,y=y))

ggplot(mtcars, aes(x=mpg, y=wt)) +
    geom_point() +
    geom_point(data=notempty, mapping=aes(x=x,y=y))

ggplot(mtcars, aes(x=mpg, y=wt)) +
    geom_point(data=empty, mapping=aes(x=x,y=y))

ggplot(mtcars, aes(x=mpg, y=wt)) +
    geom_point(data=notempty, mapping=aes(x=x,y=y))

ggplot()+
    geom_point(data=empty, mapping=aes(x=x,y=y))

ggplot()+
    geom_point(data=notempty, mapping=aes(x=x,y=y))

ggplot(empty, aes(x=x,y=y))+
    geom_point()

Results

> empty <- data.frame(x=numeric(0), y=numeric(0))
> notempty <- data.frame(x=1, y=1)
> 
> ggplot(mtcars, aes(x=mpg, y=wt)) +
+     geom_point() +
+     geom_point(data=empty, mapping=aes(x=x,y=y))
Error in eval(expr, envir, enclos) : object 'x' not found
> 
> ggplot(mtcars, aes(x=mpg, y=wt)) +
+     geom_point() +
+     geom_point(data=notempty, mapping=aes(x=x,y=y))
> 
> ggplot(mtcars, aes(x=mpg, y=wt)) +
+     geom_point(data=empty, mapping=aes(x=x,y=y))
Error in eval(expr, envir, enclos) : object 'x' not found
> 
> ggplot(mtcars, aes(x=mpg, y=wt)) +
+     geom_point(data=notempty, mapping=aes(x=x,y=y))
> 
> ggplot()+
+     geom_point(data=empty, mapping=aes(x=x,y=y))
Error in eval(expr, envir, enclos) : object 'x' not found
> 
> ggplot()+
+     geom_point(data=notempty, mapping=aes(x=x,y=y))
> 
> ggplot(empty, aes(x=x,y=y))+
+     geom_point()
Error in eval(expr, envir, enclos) : object 'x' not found

Expected results

  1. For the first plot, the second geom_point should not add anything, and so the plot should be equivalent to ggplot(mtcars, aes(x=mpg, y=wt))+geom_point()
  2. The second plot works as expected
  3. The third plot maybe should throw an error, but not that error. x is in empty, but there is no data to plot.
  4. The fourth works as expected.
  5. The fifth should behave like the third.
  6. The sixth works as expected.
  7. The seventh should also behave like the third.

sessionInfo

> sessionInfo()
R version 2.14.0 (2011-10-31)
Platform: i386-pc-mingw32/i386 (32-bit)

locale:
[1] LC_COLLATE=English_United States.1252 
[2] LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] ggplot2_0.9.0 devtools_0.4 

loaded via a namespace (and not attached):
 [1] colorspace_1.1-0   dichromat_1.2-3    digest_0.5.1       grid_2.14.0       
 [5] MASS_7.3-16        memoise_0.1        munsell_0.3        plyr_1.6          
 [9] proto_0.3-9.2      RColorBrewer_1.0-5 RCurl_1.8-0.1      reshape2_1.2      
[13] scales_0.1.0       stringr_0.6       

This "0.9.0" is commit 16e69f6. It is also an error in 0.8.9, although sometimes with different (also non-informative) error messages.

@hadley
Copy link
Member

hadley commented Dec 30, 2011

Is this a duplicate of #31 ?

@BrianDiggs
Copy link
Contributor Author

I don't believe it is a duplicate of #31 because the example there runs and gives a plot, just not as expected. The problem here is that an error is thrown when plotting saying that the variable can not be found. They may be related, of course.

Also, this may be a duplicate of #176, but I wasn't sure because there the xintercept part is outside an aes. However running that code gives a similar error (Error in eval(expr, envir, enclos) : object 'xintercept' not found) and I seem to recall that geom_vline makes a data.frame in the background. So this may be duplicate, or at least broadening, of #176.

@hadley
Copy link
Member

hadley commented Dec 30, 2011

Taking a quick look at the code, I think it is related to #31, but not I'm completely sure if it's the same or not. Somewhere the layer data is getting dropped completely, and only the plot data used - which does not contain x and y variables.

@hadley hadley closed this as completed in fe42a64 Apr 20, 2012
@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.
Labels
bug an unexpected problem or unintended behavior
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants