简体   繁体   中英

How to change colors of lines of different datasets in ggplot and have a legend

I want to make a plot with a legend of two different datasets in the same plot

I first tried this:

df1 = data.frame(c11 = c(1:5), c12 = c(1:5))
df2 = data.frame(c21 = c(1:5), c22 = (c(1:5))^0.5)

ggplot() + geom_line(data=df1, aes(x=c11, y = c12), color= "red") + 
  geom_line(data=df2, aes(x=c21,y=c22), color = "blue") 

Using this I get the color I want but no legend

Then I tried this:

ggplot() + geom_line(data=df1, aes(x=c11, y = c12, color= "DF1")) + 
  geom_line(data=df2, aes(x=c21,y=c22, color= "DF2")) 

Using this I get a legend but not the correct colors. I then tried using scale_color_manual() but I don´t know how to form an argument for 2 different datasets

I know there is a way but I do not seem to find it Any help is highly appreciated

scale_colour_manual(values = c("red", "blue"))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM