Re: [R] lattice and several groups

From: Laurent Rhelp <laurentRhelp_at_free.fr>
Date: Thu 31 Aug 2006 - 07:50:45 EST

hadley wickham a écrit :

>> I would like to use the lattice library to show several groups on
>> the same graph. Here's my example :
>>
>> ## the data
>> f1 <- factor(c("mod1","mod2","mod3"),levels=c("mod1","mod2","mod3"))
>> f1 <- rep(f1,3)
>> f2 <- factor(rep(c("g1","g2","g3"),each=3),levels=c("g1","g2","g3"))
>> df <- data.frame(val=c(4,3,2,5,4,3,6,5,4),
>> x=rep(c(1,2,3),3),f1=f1,f2=f2)
>
>
> It's pretty easy to do this with ggplot:
>
> install.packages("ggplot", repos="http://ggobi.org/r/")
> library(ggplot)
> qplot(x, val, data=df, shape=f2, colour=f1)
>
> Hadley
>
>
Great, Hadley, but the code was not exactly the good one. The code below works fine for me :

install.packages("ggplot", repos="http://ggobi.org/r/") library(ggplot)
qplot(x, val, data=df, glyph=f1, col=f2)

In fact, my problem is to fit the data for every level of the f2 factor, showing the levels of the f1 factor and that for several surveys . Here's an example closer to my actual data :

## the data

n <- 18
x1 <- seq(1,n)

val1 <- -2*x1+50
val2 <- (-2*(x1-8)^2)+100
val3 <- (-2*(x1-8)^2)+50

y <- c(val1,val2,val3)
x <- rep(x1,3)
f1 <- rep(c("mod1","mod2","mod3"),each=n/3)
f1 <- rep(f1,3)
f2 <- rep(c("g1","g2","g3"),each=n)
df <- data.frame(x=x,y=y,f1=f1,f2=f2)

surveys <-
factor(c(rep("survey1",n*3),rep("survey2",n*3),rep("survey3",n*3))) df <- rbind(df,df,df)
df <- data.frame(df,surveys=surveys)

#######################################################################
library(lattice)

para.liste <- trellis.par.get()
superpose.symbol <- para.liste$superpose.symbol superpose.symbol$pch <- c(1,2,3)
trellis.par.set("superpose.symbol",superpose.symbol)

xyplot( y~x | surveys,

        data=df,
        group=f1,
        auto.key=list(space="right")
       )

xyplot( y~x | surveys  ,
        data=df,
        type="l",
        group=f2,
        auto.key=list(space="right",points=FALSE,lines=TRUE)
       )

Can I use the ggplot library ?



R-help@stat.math.ethz.ch 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. Received on Thu Aug 31 07:47:37 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Thu 31 Aug 2006 - 10:24:53 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.