2014-01-19 72 views
0
x <- seq(0, 50, 1) 
y1 <- exp(0.0509*x) 
y2 <- exp(0.0519*x) 
df <- data.frame(x,y1,y2) 

ggplot(df, aes(x)) +      
    geom_line(aes(y=y1), colour="blue") + 
    geom_line(aes(y=y2), colour="red") 

根據代碼,我想如何爲y1添加名爲「group 1」的圖例和爲y2添加「group 2」的圖例。 我已經嘗試了很多我在這個論壇上看到的東西,但是我總是遇到一個錯誤。ggplot將圖例添加到手動曲線

+0

那麼,什麼樣的錯誤? –

+0

好吧,例如,我做了geom_line(aes(y = y2),color =「red」,labels =「test」),我嘗試了很多事情,我不得不提到他們所有的人 – S12000

回答

1

使用此:

library(reshape2) 
library(ggplot2) 
gg <- melt(df,id="x") 
ggplot(gg, aes(x=x, y=value, color=variable))+ 
    geom_line()