Closed
Description
If you add a geom that uses a different data set, and it does not use an aesthetic set in the ggplot()
call,
In the example below, I'm adding a geom_linerange()
which uses a different data set from the one specified in the ggplot()
call. The linerange doesn't have a y
aesthetic. Its data set, d2
, also doesn't have the yval
column which was used in the aesthetic mapping for y
at the top level.
If I map aes(y=NULL)
in the geom_linerange
, it gives a warning that it's ignoring y
. If I don't map y
, then it errors because it still tries to evaluate the y=yval
mapping even though it's not used for that geom.
library(ggplot2)
d <- data.frame(
xval = 1:2,
yval = 1:2
)
d2 <- data.frame(
xval = 3:4,
yval2 = 3:4
)
p <- ggplot(d, aes(x=xval, y=yval)) +
geom_point()
p + geom_linerange(aes(y = NULL, ymin = yval2, ymax = yval2+1), data = d2)
#> Warning: Ignoring unknown aesthetics: y
p + geom_linerange(aes(ymin = yval2, ymax = yval2+1), data = d2)
#> Error in eval(expr, envir, enclos) : object 'yval' not found