How to use basic R graphics in grid.newpage?

Is it possible to β€œforce” the basic R graphs in the grid grid.newpage package? For example, this works great:

 library(grid) grid.newpage() vp1 <- viewport(x=0,y=0.5,width=0.5, height=0.5, just = c("left", "bottom")) vp2 <- viewport(x=0.5,y=0,width=0.5, height=0.5, just = c("left", "bottom")) pushViewport(vp1) grid.rect() grid.text("vp1", 0.5, 0.5) upViewport() pushViewport(vp2) grid.rect() grid.text("vp2", 0.5, 0.5) 

enter image description here .

But if I try something like this:

 grid.newpage() vp1 <- viewport(x=0,y=0.5,width=0.5, height=0.5, just = c("left", "bottom")) vp2 <- viewport(x=0.5,y=0,width=0.5, height=0.5, just = c("left", "bottom")) pushViewport(vp1) grid.rect() print(plot(1,2)) grid.text("vp1", 0.5, 0.5) upViewport() pushViewport(vp2) grid.rect() print(plot(1,2)) 

The R base stretch simply overloads grid.newpage . Using par(new=T) doesn't help either.

+6
source share
1 answer

Because no one answered this, I will do it myself. As Andri said, the answer to this question is here . You will need the gridFIG() function from the gridBase package to plot the R base graphs in plot.new() instead of grid.newpage() :

 library(grid) library(gridBase) plot.new() vp1 <- viewport(x=0,y=0.5,width=0.5, height=0.5, just = c("left", "bottom")) vp2 <- viewport(x=0.5,y=0,width=0.5, height=0.5, just = c("left", "bottom")) pushViewport(vp1) grid.rect() grid.text("vp1", 0.5, 0.5) par(new=TRUE, fig=gridFIG()) plot(1,2) upViewport() pushViewport(vp2) grid.rect() grid.text("vp2", 0.5, 0.5) par(new=TRUE, fig=gridFIG()) plot(1,2) 

enter image description here

+6
source

All Articles