[R] lattice xyplot: plot multiple lines with different colors

Gabor Grothendieck ggrothendieck at gmail.com
Thu Dec 28 15:21:02 CET 2006


Try this:


library(lattice)
x <- 1:10

xyplot(x/max(x) ~ x, type = "n", col = "blue", ylim = 0:1)
for(i in 1:3) {
	trellis.focus("panel", 1, 1)
	panel.lines(x, x^i/max(x^i), col = i)
	trellis.unfocus()
}




On 12/28/06, Bram Kuijper <a.l.w.kuijper at rug.nl> wrote:
> Hi everyone,
>
> I am using the lattice package to plot some simulation results, by using
> the function xyplot(). However, I cannot find a way to plot multiple
> lines within the same xyplot and to have each of the lines be drawn in a
> different color.
>
> This is what I am currently doing:
>
> xyplot(a + b + c ~ x, my_data, panel = panel.lines)
>
> but, of course, all lines are drawn in the same color.
>
>
> What I would prefer, would be to first print a single line in an xyplot
> and then add a new line, and so on (since I want to dynamically
> determine the number of lines that need to be plotted). For example,
> something like this would be nice:
>
> for(i = 1:length(lines_to_be_drawn)) {
>     xyplot(lines_to_be_drawn[i] ~ x, my_data, panel = {some function
> setting a different color});
>
>        // do some other functions like plotting legend, parameters
> }
>
> (although in this putative example, a new xyplot is drawn over the old
> one every time).
>
> anyone any idea to plot multiple lines with different colors in a single
> xyplot? I'd like to stick to the lattice package, since this package has
> more layout possibilities over the traditional plotting functions in R.
>
> cheers,
> Bram
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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