[R] Lattice xyplot and groups

Phil Spector spector at stat.berkeley.edu
Tue Sep 21 21:11:39 CEST 2010


Does using

      df = df[order(df$type,df$set,df$x),]

before calling xyplot fix the problem?

 					- Phil Spector
 					 Statistical Computing Facility
 					 Department of Statistics
 					 UC Berkeley
 					 spector at stat.berkeley.edu




On Tue, 21 Sep 2010, Axel 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.
>
> Thank you!
>
> ______________________________________________
> R-help at r-project.org 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