[R] lattice chart: different definitions for series

Deepayan Sarkar deepayan.sarkar at gmail.com
Tue Nov 8 19:19:09 CET 2005


On 11/8/05, ManuelPerera-Chang at fmc-ag.com <ManuelPerera-Chang at fmc-ag.com> wrote:
>
>
>
>
> Dear Deepayan,
>
> Thank you very much for the example.
>
> Now I am missing the grids inside my graph. As shown in my sample code
> yesterday, before I had them defined in the
>
> panel=function(x,y,...){panel.grid(h=-1,v=-1,col="grey",lty=2,cex=0.1)}
>
> and they were properly drawn, but you did not use this approach in your
> example, and my guesses this morning to place them together with your code
> failed.
>
> Sorry for my newbee question ..

Starting from my example, and going one step at a time:

0. My example was

xyplot(mct ~ year, mydf, groups = clinic,
       panel = panel.superpose.2,
       type = c("b", "b", "p"))

Since this works, it means that when 'panel.superpose.2' is eventually
called, it gets all the arguments it needs (the details are not
important).

1. You want a new panel function that adds something to the existing
one. So as a first try, let's reproduce the above plot with an
explicit panel function.

xyplot(mct ~ year, mydf, groups = clinic,
       panel = function(...) {
           panel.superpose.2(...)
       },
       type = c("b", "b", "p"))

This new panel function just calls panel.superpose.2, passing on all
its arguments. If you start  worrying about what those arguments are,
things will get very confusing very soon, so don't.

3. You need to add a grid, so modify your panel function accordingly.

xyplot(mct ~ year, mydf, groups = clinic,
       panel = function(...) {
           panel.grid(h = -1, v = -1)
           panel.superpose.2(...)
       },
       type = c("b", "b", "p"))

And, you're done. There's another alternative described in
?panel.superpose.2 that avoids writing a panel function, but I'll skip
that here.


4. For the record, suppose you needed to do something new in your
panel function that actually used the x, y, groups and subscripts
arguments. That's simple too, just do

xyplot(mct ~ year, mydf, groups = clinic,
       panel = function(x, y = NULL, subscripts, groups, ...) {
           ## do things using x, y, etc
           print(length(groups[subscripts]))
           panel.grid(h = -1, v = -1)
           panel.superpose.2(x = x, y = y,
                             subscripts = subscripts,
                             groups = groups, ...)
       },
       type = c("b", "b", "p"))



> My best regards,
>
> Manuel
>
> PS.: As for my "experiments", I was trying to use the document "Panel
> Function for Display Marked by groups", that you wrote. In this document I
> cannot see that e.g. all the arguments for panel.superpose.2 are optionals,
> as in your example below.

Hmm. If you mean that the graphical parameters have defaults, then
yes, that's a bit confusing. The defaults are left out in the
documentation because they are long, but you can see them using

args(panel.superpose.2)

> Further in this documentation you explained:
>
> "panel.groups - the panel function to be used for each group of points.
> Defaults to panel.xyplot (behavior in S) "
>
> and then ..
>
> "type -  usually a character vector specifying what should be drawn for
> each group, passed on to the panel.groups function, which must know what to
> do with it. By default this is panel.xyplot ..."
>
> consecuently I was trying to use "type" as an argument to panel.groups, but
> without success.
>
>                       Deepayan Sarkar
>
>                       <deepayan.sarkar@        To:
> ManuelPerera-Chang at fmc-ag.com
>
>                       gmail.com>               cc:
> r-help at stat.math.ethz.ch
>
>                                                Subject:  Re: lattice chart:
> different definitions for series
>                       07.11.2005 21:23
>
>
>
>
>
>
>
> On 11/7/05, ManuelPerera-Chang at fmc-ag.com <ManuelPerera-Chang at fmc-ag.com>
> wrote:
> >
> >
> >
> >
> > Hi enthusiasts,
> >
> > Trying to create a single chart in  lattice with different plotting
> > definitions for the different series (two series should be drawn with
> lines
> > and the other without them)
> >
> > I am using a dataset, which includes a grouping variable e.g. clinic with
> > three levels, the variable "year" and a continous variable: "mct".
> >
> > In the graph the variable "year" is in the x axis, with "mct" represented
> > in the y axis.
> >
> > The diagram should include two line diagrams(representing two of the
> > groups) , with the third group represented only with symbols(no lines).
> >
> > Until now I was using white lines to eliminate the lines drawn in the
> third
> > group, but this solution is not optimal, as the grids are sometimes not
> > visible
> >
> > sp<-list(superpose.symbol=list(pch=c(1,2,1),col=c("blue","red","green")),
> >        superpose.line=list(col=c("blue","red","white"),lty=c(1,2,)))
> >
> > ... and then including
> >
> > print(xyplot(mct~trend.data$year,groups=clinic,
> >   scales=list(x=list(at=c(15:pno),labels=per.labels)),
> >   main=main.title,
> >   sub=sub.title,
> >   xlab=x.label,
> >   ylab=y.label,
> >   xlim=c(pno-12,pno+1),
> >   panel=function(x,y,...){panel.grid(h=-1,v=-1,col="grey",lty=2,cex=0.1);
> >                   panel.superpose(x,y,type="l",lwd=1.8,...);
> >                   panel.superpose(x,y,type="p",cex=1.8,...))},
> >   key=sk,
> >   par.settings=sp));
> >
> > ... was also experimenting, and searching a lot in the WWW for
> >
> > panel.superpose.2 and type=c("b","b","p"), but without success.
>
> I don't know what experiments you did, but the following seems to work
> fine for me:
>
> library(lattice)
>
> mydf <-
>     data.frame(year = rep(1991:2000, 3),
>                mct =
>                rnorm(30,
>                      mean = rep(1:3, each = 10),
>                      sd = 0.5),
>                clinic = gl(3, 10))
>
> xyplot(mct ~ year, mydf, groups = clinic,
>        panel = panel.superpose.2,
>        type = c("b", "b", "p"))
>
> -Deepayan
>
>
>
>




More information about the R-help mailing list