On 12/7/07, Aimin Yan <[EMAIL PROTECTED]> wrote:
> I try to make a xyplot like the following:
>
> xyplot(y1+y2~id|groups, ...)
>
> I also want to calculate cor(y1,y2) in each group, print it on each panel.
>
> Does anyone know how to write panel function for this?

This should work:

panel.cor <- function(x, y, groups, subscripts, ...)
{
    require(grid)
    g <- groups[subscripts]
    ug <- unique(g)
    stopifnot(length(ug) == 2)
    y1 <- y[g == ug[1]]
    y2 <- y[g == ug[2]]
    grid.text(label = round(cor(y1, y2), 3),
              x = unit(0.5, "npc"),
              y = unit(0.5, "npc"))
}

xyplot(y1 + y2 ~ id | groups,
       panel = function(...) {
           panel.xyplot(...)
           panel.cor(...)
       })

See ?grid.text for finer control over placement.

-Deepayan

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to