[R] ggplot legend for multiple time series

baptiste auguie baptiste.auguie at googlemail.com
Tue Dec 1 20:53:11 CET 2009


Hi,

I don't understand why you used scale_manual_colour if you want only
black lines. To have different line types in the legend you can map
the linetype to the data,


huron <- data.frame(year=1875:1972, level=LakeHuron)

ggplot(huron, aes(year)) +
   geom_line(aes(y=level+5, linetype="above")) +
   geom_line(aes(y=level-5, linetype="below")) +
   scale_linetype_manual("offset", c(1, 2))

HTH,

baptiste
2009/12/1 Edwin Sun <csun at cfr.msstate.edu>:
>
> Hello All,
>
> I am trying to create a legend for a black-white graph. The package I use is
> ggplot2. It can add colors to the legend key but not line types. Can you
> please help?
>
> # example from Wickman (2009, ggplot2 – elegant graphics for data analysis,
> page 109)
>
> library(ggplot2)
> huron <- data.frame(year=1875:1972, level=LakeHuron)
> ggplot(huron, aes(year)) +
>    geom_line(aes(y=level+5, colour="above")) +
>    geom_line(aes(y=level-5, colour="below")) +
>    scale_colour_manual("Direction", c("above"="black", "below"="black"))
>
> Thanks,
>
>
> Edwin
>
> Changyou Sun, Ph.D.
> Associate Professor
> Natural Resource Policy & Economics
> Box 9681, Department of Forestry
> Mississippi State University
> Mississippi State, MS 39762
>
> #363 Thompson Hall
> (662) 325 7271 (ph), 325 8726 (fax)
> csun at cfr.msstate.edu
> www.cfr.msstate.edu/forestry
> --
> View this message in context: http://n4.nabble.com/ggplot-legend-for-multiple-time-series-tp932430p932430.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