简体   繁体   English

如何更改ggplot中不同数据集线条的颜色并有图例

[英]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然后我尝试使用scale_color_manual()但我不知道如何为 2 个不同的数据集形成参数

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"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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