[R] Lattice xyplot and groups

Deepayan Sarkar deepayan.sarkar at gmail.com
Fri Sep 24 10:10:13 CEST 2010


On Wed, Sep 22, 2010 at 12:21 AM, Axel <axelgrau at gmail.com> wrote:
> 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.

That would be the logically correct approach. Here are a couple of
ways to specify color:

xyplot(y~x|set, df, type='l', groups=id,
       auto.key = list(text = c("A", "B"), lines = TRUE, points =
FALSE, columns = 2),
       par.settings = list(superpose.line =
Rows(trellis.par.get("superpose.line"), 1:2)))

xyplot(y~x|set, df, type='l', groups=id,
       auto.key = list(text = c("A", "B"), lines = TRUE, points =
FALSE, columns = 2),
       par.settings = simpleTheme(col = c("blue", "red")))

-Deepayan



More information about the R-help mailing list