[R] lattice: plotting an arbitrary number of panels, defining arbitrary groups
Deepayan Sarkar
deepayan.sarkar at gmail.com
Wed Aug 27 00:02:40 CEST 2008
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