[R] insert a text in panels, always in the same position (lattice, ltext, ?prepanel?)

Ottorino-Luca Pantani ottorino-luca.pantani at unifi.it
Fri Oct 30 10:10:29 CET 2009


Dear R-users,
my present problem is related to lattice.

I would like to put some text in each panel, namely a p-value.
I therefore wrote a simple panel function as reported here below.

I'm able to write the value in each panel at the maximum value of y for 
each panel,
but this obviously overlay the text to the points.

What I'm looking for is to write the text always in the same position 
along the panels,
say in the middle of each panel
something like  "ltext(x = 1.5,  y = (ylim[2] - ylim[1])/2 )"

I tried to play with "ylim" and prepanel but unsuccesfully.

Thank a lot in advance

#################################################################################

df <-
  expand.grid(a = gl(3,1),
              b = gl(2,1),
              c = gl(2,1),
              d = gl(2,1),
              rep = gl(3,1))
df$x <-rnorm(nrow(df))
df$x[c(1, 3, 25, 27, 49, 51)] <- df$x[c(1, 3, 25, 27, 49, 51)] + 5
## modify some values in order to have some significant values (1st and 
3rd panel)
my.panel <- function (x, y, ...)
{
  lm1 <- lm(y ~ x) ## build the model
  p.value.diff <- coef(summary(lm1))[2, 4]## extract the p-value
  panel.fill(col =  ifelse(p.value.diff < 0.05, "lightgrey", 
"transparent")) ## background color as function of p-value
  panel.xyplot(x, y) ## draw the points
  ## insert the p-value in each panel
  ltext(x = 1.1,
        y = max(y),
        ## the above is the problematic point. I tried also y = 
panel.default.xyplot(list(ylim[2])) but without success
        labels = paste("p", as.character(round(p.value.diff, 3)), sep = 
"="))
}
##
xyplot(x ~ d| a + b + c,
       data = df,
       layout = c(6, 2),
       strip = strip.custom(strip.names = TRUE, strip.levels =TRUE),       
       panel = my.panel)

-- 
Ottorino-Luca Pantani, Università di Firenze
Dip. Scienza del Suolo e Nutrizione della Pianta
P.zle Cascine 28 50144 Firenze Italia
Ubuntu 8.04.3 LTS -- GNU Emacs 23.0.60.1 (x86_64-pc-linux-gnu, GTK+ 
Version 2.12.9)
ESS version 5.5 -- R 2.9.2




More information about the R-help mailing list