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

Deepayan Sarkar deepayan.sarkar at gmail.com
Fri Oct 30 11:06:18 CET 2009

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().)


More information about the R-help mailing list