[R] panel-dependent distribution in qqmath

Deepayan Sarkar deepayan.sarkar at gmail.com
Wed Oct 11 22:20:23 CEST 2006


On 10/11/06, Benjamin Tyner <btyner at gmail.com> wrote:
> In qqmath, how would one go about having 'distribution' change with
> panel.number? I've tried
>
> set.seed(1)
> mydata <- data.frame(ind = factor(rep(2:4, each = 100)))
> mydata$val <- rt(300, df=rep(2:4, each = 100))
>
>
> plot<-qqmath(~ val | ind,
>              layout=c(3,1),
>              data = mydata,
>              prepanel = function(x, distribution, ...) {
>                 mydist<-function(p) qt(p, df = panel.number() + 1)
>                 prepanel.qqmathline(x, distribution=mydist,...)
>              },
>              panel = function(x, distribution, ...) {
>                 mydist<-function(p) qt(p, df = panel.number() + 1)
>                 panel.qqmathline(x, distribution=mydist,...)
>                 panel.qqmath(x, distribution=mydist,...)
>              })

You have the right idea, but prepanel is called in a context (when the
object is being created) that is fundamentally different from when
panel is called (when the object is being plotted). In particular,
panel.number() makes no sense in the first context. packet.number()
does, but implementation will be non-trivial. Since I don't expect
this to be a very common use, I'm not going to even try, but you can
fake it by using the hack that originally gave rise to the
'panel.number' concept, namely, keep an external counter. One solution
which does not pollute the search path is:

lattice.options(counter = 1)

qqmath(~ val | ind,
       layout=c(3,1),
       data = mydata,
       prepanel = function(x, distribution, ...) {
           counter <- lattice.getOption("counter")
           mydist <- function(p) qt(p, df = counter + 1)
           lattice.options(counter = counter + 1)
           prepanel.qqmathline(x, distribution=mydist,...)
       },
       panel = function(x, distribution, ...) {
           mydist <- function(p) qt(p, df = panel.number() + 1)
           panel.qqmathline(x, distribution=mydist,...)
           panel.qqmath(x, distribution=mydist,...)
       })

-Deepayan



More information about the R-help mailing list