Hello I have the following code.
In the data frame puntos
I create the maximum and minimum of the "x" and "y" axes.
puntos<-data.frame(x1=as.Date(c("1980-01-01","2000-01-01")),
x2=as.Date(c("1985-01-01","2005-01-01")),
y1=c(-Inf,-Inf),
y2=c(Inf, Inf))
When performing the line graph with the following code, the graph comes out without problems.
economics %>%
ggplot()+
geom_line(aes(x=date, y=unemploy))+
geom_rect(data = puntos, aes(xmin=x1, xmax=x2,
ymin=y1, ymax=y2),
fill = "red", color = "black", alpha=0.1)+
theme_bw()
But when I change the aesthetics of the geom_line() in the ggplot() function
economics %>%
ggplot(aes(x=date, y=unemploy))+
geom_line()+
geom_rect(data = puntos, aes(xmin=x1, xmax=x2,
ymin=y1, ymax=y2),
fill = "red", color = "black", alpha=0.1)+
theme_bw()
The following error appears:
Error in FUN(X[[i]], ...) : object 'unemploy' not found
Isn't it supposed that if I change geom_rect()
the data, the aesthetic that is defined comes from the name of the variables of this new data?
I think I am misunderstanding ggplot2. Why does the error occur?
It seems to me (I'm not 100% sure) that the problem is that
geom_rect
, although it doesn't explicitly use the arguments x and y, it considers them to some extent because it is related togeom_tile
, which is very similar and does allow those arguments.In your first code you don't define mapping (aes) in the first call to
ggplot
, so all is well.In your second code you do define a mapping and then it is inherited to the following elements.
geom_line
take them and it works fine. It would be supposed togeom_rect
do the same thing, since you are defining its own data and mapping, using only valid arguments for that function: xmin, xmax, ymin, ymax.However when we have an "inheritable" argument (defined in the first call to
ggplot
) it is propagated to all subsequent calls. When ageom_
particular defines its mapping it is combined with the inherited elements.crude example:
x
is inherited,y
and is defined directly in the call.So in your case
x
andy
they are inheriting the call fromgeom_rect
, but since the data ispuntos
there and the columns are not there, then the error.But
x
andy
should not be valid arguments togeom_rect
. They shouldn't and if you explicitly call them with a valid value you get a bogus warning: I ignore them, but fail because I can't find them.It returns the same error and a warning that aesthetics x and y are being ignored.
But if you define them as null the code goes:
Another alternative, pointed out by Patricio in a comment, is to use the argument
inherits.aes = FALSE
in the call ofgeom_rect()
. In this way, the previously defined mappings are ignored and the function does not look for axyy when it is not necessary and it will not find them.Conclusion: You
geom_rect
are somehow receiving and processing axy y. Perhaps the warning is issued after you searched for them and did not find them in the datapuntos
. It is assumed thatx
andy
have nothing to do, but there is a bug or some undocumented feature that makes itgeom_rect
look for them and not finding them in the data.framepuntos
produces the error.It is not uncommon that there are geoms that are wrappers for others, I suspect that the primitive here is
geom_tile
(which does accept x and y as arguments) and that itgeom_rect
only offers an interface to make it easier to use by defining the corners instead of center, height and width.