[R] Lattice xyplot and groups

Axel axelgrau at gmail.com
Tue Sep 21 22:20:23 CEST 2010


> Does using
>
>     df = df[order(df$type,df$set,df$x),]
>
> before calling xyplot fix the problem?

Thank you very much for your suggestion. It does make the lines open,
but unfortunately it mixes different id together.
With the new ordering the plot looks something like this:

(x=-10, y_id1) -> (x=-10, y_id2) -> (x=-9.5, y_id1) -> (x=-9.5, y_id2)...

I tried many combinations. It seems that when df$id is sorted before
df$dx the plot has a loop. In the other case the ids get mixed.

By the way, my previous example had wrong id labels. This should be right:

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_s2', type='A', set='s2'))
df <- rbind(df, data.frame(x=x2, y=cos(x2), id='1b_s2', type='B', set='s2'))
df <- rbind(df, data.frame(x=x2, y=3*sin(2*x2), id='2a_s2', type='A', set='s2'))
df <- rbind(df, data.frame(x=x2, y=3*cos(2*x2), id='2b_s2', 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)



More information about the R-help mailing list