[R] wrapping lattice xyplot

Zack Weinberg zackw at panix.com
Fri Jun 8 20:41:24 CEST 2007


This is an expanded version of the question I tried to ask last night
- I thought I had it this morning, but it's still not working and I
just do not understand what is going wrong.

What I am trying to do is write a wrapper for lattice xyplot() that
passes a whole bunch of its secondary arguments, so that I can produce
similarly formatted graphs for several different data sets.  This is
what I've got:

graph <- function (x, data, groups, xlab) {
  g <- eval(substitute(groups), data, parent.frame())

  pg <- function(x, y, group.number, ...) {
    panel.xyplot(x, y, ..., group.number=group.number)
    panel.text(2, unique(y[x==2]),
               levels(g)[group.number],
               pos=4, cex=0.5)
  }

  xyplot(x, data=data, groups=substitute(g),
              type='l',
              ylab=list(cex=1.1, label='Mean RT (ms)'),
              xlab=list(cex=1.1, label=xlab),
              scales=list(
                x=list(alternating=c(1,1), tck=c(1,0)),
                y=list(alternating=c(1,0))
                ),
              panel=panel.superpose,
              panel.groups=pg
              )
}

"pg" is supposed to pick "g" up from the lexical enclosure. I have no
idea whether that actually works, because it never gets that far.  A
typical call to this function looks like so:

> graph(est ~ pro | hemi, sm, obs, "Probe type")

(where 'sm' is a data frame that really does contain all four columns
'est', 'pro', 'hemi', and 'obs', pinky swear) and, as it stands above,
invariably gives me this error:

Error in eval(expr, envir, enclos) : object "est" not found

I tried substitute(x) (as that seems to have cured a similar problem
with "g") but then x is not a formula and method dispatch fails.

Help?
zw



More information about the R-help mailing list