Skip to content
Prev 175640 / 398503 Next

viewport bug in 2.8.0?: Error: Cannot pop the top-level viewport (grid and graphics output mixed?)

Hi
jonathan.shore at hsbcib.com wrote:
The combination of grid and traditional graphics is an uncomfortable
(unholy?) alliance.  One particularly sensitive point occurs when
starting a new page because traditional graphics tends to think it is
the only game in town and completely wipes the graphics device clean.
In your example above, this means that the first call to plot() removes
all memory of the grid viewports that were pushed before it (this is
what is generating the error message when you resize the window).  A
workaround is possible because grid graphics is a bit more accepting of
others and will notice if there has already been drawing on the graphics
device, so the generally safer approach is to *start* a new page using
traditional graphics, then add grid and other traditional graphics to
the page.  A modification of your example using this strategy is shown
below ...

opar <- par(no.readonly=TRUE, mar = c(2.5, 3.1, 1, 2))
# CHANGE
# Start new page with traditional graphics
plot.new()

pushViewport(viewport(x=0,y=0,width=1,height=1,
                      just=c(0,0), name='base'))
pushViewport (viewport(x=0,y=0.5,width=1,height=0.5,just=c(0,0)))
# CHANGE
par (new=TRUE, fig=gridFIG())

# plot in top half of page
plot (X,Y,col='black',xlab="",main='',cex.axis=.7)

# get toplevel view
seekViewport('base')

# create lower viewport
pushViewport(viewport(x=0,y=0,width=1, height=0.5, just=c(0,0)))
par(new=TRUE,fig=gridFIG())

plot (X,Y,col='grey')

... does something similar work for your "real" code ?

(the instability is another issue, but I don't get that on Linux, so
it's going to be harder to track down)

Paul