atom feed3 messages in org.r-project.r-help[R] lattice xyplot: plot multiple lin...
FromSent OnAttachments
Bram KuijperDec 28, 2006 6:08 am 
Gabor GrothendieckDec 28, 2006 6:20 am 
Chuck ClelandDec 28, 2006 6:21 am 
Subject:[R] lattice xyplot: plot multiple lines with different colors
From:Chuck Cleland (
Date:Dec 28, 2006 6:21:26 am

Bram Kuijper 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.

I don't know about dynamically determining the number of lines, but the following gives different colors for each line automatically:

library(lattice) df <- data.frame(a = runif(10), b = runif(10), c = runif(10), x = 1:10) xyplot(a + b + c ~ x, data = df, type = "l", auto.key=TRUE)

cheers, Bram