Re: [R] lattice: adding text to plots

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Mon 09 Oct 2006 - 13:01:27 GMT

Use panel.superose instead of panel.xyplot. Also you might want to use grid.text instead of panel.text so that you can use npc coordinates (i.e. 0 to 1) making it independent of the values on the panel:

library(lattice)
library(grid)
xyplot(y1 + y2 ~ x | g * h, data = DF, type = "l", auto.key = TRUE,   panel = function(x, y, subscripts, groups, ...) {

     panel.superpose(x, y, subscripts, groups, ...)
     grid.text(DF$f[subscripts[1]], .1, .9)
})

On 10/9/06, Ritwik Sinha <ritwik.sinha@gmail.com> wrote:
> Hi Gabor,
>
> Thanks for your reply. The solution you gave me and the one that I had
> attempted have the same problem. The plots don't differentiate between
> y1 and y2, in other words, they loose the groups attribute. This is
> what 'does not work' means. I am attaching a ps file (I hope that does
> not get stripped). In each panel there should be two lines, but this
> has just one (group lost).
>
> This plot was produced with R 2.4 on windows and lattice 0.14-9.

>
> Thanks and regards,
> Ritwik.
>
> On 10/9/06, Gabor Grothendieck <ggrothendieck@gmail.com> wrote:
> > Could you explain what "does not work" means. It seems to produce a
> > graph with x-y numbers on it in R 2.4.0 on Windows.
> >
> > At any rate, I would have done it like this although I think you can
> > leave off the [1] on subscripts and it will still work.
> >
> > library(lattice)
> > library(grid)
> > xyplot(y1 + y2 ~ x | g * h, data = DF, type = "l",
> > panel = function(x, y, subscripts, groups, ...) {
> > panel.xyplot(x, y, ...)
> > grid.text(DF$f[subscripts[1]], .1, .9)
> > })
> >
> >
> > On 10/9/06, Ritwik Sinha <ritwik.sinha@gmail.com> wrote:
> > > Hi Everyone,
> > >
> > > I know there was a thread recently that dealt with a similar issue,
> > > but this one is a little different.
> > >
> > > I have the following data frame
> > >
> > > DF <- data.frame(x = 1:12, y1 = rnorm(12), y2 = rnorm(12), g =
> > > gl(2,6), h = rep(c("1", "2"), 6), f = c(rep(c("1-1","1-2"),3),
> > > rep(c("2-1","2-2"),3)))
> > >
> > > I essentially want this plot
> > >
> > > xyplot(y1+y2~x|g*h, data=DF, type="l")
> > >
> > > However, now I want to add a different text to each panel, the text
> > > being from column "f" of the data frame. In other words, I want text
> > > "1-1" in the panel where g=1 and h=1 and so on. I tried to pass groups
> > > and subscript to the panel function but could not get what I was
> > > looking for. The following attempt does not work.
> > >
> > > xyplot(y1+y2~x|g*h, data=DF, type="l", auto.key=TRUE,
> > > panel=function(x,y,..., groups, subscripts){panel.xyplot(x,y,...);
> > > panel.text(x=4,y=0, labels=DF$f[subscripts])})
> > >
> > > My R version is 2.2.1 and lattice version is 0.12-11 (sorry they are
> > > not the latest ones, these are on the server).
> > >
> > > --
> > > Ritwik Sinha
> > > Graduate Student
> > > Epidemiology and Biostatistics
> > > Case Western Reserve University
> > > ritwik.sinha_at_gmail.com | +12163682366 | http://darwin.cwru.edu/~rsinha
> > >
> > > ______________________________________________
> > > R-help@stat.math.ethz.ch mailing list
> > > https://stat.ethz.ch/mailman/listinfo/r-help
> > > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> > > and provide commented, minimal, self-contained, reproducible code.
> > >
> >
>
>
> --
> Ritwik Sinha
> Graduate Student
> Epidemiology and Biostatistics
> Case Western Reserve University
> ritwik.sinha_at_gmail.com | +12163682366 | http://darwin.cwru.edu/~rsinha
>
>
>



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Tue Oct 10 03:42:23 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Mon 09 Oct 2006 - 18:30:09 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.