[R] Lattice xyplot and groups

Axel axelgrau at gmail.com
Tue Sep 21 20:51:16 CEST 2010


Hi,

I'm trying to plot many (x, y) data files using the xyplot function
from the lattice package. Each file can be classified by set name (s1,
s2,...) and data type (A, B, ...). Each data set contains a different
number of files. If the data is grouped by type or set and visualized
as line plot with xyplot(type='l'), the first and last point are
joined into a closed line that traverses the whole plot from left to
right.
This is an example showing the problem:

library(lattice)
x1 <- seq(-10, 10, 0.5)
x2 <- seq(-10, 10, 0.1)

df <- data.frame(x=x1, y=sin(x1), id='1a_s1', type='A', set='s1')
df <- rbind(df, data.frame(x=x1, y=cos(x1), id='1b_s1', type='B', set='s1'))
df <- rbind(df, data.frame(x=x1, y=3*sin(2*x1), id='2a_s1', type='A', set='s1'))
df <- rbind(df, data.frame(x=x1, y=3*cos(2*x1), id='2b_s1', type='B', set='s1'))

df <- rbind(df, data.frame(x=x2, y=sin(x2), id='1a_s1', type='A', set='s2'))
df <- rbind(df, data.frame(x=x2, y=cos(x2), id='1b_s1', type='B', set='s2'))
df <- rbind(df, data.frame(x=x2, y=3*sin(2*x2), id='2a_s1', type='A', set='s2'))
df <- rbind(df, data.frame(x=x2, y=3*cos(2*x2), id='2b_s1', type='B', set='s2'))

p=xyplot(y~x|set, df, type='l', group=type, auto.key = list(points =
FALSE, lines = TRUE, columns = 2))
print(p)

I would really appreciate if you could suggest a way to keep the lines
open, either by changing the plot command or by building the data
frame in a better way. One solution would be to group by id, but then
I don't know if it is possible to make the line color the same for a
given data type.

Thank you!



More information about the R-help mailing list