I am new to ggplot. I am trying to understand how to use ggplot. I am reading Wickham's book and still trying to wrap my head around mapping vs. setting color.
A) Discrete case Here's what I did:
library(dplyr)
library(ggplot2)
test<-filter(mpg,year==2008)
test<-test[1:10,]
grid <- data_frame(displ = seq(min(mpg$displ), max(mpg$displ), length = 50))
mod <- loess(hwy ~ displ, data = mpg)
grid$hwy <- predict(mod, newdata = grid)
a) Use discrete values and then use (aes(color = "xyz"))
ggplot(mpg,aes(displ,hwy)) +
geom_point() +
geom_text(data = test,aes(label=trans,color = "blue"))
This just adds a legend with the label "blue". Why does this happen?
b) Supply color = "blue" outside of aesthetics.
ggplot(mpg,aes(displ,hwy)) +
geom_point() +
geom_text(data = test,aes(label=trans),color = "blue")
This works and changes the color to "blue".
B) Continuous case
a) Use (aes(color = "xyz")) Here's what I did:
ggplot(mpg,aes(displ,hwy)) +
geom_point() +
geom_line(data = grid, aes(colour = "green"),size=1.5)
As with the case a) for discrete case, this adds a pink line with the text "green"
b) Supply color outside of aesthetics.
ggplot(mpg,aes(displ,hwy)) +
geom_point() +
geom_line(data = grid, colour = "green",size=1.5)
Here, the color of the line does change to "Green" and I have lost the label.
So, I am not understanding the value of aes(colour = "xyz"). All it does is that add a label. Isn't it? Why would we use it?
aes(colour = 'xyz'). I can't think of a good reason to use it other than to illustrate why you shouldn't use it. - Gregor Thomas'xyz'is not the name of a color but a label. Likeggplot(mtcars, aes(mpg, disp)) + geom_point() + geom_smooth(aes(color = 'this is a loess function')). - Axemanggplot(mtcars, aes(mpg, disp)) + geom_point() + geom_smooth(aes(color = "loess")) + geom_smooth(method = 'lm', aes(color = 'lm'))- Gregor Thomas