Strange legend using aes(colour) in ggplot2
I have this code:
x <- seq(-600, 600, length=10000)
dat1 <- data.frame(x=x, SD=400, val = (1/(1+10^(-x/400))))
dat2 <- data.frame(x=x, SD=200, val = (1/(1+10^(-x/200))))
dat3 <- data.frame(x=x, SD=600, val = (1/(1+10^(-x/600))))
dat <- rbind(dat1, dat2, dat3)
ggplot(data=dat, aes(x=x, y=val, colour=SD)) + geom_line(aes(group=SD))
What I expected is to 开发者_C百科have 3 curves and I do. However the legend shows that there are 6 curves - for SD 100, 200, 300, 400, 500, 600 instead of only 200, 400, 600. Why is that and how do I fix this?
The legend is not indicating the presence of 6 curves. You've mapped the continuous variable SD to the aesthetic colour, which results in a continuous colour scale, i.e. a gradient. If you want only the three values in the legend, try wrapping SD in factor
:
ggplot(data=dat, aes(x=x, y=val, colour=factor(SD))) + geom_line(aes(group=SD))
精彩评论