hadley/ggplot2 — Issue #332: Mapped variable not found when it is in a zero row data.frame
Author: BrianDiggs
Created: 2011-12-30 00:32:58+00:00
State: open

# 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

```r
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

```r
> 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()`
1. The second plot works as expected
1. The third plot maybe should throw an error, but not that error. `x` is in `empty`, but there is no data to plot.
1. The fourth works as expected.
1. The fifth should behave like the third.
1. The sixth works as expected.
1. The seventh should also behave like the third.

# sessionInfo

```r
> 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 16e69f6348. It is also an error in 0.8.9, although sometimes with different (also non-informative) error messages.