[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
success.

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.

Thanks.


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())
abline(h=1:10, col = "red")  #### THESE DO NOT GET DRAWN AS EXPECTED
popViewport()

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




More information about the R-help mailing list