0

I'm trying to plot two figures onto one PDF, using this code:

ncols <- 1
nrows <- 2
grid.newpage()
pushViewport(viewport(layout = grid.layout(nrows, ncols)))
for (i in seq_along(chroms)) {
pushViewport(viewport(layout.pos.col = 1, layout.pos.row = 1))
plotTracks(list(itrack, gtrack, dTrack),
    chromosome = chr1, add = TRUE)
pushViewport(viewport(layout.pos.col = 1, layout.pos.row = 2))
plotTracks(list(biomTrack, gtrack2),
    chromosome = chr1, add = TRUE)
popViewport(1)
}

The problem is, the two figures overlap! (And the row2 of the figure remains empty)

I'm hoping fresh eyes will catch my bug. What am I doing wrong?

EDITED TO REFLECT COMMENT FROM @DINRE

ncols <- 1
nrows <- 2
grid.newpage()
pushViewport(viewport(layout = grid.layout(nrows,
+     ncols)))
for (i in seq_along(chroms)) {
pushViewport(viewport(layout.pos.col = 1, layout.pos.row = 1))
plotTracks(list(itrack, gtrack, dTrack),
    chromosome = chr1, add = TRUE)
**popViewport(1)** ## NEED TO POP OUT FIRST ROW BEFORE PLOTTING SECOND.
pushViewport(viewport(layout.pos.col = 1, layout.pos.row = 2))
plotTracks(list(biomTrack, gtrack2),
    chromosome = chr1, add = TRUE)
popViewport(1)
}
mfk534
  • 719
  • 1
  • 9
  • 21
  • Please make your situation reproducible, i.e. provide us with the data and the code needed to mimic your situation. See http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example for more tips on how to do this. – Paul Hiemstra Apr 01 '13 at 19:16
  • Is there a particular reason why you are using the `grid.layout` function? You can just as easily specify the exact dimensions, locations, and justifications for the viewports. If you want one in the first row and another in the second row, use 'npc' dimensions with something like this: `pushViewport(viewport(x=0,y=1, height=0.5, width=1, just=c("left","top")))` and `pushViewport(viewport(x=0,y=0, height=0.5, width=1, just=c("left","bottom")))` – Dinre Apr 01 '13 at 19:16

1 Answers1

2

The bottom row's viewport is being pushed inside of the top row's viewport. Popping the top row's viewport first seems to fix the problem.

Note: This is a really common mistake to make with viewports, so don't feel bad if it happens to you.

Dinre
  • 4,196
  • 17
  • 26