Skip to content

Location of grobs etc on lattice output

4 messages · John Maindonald, Deepayan Sarkar

#
Is there any way, after use of print.trellis(), to obtain the
co-ordinates of the plot region, e.g., in what are then the
native co-ordinates?

e.g.
  library(DAAG)
  library(lattice); library(grid)
  data(cuckoos)
  pushViewport(viewport(layout=grid.layout(2, 1)))
  pushViewport(viewport(layout.pos.row=1))
  cuckoos.strip <- stripplot(species ~ length, data=cuckoos)
  print(cuckoos.strip, newpage=FALSE)
  grid.text("A", x=unit(0.18,"native"), y=unit(0.925,"native"))
   # This works, but is fiddly, and needs rejigging if width
   # or fontsize are changed.
  popViewport(1)

An alternative would of course be to access the co-ordinate
system used by the lattice function for locating the panels,
or for locating labelling.

As in the example above, I have been using grid.text() to
position text outside the plot region, but closer to the "top"
axis than the legend parameter to the lattice function will
allow.

John Maindonald             email: john.maindonald at anu.edu.au
phone : +61 2 (6125)3473    fax  : +61 2(6125)5549
Centre for Bioinformation Science, Room 1194,
John Dedman Mathematical Sciences Building (Building 27)
Australian National University, Canberra ACT 0200.
#
On Saturday 20 November 2004 19:41, John Maindonald wrote:
Have you read help(trellis.focus)? This is new in 2.0.0 and the 
recommended API for interacting with lattice plots (you can of course 
use grid tools directly, but details are more likely to change at that 
level). 

It hasn't had much testing, so I would appreciate reports of things that 
should be doable easily but isn't.
trellis.focus("panel", row=1, column=1, clip.off=TRUE)

will put you in the plot region (panel), but will switch off clipping so 
you can write text outside.

You can also now control the amount of space between the axis and 
legend, see

str(trellis.par.get("layout.heights"))

Deepayan
#
I'm puzzled about side effects of trellis.unfocus():

The following runs without problem, though grid.text() does not
seem to do anything.  (I'd thought that I had it working at one point.)

     library(DAAG); library(lattice); library(grid)
     cuckoos.strip <- stripplot(species ~ length, xlab="", data=cuckoos)
     cuckoos.bw <- bwplot(species~length, xlab="Length of egg (mm)",
                          data=cuckoos)
     vp0 <- viewport(layout=grid.layout(2, 1))
     pushViewport(vp0)
     vp1 <- viewport(layout.pos.row=1)
     vp2 <- viewport(layout.pos.row=2)
     pushViewport(vp1)
     print(cuckoos.strip,newpage=FALSE)
#   trellis.focus("panel", row=1, column=1, clip.off=TRUE)
     grid.text("A", x=unit(0,"native"), y=unit(1.05,"native"),
               gp=gpar(fontsize=9))
#   trellis.unfocus()  ## & remove the following upViewport()
     upViewport()
     pushViewport(vp2)
     print(cuckoos.bw, newpage=FALSE)
     trellis.focus("panel", row=1, column=1, clip.off=TRUE)
     grid.text("B", x=unit(0,"native"), y=unit(1.05,"native"),
               gp=gpar(fontsize=9))
     trellis.unfocus()

If I remove the #'s, and remove the upViewport() that
follows the second #, I seem to lose the current tree,
as though the newpage=FALSE for the next print()
is ignored.  Should I be able to do something like this?
Clearly I do not understand what happens when
trellis.focus() is invoked.

This seems an area where an effective GUI, with a
graphical display of the viewport tree, could be very
helpful.

John Maindonald             email: john.maindonald at anu.edu.au
phone : +61 2 (6125)3473    fax  : +61 2(6125)5549
Centre for Bioinformation Science, Room 1194,
John Dedman Mathematical Sciences Building (Building 27)
Australian National University, Canberra ACT 0200.
On 21 Nov 2004, at 3:41 PM, Deepayan Sarkar wrote:

            
#
On Sunday 21 November 2004 16:35, John Maindonald wrote:
I think you want "npc" rather than "native" here. x=0 on the native 
scale is outside the device area.
This is a bug in trellis.unfocus, caused by my not reading grid 
documentation carefully enough, I didn't notice that upViewport(0) 
jumps to the root viewport instead of going up 0 viewports. I'll post 
an update soon.

Quick fix:

assignInNamespace("trellis.unfocus", ns = "lattice",
                  value = function()
{
    if (lattice:::lattice.getStatus("vp.highlighted"))
    {
        grid.remove("lvp.highlight", warn = FALSE)
        lattice:::lattice.setStatus(vp.highlighted = FALSE)
    }
    lattice:::lattice.setStatus(current.focus.column = 0,
                      current.focus.row = 0)
    if (lattice:::lattice.getStatus("vp.depth") > 0)
        upViewport(lattice:::lattice.getStatus("vp.depth"))
    lattice:::lattice.setStatus(vp.depth = 0)
    invisible()
})
True, but it may be overkill for the amount of use it would get.

Deepayan