I am an absolute beginner in ggplot2. I got frustrated with ggplot2 and started reading Wickham's awesome book. He says that "a scale is required for every aesthetic used on the plot.".
So, I did the following:
Try 1:
huron <- data.frame(year = 1875:1972, level = as.numeric(LakeHuron))
ggplot(huron, aes(year)) +
geom_line(aes(y = level + 5, color = "y+5")) +
scale_color_manual(values = c("orange")) +
geom_line(aes(y = level - 5, color = "y-5")) +
scale_color_manual(values = "blue")
" insufficient value of colors provided."
ggplot(huron, aes(year)) +
geom_line(aes(y = level + 5, color = "y+5")) +
scale_color_manual(values = c("orange", "blue")) + #Added another color here.
geom_line(aes(y = level - 5, color = "y-5"))
color = red
color = blue
To answer the specific question in the comments:
If you want to force specific colors, you need to use scale_color_manual
. As the name suggests, this needs some manual work.
library(ggplot2)
#default colors
#http://stackoverflow.com/a/8197703/1412059
gg_color_hue <- function(n) {
hues = seq(15, 375, length = n + 1)
hcl(h = hues, l = 65, c = 100)[1:n]
}
ggplot(mpg, aes(displ, hwy)) +
geom_point(aes(colour = class)) +
geom_smooth(method = "lm", se = FALSE, aes(color = "model")) +
scale_color_manual(values = setNames(c(gg_color_hue(length(unique(mpg$class))), "red"),
c(unique(mpg$class), "model")))
However, I would use an additional aesthetic for the line type.
ggplot(mpg, aes(displ, hwy)) +
geom_point(aes(colour = class)) +
geom_smooth(method = "lm", se = FALSE, aes(linetype = "model"), color = "red")