[R] Lattice graphics adding abline line (1:1 line) ???

Peter Ehlers ehlers at ucalgary.ca
Wed Aug 29 01:29:57 CEST 2012


On 2012-08-27 15:49, iwaite wrote:
> I have read multiple books and looked at many posts online and can't seem to
> figure out how to add a 1:1 line in lattice xyplot. I've tried multiple
> versions of getting this to work, including trying "panel.abline(h=0,v=1),
> panel=function and others.
>
> Second question, how do I get the legend created via "auto.key" to use the
> "pch=c(15,16,17,18) and associated colors, these are circle, square,triangle
> and diamond, but the auto.key just plots points with different default
> colors, not the grey scales and symbols that the actual plots uses, how do I
> get these to match. Is auto.key not the way to go, again I read on many
> different methods, but no luck yet.
>
> Thanks,
>      Ian
>
> xyplot(Inv591$RichTOL~gbm_tol$fitted, data=Inv591,group=EcoRegion, main=
> "Observed vs Predicted for RichTOL (BRT development model)",xlab="Observed
> RichTOL", ylab="Predicted RichTOL", xlim= c(2,8), ylim= c(2,8),
> pch=c(15,16,17,18), cex=1.1,
> col=c('grey10','grey30','grey60','grey80'),cex.lab=1.8, cex.axis=1.8,font=2,
> auto.key=list(corner=c(1,0)),
> panel=function(x,y){
> panel.xyplot(x,y)
> panel.abline(lm(Inv591$RichTOL~gbm_tol$fitted))
>

I don't know what a "1:1 line" is, but your (nonreproducible)
code suggests that you want a regression line (or lines?).
As for auto.key, it may be better to use the "key" argument.

Here are a couple of suggestions (using the iris data)
that may help you:

1. If you just want a single regression line for all groups
combined:

   library(lattice)
   xyplot(Sepal.Length ~ Sepal.Width, data = iris,
     groups = Species,
     pch = 15:17, col = 2:4,
     panel = function(x,y,...){
       panel.xyplot(x,y,...)
       panel.lmline(x,y)},
     key = list(corner = c(1,0),
             text = list(lab = levels(iris[["Species"]])),
             points = list(pch = 15:17, col = 2:4)))

2. If you want separate regression lines for each group:

   xyplot(Sepal.Length ~ Sepal.Width, data=iris,
     groups = Species,
     pch = 15:17, col = 2:4,
     panel = "panel.superpose",
     panel.groups = function(x,y,...){
       panel.xyplot(x,y,...)
       panel.lmline(x,y,...)
     },
     key = list(corner = c(1,0),
             text = list(lab = levels(iris[["Species"]])),
             points = list(pch = 15:17, col = 2:4)))

If you use panel.abline for regression lines, be sure to
specify the regression formula with the "reg=" argument.

Peter Ehlers




More information about the R-help mailing list