[R] Lattice: regression lines within grouped xyplot panels

Deepayan Sarkar deepayan.sarkar at gmail.com
Sat Aug 9 01:05:10 CEST 2008


On Fri, Aug 8, 2008 at 2:38 PM, Rainer Hurling <rhurlin at gwdg.de> wrote:
> Dear community,
>
> I am looking for a possibility to draw 'regression lines' instead of
> 'smooth' lines in grouped xyplots. The following code should give you a
> small example of the data structure.
>
>
> library(lattice)
> data(Gcsemv, package = "mlmRev")
>
> # Creates artificial grouping variable ...
> Gcsemv$Groups <-
>  ifelse(as.numeric(as.character(Gcsemv$school))>65000,
>         "Group1", "Group2")
>
> xyplot(written ~ course | gender, data = Gcsemv,
>       type = c("g", "p", "smooth"),
>       groups = Groups,
>       panel = function(x, y, ...) {
>         panel.xyplot(x, y, ...)
> # Here I want to draw the regression lines
> #         panel.abline(x, y)
>       },
>       auto.key = list(space = 'right'))

Does this do what you want?:

xyplot(written ~ course | gender, data = Gcsemv,
      type = c("g", "p", "r"),
      groups = Groups)

The problem with your approach is that the panel function you define
doesn't deal with groups. An easy workaround is to use
panel.superpose:

xyplot(written ~ course | gender, data = Gcsemv,
      type = c("g", "p"),
      groups = Groups,
      panel = panel.superpose,
      panel.groups = function(x, y, ...) {
        panel.xyplot(x, y, ...)
        panel.lmline(x, y, ...)
      },
      auto.key = list(space = 'right'))

-Deepayan



More information about the R-help mailing list