Skip to content

(x,y) coordinates for grid.text()

6 messages · Hadley Wickham, Erik Iverson, David Afshartous

#
All,

When using grid.text it seems my supplied (x,y) coordinates are being
plotted only in npc (normalized parent coordinates) where (.5,.5) is the
center of the graph.  How do I allow (x,y) to be coordinates corresponding
to the (x,y) values in the graph?   The examples in ?grid.text seem to do
this.  (I think the problem lies in how x and y are defined with respect to
unit ).   Any help much appreciated.  Sample code below.

Cheers,
David




y = c( 0.4,  0.6, -0.1,  0.3,  0.3, -0.2,  0.7,  0.7,  0.2,  0.0,  0.9,
-0.1,  0.6, -1.1,  0.8, -1.0,  0.4,  0.1,  0.7, -0.2, -0.1, -0.1,  2.2,
0.7,  1.1,  0.2, -0.2, -0.9,  0.4,  0.1, -0.3, -0.4)
x = c(4.1000,  4.9600,  1.2000,  3.9000,  3.1875,  1.9000,  1.8625,
0.7650,  1.5750,  2.4700,  1.6250,  1.5500,  2.3125,  1.3125,  1.0600,
-0.5500,  1.1000,  0.0200, -0.0375,  3.4600,  2.5250,  2.0950,  0.8000,
1.6050, -0.4150, -0.7300,  1.1550,  1.4850,  2.2000,  2.2500,  0.6000,
2.1000)
junk.frm = data.frame(ID = rep(1:16, each = 2), x, y, z = rep(c("D", "P"),
16))
xyplot(y ~ x , data = junk.frm[junk.frm$z =="D",], type = c("g", "p",
"smooth"), pch = 20)

grid.text("put text here", x = 1, y = 1.5)  # doesn't show up
grid.text("put text here", x = .4, y = .7) # shows up in the desired
location but this is for npc
#
Have you read the documentation for grid.text?  You need to read the
description of the default.units argument and then read ?unit.

Hadley
#
On Tue, Mar 4, 2008 at 11:18 AM, hadley wickham <h.wickham at gmail.com> wrote:
Oh, and since you're drawing on top of a lattice plot, you'll also
need to read ?trellis.focus

Hadley
#
You want "native" coordinates.  But since you are using lattice (built 
'on top' of grid), you probably need some additional info, like the name 
of the viewport you want to write to I assume(?), but I can't help you 
with that part.
David Afshartous wrote:
#
Okay, I see that default.units is set to "npc" and hence the behavior I
mentioned.  Looking at ?unit, I see the description of various units but it
isn't clear which one I need to select to achieve the result I specified
earlier.  Maybe I'm missing something very basic, but I assume there must be
a simple way to add text to an xyplot() at a specified (x,y) coordinate in
the units of the plotted points?
On 3/4/08 12:18 PM, "hadley wickham" <h.wickham at gmail.com> wrote:

            
#
Thanks, yes "native" is the right one.  The code below now works as desired:

trellis.focus("panel", 1, 1)
grid.text("put text here", x = 1, y = 1.5, default.units = "native")
On 3/4/08 1:44 PM, "Erik Iverson" <iverson at biostat.wisc.edu> wrote: