I asked about this previously (see How to symbolize groups separately in a graph (R)), but could not get the suggested code to work. I would like to know if I can do what I need in xyplot
. I used this code:
xyplot(y~x,groups=site,type=c("p","r"))
to try to get a regression line, but it gave me a line through each group separately. I want to symbolize the groups differently, but they compose a single data set that I want to add the line to.
I looked at Plotting xyplot with regression line on lattice graphics, but I can't get the panel functions to work (this may very well be my lack of experience with R).
Side question related to panel functions: How do I insert line breaks in my code, and are they necessary (including putting the curly braces on different lines than the functions)? Do the different panel functions need to be separated in some particular way?
Edit: Will abline
work? It doesn't give any errors when I apply it after plotting my points in xyplot
, but doesn't graph a line either.
Data set:
s x y
COB 0 2
COB 0 6
COB 0 4
COB 0 3
COB 0 3
COB 2 7
COB 2 8
COB 2 4
COB 2 13
COB 2 9
JP 9 9
JP 9 9
JP 9 14
JP 14 20
JP 14 18
JP 14 19
Code:
xyplot(y~x|site,data=D,panel=function{x,y,...){panel.xyplot(x,y,...)panel.abline(lm(y~x),col="red")})
Error I receive:
Error: unexpected '{' in "xyplot(y~x|site,data=D,panel=function{"
New code:
xyplot(y~x,groups=site,auto.key=list(columns=nlevels(Data$s)),panel=function(x,y,...){panel.xyplot(x,y,...);panel.abline(lm(y~x),col="red")},xlab="Years burned",ylab="Species per plot",main="Years burned vs. number of species",par.settings=list(superpose.symbol=list(pch=15,cex=2,col=c("blue,"green")))
Error:
Error: unexpected symbol in "s=nlevels(Data$s)),panel=function(x,y,...){panel.xyplot(x,y,...);panel.abline(lm(y~x),col="red")},xlab="Years burned",ylab="Species per plot",main="Years burned vs. number of species",par.sett"
Thanks.