[R] lattice: adding text to plots

Ritwik Sinha ritwik.sinha at gmail.com
Tue Oct 10 04:27:20 CEST 2006


On 10/9/06, Deepayan Sarkar <deepayan.sarkar at gmail.com> wrote:
> Just out of curiosity, what's wrong with
>
> xyplot(y1 + y2 ~ x | interaction(g, h, sep = "-"), data = DF, type =
> "l", auto.key = TRUE)

The reason would be that I want to add information to the plot that is
not of the form "i-j". My example happens to be an over
simplification.

> ? Not having to do the other sort of callisthenics is the whole point
> of having strips.
>
> I should point out a subtle effect here (even hinted at in the
> documentation, miraculously), which can be seen with
>
> > xyplot(y1 + y2 ~ x | g * h, data=DF,
> + panel = function(x, y, ..., subscripts) print(subscripts) )
> [1]  1  3  5 13 15 17
> [1]  7  9 11 19 21 23
> [1]  2  4  6 14 16 18
> [1]  8 10 12 20 22 24
>
> Note that the last three values of 'subscripts' in each panel is 12 +
> i, where i is the true subscript (DF has only 12 rows). This is a
> consequence of the fact that DF is internally 'reshape()'d. Gabor's
> solution avoids this by using subscripts[1] in each panel (and also
> because 'x' and 'y' are not recycled to be as long as the labels in
> grid.text), but it's something to be generally aware of.

Thanks for the clarification. I had realized that the internal reshape
makes this change.

>
> -Deepayan
>
> >
> > Thanks and regards,
> > Ritwik.
> >
> > On 10/9/06, Gabor Grothendieck <ggrothendieck at 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 at 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



More information about the R-help mailing list