0

I have the following chunk of script for creating a scatter plot with an lm line and an lm line through the origin:

xyplot(nbsp~FRic, groups = Ecoregion,data=dataplot, xlab="Functional Richness", ylab="Species Richness",
       panel=function(x, y, groups) {
         panel.xyplot(x, y)
         panel.abline(lm(y~x), col='blue')
         panel.abline(lm(y~x+0), col='red')
       },
       type='l',
       auto.key = list(title='Ecoregion', space='right')
)

Which gets me a correct plot, but all the points are the same colour and don't match up to the groups represented in the legend. (Sorry I don't have enough reputation points to post pictures) However, I need the colours of the points to match up to the legend, and whatever I do I cannot get them to.

Any and all help would be greatly appreciated. I've rewritten this chunk more times than I can count, and I'm sure it's a silly error.

Thanks, Katherine

John Paul
  • 12,196
  • 6
  • 55
  • 75
KatMartin
  • 1
  • 1

1 Answers1

0

I think this is what you want. Be careful with your function arguments.

xyplot(nbsp~FRic, groups = Ecoregion,
       data=dataplot, xlab="Functional Richness", 
       ylab="Species Richness",
       panel=function(x, y, groups=groups,...) {
         panel.xyplot(x, y, groups=groups,...)
         panel.abline(lm(y~x), col='blue')
         panel.abline(lm(y~x+0), col='red')
       },
       type='l',
       auto.key = list(title='Ecoregion', space='right')
)
kdauria
  • 6,300
  • 4
  • 34
  • 53