Skip to content

circular reference lines in splom

4 messages · Peter Ehlers, Max Kuhn, Deepayan Sarkar

#
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.

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:
Warning messages:
1: In is.na(x) : is.na() applied to non-(list or vector) of type 'NULL'

It does not appear to me that panel.circ2 is even being called.

Thanks,

Max
R version 2.11.1 Patched (2010-09-30 r53356)
Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)

locale:
[1] en_US.UTF-8/en_US.UTF-8/C/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
[1] lattice_0.19-11 ellipse_0.3-5

loaded via a namespace (and not attached):
[1] grid_2.11.1  tools_2.11.1
#
On 2011-01-19 20:15, Max Kuhn wrote:
I don't see a function panel.groups() in lattice.
Does this do what you want or am I missing the point:

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

Peter Ehlers
#
This did the trick:

panel.circ3 <- function(...)
  {
    args <- list(...)
    circ1 <- ellipse(diag(rep(1, 2)), t = 1)
    panel.xyplot(circ1[,1], circ1[,2],
                 type = "l",
                 lty = trellis.par.get("reference.line")$lty,
                 col = trellis.par.get("reference.line")$col,
                 lwd = trellis.par.get("reference.line")$lwd)
    circ2 <- ellipse(diag(rep(1, 2)), t = 2)
    panel.xyplot(circ2[,1], circ2[,2],
                 type = "l",
                 lty = trellis.par.get("reference.line")$lty,
                 col = trellis.par.get("reference.line")$col,
                 lwd = trellis.par.get("reference.line")$lwd)
    panel.xyplot(args$x, args$y,
                 groups = args$groups,
                 subscripts = args$subscripts)
  }


splom(~dat, groups = grps,
      lower.panel = panel.circ3,
      upper.panel = panel.circ3)


Thanks,

Max
On Thu, Jan 20, 2011 at 11:13 AM, Peter Ehlers <ehlers at ucalgary.ca> wrote:

  
    
7 days later
#
On Thu, Jan 20, 2011 at 9:45 AM, Max Kuhn <mxkuhn at gmail.com> wrote:
Which would be (without the groups)

splom(~dat, panel = panel.circ)

and that works.
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