[R] lattice: adding text to plots
Gabor Grothendieck
ggrothendieck at gmail.com
Mon Oct 9 07:18:46 CEST 2006
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
>
> ______________________________________________
> R-help at 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.
>
More information about the R-help
mailing list