Ottorino-Luca Pantani
2009-Oct-30 09:10 UTC
[R] insert a text in panels, always in the same position (lattice, ltext, ?prepanel?)
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
Deepayan Sarkar
2009-Oct-30 10:06 UTC
[R] insert a text in panels, always in the same position (lattice, ltext, ?prepanel?)
On Fri, Oct 30, 2009 at 2:10 AM, Ottorino-Luca Pantani <ottorino-luca.pantani at unifi.it> wrote:> 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.Use the (lower-level) grid function grid.text(), which gives you more flexibility in units. You want something like grid.text(label, x = unit(0.5, "npc"), y = unit(0.5, "npc")) (Of course, a lattice-only solution is also possible; you could also use current.panel.limits() to compute the center of the panel in native coordinates, and then use panel.text().) -Deepayan
Reasonably Related Threads
- How to properly shade the background panels of an xyplot?
- on gsub (simple, but not to me!) sintax
- Again on overlaying plots (a plot region within a plot region)
- on trellis.par.set/get (reproducing figures from Pinheiro & Bates)
- dividing a dataframe column by different constants