[R] exact relative positioning of lattice plots

John G. Bullock john.bullock at yale.edu
Wed Jun 20 05:56:25 CEST 2012


Hello,

I have two lattice plots for which panel.height is fixed.  I am trying to position them in a column so that there is exactly half an inch of vertical space between them.  This would be a simple task if I were not fixing panel.height.  But given that I am, I can't find a way to put exactly half an inch of space between the plots.  Is there a way?  

I've checked the Sarkar and Murrell books, and I've searched the archives of this list, but I haven't found a solution.

My first approach was to set the "split", "par.settings", and "lattice.options" arguments when creating my plots.  But I couldn't get that approach to work.

My second approach was to use grid.layout to specify a three-row layout, with the middle row being exactly .5" high. This lets me print one plot to the top row and the other to the bottom row.  The problem is that I can't vertically justify the placement of the plots within their viewports.  As a result, I get more or less than .5" between the plots, depending on the size of the page.  Here is a small demonstration:

# Load packages
library(grid)
library(lattice)

# Create layout and viewport
masterLayout <- grid.layout(
  nrow    = 3,
  ncol    = 1,
  heights = unit(c(1, .5, 1), c("null", "inches", "null")),
  respect = matrix(c(0, 1, 0)))
vp1 <- viewport(layout.pos.row = 1, name = "vp1", just = c("center", "bottom"))  
vp2 <- viewport(layout.pos.row = 2, name = "spacer")    
vp3 <- viewport(layout.pos.row = 3, name = "vp3", just = c("center", "top"))     

# Create plots
theme.padding <- list(
  layout.heights = list(
    top.padding       = 0,
    main.key.padding  = 0,
    key.axis.padding  = 0,
    axis.xlab.padding = 0,
    key.sub.padding   = 0,
    bottom.padding    = 0))
myPlot <- xyplot(
  1 ~ 1,
  panel        = function () grid.rect(gp = gpar(fill="blue")),
  scales       = list(axs = 'i', draw = FALSE),
  xlab         = NULL,
  ylab         = NULL,
  par.settings = theme.padding)

# Push viewports and print plots
grid.newpage()
pushViewport(
  vpTree(
    viewport(layout = masterLayout, name="master"),
    vpList(vp1, vp2, vp3)))
seekViewport("master")
print(myPlot, draw.in = "vp1", panel.height = list(x=2, units="in"))
print(myPlot, draw.in = "vp3", panel.height = list(x=3, units="in"))
seekViewport("spacer")
grid.rect() 
===

Thank you for any help that you can offer.

John Bullock



More information about the R-help mailing list