[R] circular reference lines in splom

Deepayan Sarkar deepayan.sarkar at gmail.com
Fri Jan 28 12:04:24 CET 2011


On Thu, Jan 20, 2011 at 9:45 AM, Max Kuhn <mxkuhn at gmail.com> wrote:
> Hello everyone,
>
> I'm stumped. I'd like to create a scatterplot matrix with circular
> reference lines. Here is an example in 2d:
>
> library(ellipse)
>
> set.seed(1)
> dat <- matrix(rnorm(300), ncol = 3)
> colnames(dat) <- c("X1", "X2", "X3")
> dat <- as.data.frame(dat)
> grps <- factor(rep(letters[1:4], 25))
>
> panel.circ <- function(x, y, ...)
>  {
>    circ1 <- ellipse(diag(rep(1, 2)), t = 1)
>    panel.xyplot(circ1[,1], circ1[,2],
>                 type = "l",
>                 lty = 2)
>    circ2 <- ellipse(diag(rep(1, 2)), t = 2)
>    panel.xyplot(circ2[,1], circ2[,2],
>                 type = "l",
>                 lty = 2)
>    panel.xyplot(x, y)
>  }
>
> xyplot(X2 ~ X1, data = dat,
>       panel = panel.circ,
>       aspect = 1)
>
> I'd like to to the sample with splom, but with groups.

Which would be (without the groups)

splom(~dat, panel = panel.circ)

and that works.


> My latest attempt:
>
> panel.circ2 <- function(x, y, groups, ...)
>  {
>    circ1 <- ellipse(diag(rep(1, 2)), t = 1)
>    panel.xyplot(circ1[,1], circ1[,2],
>                 type = "l",
>                 lty = 2)
>    circ2 <- ellipse(diag(rep(1, 2)), t = 2)
>    panel.xyplot(circ2[,1], circ2[,2],
>                 type = "l",
>                 lty = 2)
>    panel.xyplot(x, y, type = "p", groups)
>  }
>
>
>
> splom(~dat,
>      panel = panel.superpose,
>      panel.groups = panel.circ2)
>
> produces nothing but warnings:

But that's because you don't have groups (in which case
panel.superpose is meaningless). Things work as expected if you
include groups:

splom(~dat, groups = grps,
      panel = panel.superpose, panel.groups = panel.circ)

Of course this does not use different colors for the groups, because
you are not passing on ... to the panel.xyplot() call inside
panel.circ(). Also, it's plotting the same circle over and over for
each group.

Assuming that at some point you want circ1 and circ2 to be
data-dependent and colored accordingly, a reasonable modification is

panel.circ <- function(x, y, ..., type = "p", lty = 1)
 {
   circ1 <- ellipse(diag(rep(1, 2)), t = 1)
   panel.xyplot(circ1[,1], circ1[,2],
                type = "l",
                lty = 2, ...)
   circ2 <- ellipse(diag(rep(1, 2)), t = 2)
   panel.xyplot(circ2[,1], circ2[,2],
                type = "l",
                lty = 2, ...)
   panel.xyplot(x, y, ..., type = type, lty = lty)
 }

splom(~dat, groups = grps,
      panel = panel.superpose, panel.groups = panel.circ)


Or, if you want a common circle as your panel.circ2 seems to suggest,
there is no need to use panel.superpose:

panel.circ2 <- function(x, y, groups, ...)
 {
   circ1 <- ellipse(diag(rep(1, 2)), t = 1)
   panel.xyplot(circ1[,1], circ1[,2],
                type = "l",
                lty = 2)
   circ2 <- ellipse(diag(rep(1, 2)), t = 2)
   panel.xyplot(circ2[,1], circ2[,2],
                type = "l",
                lty = 2)
   panel.xyplot(x, y, groups = groups, ...)
 }

splom(~dat, panel = panel.circ2, groups = grps)

-Deepayan



More information about the R-help mailing list