[R] aligning column of xyplots and removing space between them

Gabor Grothendieck ggrothendieck at gmail.com
Tue Oct 11 09:43:01 CEST 2005


The code below displays three graphs in three rows and one column but:

1. I want to remove the space between the graphs (I tried playing with position=
arg to print.trellis but it seems quite difficult to get the right
values and all
my attempts had space between them or had overlapping graphs.  Is
there a better way to do this?

2. the widths of the plots are not the same even though I specified the same
xlim= to them all.  How do I make them the same?

3. how do I get rid of the ticks at the top of the bottom plot?

4. the bottom graph is supposed to plot 1:3 against itself but the third
point is not showing even though I specified ylim = c(0,3).  Must
I specify ylim = c(0,3+1) or is there a better way?


Here is the code (its a modified version of some code that I previously
posted regarding a different question):

### everything from here to the grid.newpage line is just
### to set up the viewports for the graphs so you
### can just go to the comment that says
### 'relevant part starts here'

library(grid)
library(lattice)
trellis.par.set(theme = col.whitebg())

grid.newpage()

pushLayout <- function(nr, nc, ..., name="layout") {
  pushViewport(viewport(layout=grid.layout(nr, nc, ...), name=name))
  for (i in 1:nr) {
    for (j in 1:nc) {
      pushViewport(viewport(layout.pos.row=i, layout.pos.col=j))
      upViewport()
    }
  }
  upViewport()
}

with.vpPath <-
with.viewport <- function(data, expr, ...) {
      # if data is a vpPath it cannot be ROOT since NULL will not dispatch here
      depth <- if (data$name == "ROOT") 0 else downViewport(data$name)
      result <- eval.parent(substitute(expr))
      upViewport(depth)
      invisible(result)
}

grid.newpage()

# n and nr are number of cells and rows
n <- nr <- 3
nc <- 1  # must be 1

heights <- unit(c(2, rep(1, nr-1)), "null")
downViewport(pushLayout(nr, nc, heights = heights))

vpt <- current.vpTree(all = FALSE)

### relevant part starts here
#########################

xlab <- main <- function(x) if (x) "v"
for(k in 1:n) with(vpt$children[[k]],
        print( xyplot(v ~ v, list(v = 1:k), xlab = xlab(k == n),
	xlim = c(0,n), ylim = c(0,n), main = main(k == 1),
	scales = list(x = list(draw = k == n), y = list(alternating = 3))),
	newpage = FALSE)
)




More information about the R-help mailing list