[R] ggplot add a legend?

Ista Zahn istazahn at gmail.com
Tue Dec 18 01:41:30 CET 2012


Map year to an aesthetic and let ggplot do it for you:

df2010$year <- 2010
df2011$year <- 2011
df2012$year <- 2012
dfAll <- rbind(df2010, df2011, df2010)

ggplot(dfAll, aes(x=Price, y=percenCapacity, color=year) +
scale_x_log10()

Best,
Ista

On Mon, Dec 17, 2012 at 6:03 PM, jcrosbie <james at crosb.ie> wrote:
>
> How to I go about adding a legend  to the following plot?
>
> ggplot() +  geom_line(aes(x=df2010$Price,y=df2010$percentCapacity), colour =
> "red")
> + geom_line(aes(x=df2011$Price,y=df2011$percentCapacity), colour = "green")
> +geom_line(aes(x=df2012$Price,y=df2012$percentCapacity), colour = "blue")
> +scale_x_log10()
>
>
> <http://r.789695.n4.nabble.com/file/n4653389/Rplot01.jpeg>
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/ggplot-add-a-legend-tp4653389.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list