[R] lattice xyplot - formatting of multiple Y variables when using subgroups

Prasenjit Kapat kapatp at gmail.com
Sun Oct 17 19:02:25 CEST 2010


On Sun, Oct 17, 2010 at 10:01 AM, Coen van Hasselt
<coenvanhasselt at gmail.com> wrote:
> Hi all,
>
> Using xyplot I want to print to Y variables (y1, y2) versus X, conditional
> on the group.
> How can I obtain a line (type="l") for one relationship (ie. y1 ~ x) and
> points (type="p") for the other (y2 ~ x) ?
>
> library(lattice)
>
> # create some sample data
> df<-data.frame(group=as.factor(c(rep("a",4), rep("b",4))), # grouping
> variable for conditional plots
>    x=c(1:4,1:4), # x variable
>    y1=rnorm(4,0,1), # y1
>    y2=rnorm(4,0,1)) # y2
>
>
> # Basically I want this plot, but then for y1~x points and for y2~x lines.
> xyplot(y1+y2 ~ x|group, data=df)
>
> # This works, but then I get the same plot in the two grouping windows
> because conditioning is lost in the panel function.
> xyplot(y1+y2 ~ x|group, data=df, type="l",
>    panel=function(...){panel.xyplot(df$x,df$y1);
>            panel.xyplot(df$x,df$y1, type="l")})

Try:

xyplot(y1+y2 ~ x|group, data=df, type=c("l","p"),
   panel=panel.superpose, distribute.type = TRUE)

> Thanks for your help.
>
> Coen

-- 
Prasenjit



More information about the R-help mailing list