[R] Problem going back to a viewport with gridBase

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Tue 31 May 2005 - 13:19:09 EST


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)



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Tue May 31 13:23:33 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:32:16 EST