[R] Lattice: regression lines within grouped xyplot panels

Rainer Hurling rhurlin at gwdg.de
Sat Aug 9 09:02:18 CEST 2008


On 09.08.2008 01:05 (UTC+1), Deepayan Sarkar wrote:
> 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?:

Yes, exactly!

> 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:

I knew that I had to look for a panel function dealing with groups, but 
I had no clue how to declare.
> 
> 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'))

Both, type("r") and panel.groups() are fine for my problem. And with 
panel.groups() I am able to write my own group functions, very nice. Now 
I can start analyzing ...  :-)

> -Deepayan

Many thanks, also for your valuable book,
Rainer



More information about the R-help mailing list