I have a plot whose legend should contain two levels. Ggplot shows a legend with six levels, including four which do not appear in the data frame. A simple reproduction of the problem is shown below:
x <- seq(from=1, to=10, by=0.5)
y.2 <- x^2
y.3 <- x^3
exponent.2 <- 2
exponent.3 <- 3
data2 <- data.frame(x=x, y=y.2, exponent = exponent.2)
data3 <- data.frame(x=x, y=y.3, exponent = exponent.3)
data <- rbind(data2, data3)
p <- ggplot(data,aes(x,y,group=exponent, color=exponent)) + geom_line()
p
I am obviously doing something wrong, but need help in finding the problem.
ggplot2 interprets exponent as a continuous variable; thus it displays a number of breaks similarly to what
pretty(c(2, 3))would return.You can use
colour = factor(exponent), or specify explicitely the colour breaks.