Skip to content
Prev 365037 / 398500 Next

[FORGED] Re: [FORGED] lattice: control panel extent on device

Hi

I think your plots are not *quite* horizontally aligned (because of 
differences in the lengths of y-axis labels).  Here is a slight 
modification that messes with the labels (but at least not manually) to 
get things exact ...

valign_lattice <- function(x) {

     if (inherits(x, "trellis")) x <- list(x)

     if (!all(sapply(x, inherits, 'trellis')))
         stop("all elements of x must inherit from trellis class")

     nx <- length(x)
     names(x) <- LETTERS[1:nx]
     h1 <- 1/nx
     y0 <- seq(from = 0, to = 1 - h1, length = nx)
     n <- 1
     grid.newpage()
     pushViewport(viewport(y=y0[n], height=h1, just="bottom"))
     # Force identical widths where we can
     layout.widths <- lattice.options("layout.widths")[[1]]
     layout.widths$ylab <- list(x=1, units="cm", data=NULL)
     layout.widths$panel <- list(x=1, units="null", data=NULL)
     layout.widths$key.right <- list(x=1, units="cm", data=NULL)
     lattice.options(layout.widths=layout.widths)
     # Force (width of) left axis labels to be the same
     yrange <- x[[n]]$y.limits
     yticks <- axisTicks(yrange, FALSE)
     x[[n]] <- update(x[[n]],
                      scales=list(y=list(at=yticks,
                                         labels=rep(" ", length(yticks)))))
     prefix <- LETTERS[n]
     print(x[[n]], newpage=FALSE, prefix=prefix)
     downViewport(paste0(prefix,".panel.1.1.off.vp"))
     # Draw proper left axis labels
     grid.text(yticks, x=unit(0, "npc") - unit(1, "lines"),
               y=unit(yticks, "native"), just="right",
               gp=gpar(cex=.8))
     # Determine width of levelplot panel
     border <- grid.get("border", grep=TRUE)
     width <- convertWidth(border$width, "in", valueOnly=TRUE)
     xscale <- current.viewport()$xscale
     upViewport(0)

     if (nx > 1){
         for (n in 2:nx){
             pushViewport(viewport(y=y0[n], height=h1, just="bottom"))
             # Force identical widths where we can
             layout.widths$ylab <- list(x=1, units="cm", data=NULL)
             layout.widths$panel <- list(x=width, units="in", data=NULL)
             layout.widths$key.right <- list(x=1, units="cm", data=NULL)
             lattice.options(layout.widths=layout.widths)
             x[[n]] <- update(x[[n]], xlim = xscale)
             # Force (width of) left axis labels to be the same
             yrange <- x[[n]]$y.limits
             yticks <- axisTicks(yrange, FALSE)
             x[[n]] <- update(x[[n]],
                              scales=list(y=list(at=yticks,
                                                 labels=rep(" ",
 
length(yticks)))))
             prefix <- LETTERS[n]
             print(x[[n]], newpage=FALSE, prefix=prefix)
             downViewport(paste0(prefix,".panel.1.1.off.vp"))
             # Draw proper left axis labels
             grid.text(yticks, x=unit(0, "npc") - unit(1, "lines"),
                       y=unit(yticks, "native"), just="right",
                       gp=gpar(cex=.8))
             upViewport(0)
         } #n-loop
     }
}

Paul
On 27/10/16 04:21, Ben Tupper wrote:
attached base packages: [1] stats     graphics  grDevices utils