-2

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.

Community
  • 1
  • 1

1 Answers1

1

abline is for base graphics. panel.abline is the lattice function that you want. You can use it in the panel function to draw the line.

Here's an example from iris:

xyplot(
  Petal.Width ~ Petal.Length,
  data = iris,
  groups = Species,
  panel = function(x, y, ...) {
    panel.xyplot(x, y, ...)
    panel.abline(lm(y~x))
  },
  grid = TRUE,
  auto.key = list(title='Species', space='right')
)

enter image description here

Using your data, here is what is wrong with your function:

  • There is no column site in your data
  • you have a { defining the function argument list rather than (
  • there is not a semicolon or newline between the panel.xyplot and panel.abline calls in your panel function.
  • you probably want groups=s instead of conditioning on s.

Here is working code:

xyplot(y~x,data=D,groups=s,panel=function(x,y,...){panel.xyplot(x,y,...); panel.abline(lm(y~x),col="red")})

enter image description here

Matthew Lundberg
  • 42,009
  • 6
  • 90
  • 112
  • I kept researching and came across panel.abline. However, the line that it makes it completely off. It's like panel.abline's coordinate system and my plot's are not the same. How do I fix this? – Alex Petzke Feb 09 '16 at 04:10
  • @AlexPetzke Provide a reproducible example that shows that it's not working for you. It seems to work fine for Matthew's example so how id your data different. – MrFlick Feb 09 '16 at 04:12
  • The s/site thing and conditioning versus groups was my mistake. The sites had long names I simplified and changed the name. Regarding the {/(: apparently I had stared at code for so long that I stopped really seeing it. R doesn't seem to let me add newlines, but good to know about the semicolon. – Alex Petzke Feb 09 '16 at 16:30
  • Thank you for the help and for putting up with my mistakes and ignorance, much appreciated! – Alex Petzke Feb 09 '16 at 16:31
  • You can use newlines. See my `iris` example. – Matthew Lundberg Feb 09 '16 at 16:36
  • I spoke too soon...and should have included the whole code and not just the parts I didn't know how to do. Please see above...thanks. Same data set applies. – Alex Petzke Feb 09 '16 at 16:51
  • 1
    This doesn't look to be related to the question as it was originally asked, and was answered. I suggest asking a new question for this additional problem. – Matthew Lundberg Feb 09 '16 at 20:16