I am following Chapter 1 of Wickham and Grolemund's "R for data science" on visualization.
I have tried:
ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy, color = "blue"))
hoping to achieve a plot with all points colored blue, but instead, to my surprise, they were all red! Reading the correct code to achieve the blue points, in page 11 of the printed version or in Section 3.3 of the online version, I found it should be
ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy), color = "blue")
and, in fact, they state that, to manually set an aesthetic you have to give it outside the aes() function, but inside the corresponding geom, geom_point() here. Why is it so? What is the exact explanation for this behavior? In fact, it seemed natural to me that the correct syntax would be the one of the first command.I guess this issue is related either to layers and/or to scope of variables, but I just could not get the hang of it... Can someone spoon feed me?
Edit: Sorry for not doing my correct homework: this is just Exercise 1 proposed in the text itself at the end of the corresponding Section... The answer however still escapes me.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…