Read the "panel" part of ?xyplot carefully and note ?panel.superpose.
Typically, when you have a groups argument your panel function must be of
the form
panel = function(x,y,...){
panel.superpose(x,y,...)
etc.
}
However, I think it might all happen automagically if you do the call as:
xyplot(y~x,groups=type,plotdata, type = "b", auto.key=T)
This is also referenced in the "panel" section of the xyplot help,
where you
are referred to ?panel.xyplot for info on the type argument.
So, in brief, read the docs more carefully.
Bert Gunter
Genentech Nonclinical Statistics
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at
r-project.org]
> On Behalf Of array chip
> Sent: Thursday, July 08, 2010 3:58 PM
> To: r-help at r-project.org
> Subject: [R] xyplot help
>
> Hi, I am learning xyplot. I have an example dataset attached.
>
>
plotdata<-read.table("plotdata.txt",sep='\t',header=T,row.names=1)
>
> head(plotdata,n=4)
> y x type
> 1 -4.309601 -0.7448405 A
> 2 -4.715421 0.7875994 A
> 3 -2.310638 0.5455310 A
> 4 -2.685803 10.4116868 A
>
> xyplot(y~x,groups=type,plotdata, auto.key=T)
>
> This shows different colors for different levels of "type".
>
> Now, I want to add a fitted line to the plot, the formula is
> -1.324+0.1117*x-0.0006357*x*x
>
> I tried the following code to do this:
>
> xyplot(y~x,groups=type,plotdata, auto.key=T
> , panel = function(x,y) {
> panel.xyplot(x,y, type='p')
> x<-sort(x)
> panel.lines(x,-1.324+0.1117*x-0.0006357*x*x)
>
> })
>
> Now, it doesn't show different colors for different levels of
"type". How
> can I
> restore that?
>
> Also, is there anyway to put the legend at bottom of the plot (instead at
> the
> top right now)? And is there anyway to print legend horizontally, instead
> of
> vertically as shown right now?
>
> Thanks
>
> John
>
>
>