When should we add aes() ? The first code is giving error while the other isn't
Solution 1:
It's not a question of timing. It's a question of default behaviour.
The actual error message is
Error: A continuous variable can not be mapped to shape
So in the first example, because shape
is inside the call to aes()
, it is defining a scale. 1
is regarded not as a constant but as a continuous variable.
In the second example, because shape is outside the call to aes()
, it does not define a scale and therefore the error does not occur.
A variation of the first example which does not produce an error is
gplot(mtcars, aes(wt, mpg)) + geom_point(aes(shape = as.factor(1), size = 4))
because the call to as.factor()
means that 1
is no longer regarded as continuous.