Bram Kuijper
2006-Dec-28 14:08 UTC
[R] lattice xyplot: plot multiple lines with different colors
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
Gabor Grothendieck
2006-Dec-28 14:21 UTC
[R] lattice xyplot: plot multiple lines with different colors
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.
>
Chuck Cleland
2006-Dec-28 14:21 UTC
[R] lattice xyplot: plot multiple lines with different colors
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 > > ______________________________________________ > 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. > >-- Chuck Cleland, Ph.D. NDRI, Inc. 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894