[R] lattice: plotting an arbitrary number of panels, defining arbitrary groups

Alex Karner aakarner at ucdavis.edu
Wed Aug 27 03:56:25 CEST 2008


Thanks Deepayan, works like a charm.

A followup question though--I'd like to produce the same data on four
panels with the final two "zoomed in", i.e. plotted with shorter x and
y axes. Since I can't access panel.number in the prepanel function,
and since updating the plot with lists of the new x and y axis ranges
via xlim and ylim doesn't work (I think since I only actually have one
repeated panel), I've tried a trick you mentioned elsewhere
(http://tolstoy.newcastle.edu.au/R/e2/help/06/10/2621.html)--keeping a
counter in the prepanel function. The following code snippet from
xyplot doesn't work, however:

lattice.options(counter = 1)

...

    scales = list(relation = "free"),
    prepanel = function(...) {
      counter <- lattice.getOption("counter");
      if(counter < 2) {
        list(xlim=range(0,1500), ylim = range(0, 4), dx = NULL, dy = NULL);
        lattice.options(counter = counter + 1) }
      else {
        list(xlim=range(0,500), ylim = range(0, 2), dx = NULL, dy = NULL);
        lattice.options(counter = counter + 1) }
        },

...

Only the first set of limits is respected, and counter equals 2 upon
completion which implies that all of the axes are being drawn at once.
I think I'm misunderstanding something.

Thanks again,
-Alex

On Tue, Aug 26, 2008 at 6:02 PM, Deepayan Sarkar
<deepayan.sarkar at gmail.com> wrote:
>
> On Tue, Aug 26, 2008 at 2:26 PM, Alex Karner <aakarner at ucdavis.edu> wrote:
> > R Friends,
> >
> > I'm running R2.7.1 on Windows XP.
> >
> > I'm trying to get some lattice functionality which I have not seen
> > previously documented--I'd like to plot the exact same data in multiple
> > panels but changing the grouping variable each time so that each panel
> > highlights a different feature of the data set. The following code does
> > exactly that with a simple and fabricated air quality data set.
> >
> > dataSet <- data.frame("Pollutant"=c(rep("Black Carbon",5),rep("PM10",5)),
> > "Detector"=c(1:5,1:5), "Value"=c(seq(50,10,-10),seq(100,60,-10)),
> > "Class"="Mass")
> >
> > xyplot(
> >  Value ~ Detector | Pollutant,
> >  data=dataSet,
> >  aspect = 1.0,
> >  subscripts=TRUE,
> >  panel = function(x,y,subscripts,...) {
> >    if(panel.number() == 1)
> > panel.superpose(x=dataSet$Detector,y=dataSet$Value,1:nrow(dataSet),groups=dataSet$Pollutant);
> >    if(panel.number() == 2)
> > panel.superpose(x=dataSet$Detector,y=dataSet$Value,1:nrow(dataSet),groups=normToEdge_dataSet$Class);
> >    }
> >  )
> >
> > Although the panel labels indicate that only one type of pollutant is
> > displayed in each, I've instead forced all of the data to be plotted in
> > both. The first panel shows two colors, grouped by pollutant, the second
> > shows one color, grouped by class.
> >
> > Here's where the problem comes, if I add an additional pollutant, instead
> > defining the data set as follows:
> >
> > dataSet <- data.frame("Pollutant"=c(rep("Black
> > Carbon",5),rep("PM10",5),"Ultrafines"),
> > "Detector"=c(1:5,1:5,10),"Value"=c(seq(50,10,-10),seq(100,60,-10),75),"Class"=c(rep("Mass",10),"Count"))
> >
> > and rerun the same plotting script, I obtain three panels. The one labeled
> > "Black Carbon" correctly displays all three pollutants in different colors.
> > "PM10" however, displays all classes in one color when there should now be
> > two. Additionally, I now obtain a panel entitled "Ultrafines" which I'd like
> > to suppress.
> >
> > The actual data set has a number of different pollutants, so what I'd
> > ideally like to do is arbitrarily define two panels with different grouping
> > variables. I've tried to set up dummy groups and to condition on those, but
> > with no luck. I think what I need to do is possible with viewports, but is
> > there no way to entice lattice to function in this way?
> >
> > Any help would be appreciated.
>
> Panels can be repeated, using the standard R indexing interface; so,
> for example,
>
> ## trellis object with one panel, but different groups depending on
> panel.number()
>
> p <-
>    with(dataSet,
>         xyplot(Value ~ Detector,
>                group.list = list(Pollutant, Class),
>                aspect = 1.0,
>                subscripts = TRUE,
>                panel = function(..., group.list) {
>                    panel.xyplot(...,
>                                 groups = group.list[[panel.number()]])
>                }))
>
> ## plot first panel twice
>
> p[c(1, 1)]
>
>
> ## add a strip function
>
> update(p[c(1, 1)],
>       strip = function(...) {
>           lab <- c("Pollutant", "Class")[panel.number()]
>           strip.default(1, 1, var.name = "", factor.levels = lab)
>       })
>
> -Deepayan



More information about the R-help mailing list