[R] Positioning in xyplot

Michael Kubovy kubovy at virginia.edu
Sat Apr 21 16:38:10 CEST 2007


Hi Deepayan,

There appears to be a small problem in the code you created to  
reposition one panel in xyplot: it plots the panels but not the  
contents. Putting together everything that you did on this problem, I  
have the following:

require(lattice)
require(grid)
## this is a safer version of current.panel.limits()
current.limits <-
    function ()
{
    xlim <- convertX(unit(c(0, 1), "npc"), "native", valueOnly = TRUE)
    ylim <- convertY(unit(c(0, 1), "npc"), "native", valueOnly = TRUE)
    if (any(!is.finite(xlim))) xlim <- c(0, 1)
    if (any(!is.finite(ylim))) ylim <- c(0, 1)
    list(xlim = xlim, ylim = ylim)
}
## this calls 'fun' after moving its viewport if panel.number() == 5
callAfterMoving <-
   function(fun, border = TRUE, move.x = 1, ...)
{
   if (panel.number() != 5) {  ## was == 5
       fun(...)
       if (border) grid.rect()
   }
   else {
       cpl <- current.limits()
       pushViewport(viewport(x = move.x,
                             width = unit(1, "npc"),
                             xscale = cpl$xlim,
                             yscale = cpl$ylim,
                             clip = "off"))
       fun(...)
       if (border) grid.rect()
       upViewport()
   }
}
## panel function with axes on the left:
panel.leftaxes <- function(...)
{
    if (panel.number() == 5)
       panel.axis("left", outside = TRUE,
                   line.col = "black")
    panel.xyplot(...)
}

z <- expand.grid(x = 1:10, p = 1:5, r = 1:10)
z$y <- rnorm(nrow(z))
z$p <- factor(z$p, levels = c(1, 5, 2, 4, 3))

xyplot(y ~ x | p, z, groups = r,
       layout = c(2, 3), type = "l",
       par.settings =
       list(axis.line = list(col = "transparent"),
            strip.border = list(col = "transparent")),
       panel = function(...) {
           callAfterMoving(panel.leftaxes, ...)
       },
       strip = function(...) {
           callAfterMoving(strip.default, ...)
       },
       axis = function(..., line.col, side) {
           if (side != "left" || panel.number() != 5) {
               callAfterMoving(axis.default,
                               border = FALSE,
                               line.col = 'black',
                               side = side,
                               ...)
           }
       })


I also wonder why one doesn't need to call panel.xyplot().

Thanks,
MK


_____________________________
Professor Michael Kubovy
University of Virginia
Department of Psychology
USPS:     P.O.Box 400400    Charlottesville, VA 22904-4400
Parcels:    Room 102        Gilmer Hall
         McCormick Road    Charlottesville, VA 22903
Office:    B011    +1-434-982-4729
Lab:        B019    +1-434-982-4751
Fax:        +1-434-982-4766
WWW:    http://www.people.virginia.edu/~mk9y/



More information about the R-help mailing list