[R] npc to native coordinates for xyplot

Deepayan Sarkar deepayan.sarkar at gmail.com
Mon Sep 1 19:39:25 CEST 2008

On 9/1/08, Jason Pare <jay.pare at gmail.com> wrote:
> Hello,
>  I am having trouble adding symbols to grid graphics. I am able to
>  create a lattice scatterplot using xyplot, which has a range from
>  -15:15 in both the X and Y directions. However, when I try to add
>  circles and text to this graph using grid.circle() and grid.text(),
>  they are offset using the default npc coordinates.
>  Nothing happens when I pass the parameter default.units="native" to
>  grid.circle, as suggested in this
>  post:http://tolstoy.newcastle.edu.au/R/e4/help/08/03/5534.html. I also
>  tried replicating the code in Paul Murrell's GridBase documentation,
>  but to no avail:
>  xyplot(y ~ x, groups=pt, data=dt)
>  vps <- baseViewports()
>  pushViewport(vps$inner, vps$figure, vps$plot)
>  grid.circle(x=unit(-6,"native"), y=unit(-8,"native"), r=1)
>  popViewport(3)
>  Is there some other parameter or command that will convert the
>  grid.circle() NPC coordinates to the native coordinates within my
>  xyplot graph, and not the absolute dimensions of the viewport? I am
>  new to creating viewports and grid graphics in general, so any help
>  would be much appreciated.

I'm not sure why you are using baseViewports(); xyplot() uses grid,
not base graphics.

lattice provides means to access (most of) the viewports it creates.
You could do:

xyplot(y ~ x, groups=pt, data=dt)
grid.circle(x=unit(-6,"native"), y=unit(-8,"native"), r=1)

There are other ways to do this too.


More information about the R-help mailing list