[R] Problem going back to a viewport with gridBase

Gabor Grothendieck ggrothendieck at gmail.com
Tue May 31 05:19:09 CEST 2005

I am setting up base plots -- one in viewport A and and one in B.  This part
works fine.  But if I go back to A after having done B and add
horizontal lines it seems
to not use the correct coordinates.  How do I tell it to resume using A's
coordinates?  I am already using par(fig = gridFIG()) but it seems that that's
not enough to reestablish them.  What happens is that when I go back to 
A it draws the horizontal lines as if its relative to B's coordinates
rather than
restablishing A's coordinates.  As a result the horizontal lines are
drawn near the
bottom of the graph instead of at the correct heights.  Try running the code
below to see what I mean.

I have also tried to use baseViewports with this but did not have any

How do I modify this example so that the horizontal red lines come out
at the appropriate levels?    Note that this is just an example and in 
the future I will want to have multiple viewports each with a base plot and
add arbitrary additional line or point plots to them so the solution needs
to be sufficiently general that I can so generalize it.



opar <- par(no.readonly = TRUE)

# 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)

# 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)

# go back to A and add horizontal grid lines
par(fig = gridFIG())
abline(h=1:10, col = "red")  #### THESE DO NOT GET DRAWN AS EXPECTED

# go back to B and add vertical grid lines
par(fig = gridFIG())
abline(v=1:10, col = "red")

More information about the R-help mailing list