Suppose I have the following data frames:
df1 = data.frame(c11 = c(1:5), c12 = c(1:5))
df2 = data.frame(c21 = c(1:5), c22 = (c(1:5))^0.5)
df3 = data.frame(c31 = c(1:5), c32 = (c(1:5))^2)
I want to plot these as lines in the same plot/panel. I can do this by
p <- ggplot() + geom_line(data=df1, aes(x=c11, y = c12)) +
geom_line(data=df2, aes(x=c21,y=c22)) +
geom_line(data=df3, aes(x=c31, c32))
All these will be black. If I want them in a different color, I can specify the color explicitly as an argument to geom_line()
. My question is can I specify a list of a few colors, say 5 colors, such as, red, blue, green, orange, gray, and use that list so that I do not have to explicitly specify the colors as an argument to geom_line() in case of each line. If the plot p
contains 2 geom_line() statements then it will color them red and blue respectively. If it contains 3 geom_line statements, it will color them red, blue and green. Finally, how can I specify the legend for these plots. Even if I can give the colors as a vector at the end of p
that would be great. Please let me know if the question is not clear.
Thanks.