Skip to content

Problem going back to a viewport with gridBase

1 message · Paul Murrell

#
Hi
Gabor Grothendieck wrote:
The base, or "traditional", graphics system only records the *current* 
plotting coordinates;  it does not retain a memory of previous plotting 
coordinates.  What your example does is *reposition* the plotting 
region, but to do what you want you would have to recreate the plotting 
coordinates of the first plot.  This is possible (at least in simple 
cases like the above), as shown below.  However, perhaps a better 
approach would be to use a combination of grid and lattice plots, where 
the coordinate systems are retained and don't need to be recreated.  An 
example of this approach is given at the end.

#######
# Modified example using gridBase
#######
library(gridBase)

opar <- par(no.readonly = TRUE)
grid.newpage()

# two columns, one row
unit. <- unit(c(1,1), c("null","null"))
pushViewport(viewport(layout = grid.layout(1, 2, widths = unit.)))

# draw green graph in first column (viewport A)
pushViewport(viewport(layout.pos.col = 1, name = "A"))
par(fig = gridFIG()); par(new = TRUE)
plot(1:10, col = "green", pch = 20)
upViewport(1)

# draw purple graph in second column (viewport B)
pushViewport(viewport(layout.pos.col = 2, name = "B"))
par(fig = gridFIG()); par(new = TRUE)
plot(1:100, col = "purple", pch = 18)
upViewport()

# go back to A and add horizontal grid lines
seekViewport("A")
par(fig = gridFIG()); par(new=TRUE)  #### extra par(new=TRUE)
plot(1:10, type="n", axes=FALSE, ann=FALSE)  #### RESET PLOT A AXES
abline(h=1:10, col = "red")
popViewport()

# go back to B and add vertical grid lines
seekViewport("B")
par(fig = gridFIG()); par(new=TRUE)  #### extra par(new=TRUE)
plot(1:100, type="n", axes=FALSE, ann=FALSE)  #### RESET PLOT B AXES
abline(v=1:10, col = "red")
popViewport()
par(opar)


#######
# Similar result but using grid and lattice
#######
library(grid)
library(lattice)

grid.newpage()

# two columns, one row
unit. <- unit(c(1,1), c("null","null"))
pushViewport(viewport(layout = grid.layout(1, 2, widths = unit.)))

# draw green graph in first column (viewport A)
pushViewport(viewport(layout.pos.col = 1, name = "A"))
# lattice plot instead of base plot
p1 <- xyplot(1:10 ~ 1:10, col="green", pch=20)
# prefix important so I can refer to it later
print(p1, newpage=FALSE, prefix="plotA")
upViewport(1)

# draw purple graph in second column (viewport B)
pushViewport(viewport(layout.pos.col = 2, name = "B"))
p2 <- xyplot(1:100 ~ 1:100, col="purple", pch=18)
print(p2, newpage=FALSE, prefix="plotB")
upViewport()

# go back to A and add horizontal grid lines
seekViewport(trellis.vpname("panel", 1, 1, prefix="plotA"))
# I'm working on a grid.abline() ...
grid.segments(x0=0, x1=1,
               y0=unit(1:10, "native"),
               y1=unit(1:10, "native"),
               gp=gpar(col="red"))

# go back to B and add vertical grid lines
seekViewport(trellis.vpname("panel", 1, 1, prefix="plotB"))
grid.segments(y0=0, y1=1,
               x0=unit(1:10, "native"),
               x1=unit(1:10, "native"),
               gp=gpar(col="red"))

upViewport(0)

Paul