6

Is it possible to "force" R base plots in grid package's grid.newpage? For example, this works fine:

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

R base plot just over-rides grid.newpage. Using par(new=T) does not help either.

Richie Cotton
  • 118,240
  • 47
  • 247
  • 360
Mikko
  • 7,530
  • 8
  • 55
  • 92
  • 4
    @james i have learnt to never say something is impossible in R – Andrie Feb 21 '13 at 10:44
  • 2
    The package [gridbase](http://cran.r-project.org/web/packages/gridBase/index.html) by Paul Murrell, author of grid, allows you to do this. For example, see the [vignette](http://cran.r-project.org/web/packages/gridBase/vignettes/gridBase.pdf) – Andrie Feb 21 '13 at 10:45
  • @Andrie I stand corrected! Though I note that the fundamental differences still impose limitations – James Feb 21 '13 at 10:50
  • this answers your question: http://stackoverflow.com/a/13022137/602276 – Andrie Feb 21 '13 at 10:53
  • `fig=gridFIG()`!!! Thank you Andrie! – Mikko Feb 21 '13 at 11:01
  • @Largh even it is possible to mix the 2 using `gridBase` see [this](http://stackoverflow.com/questions/14124373/combine-base-and-ggplot-graphics-in-r-figure-window/14125565#14125565) for example ..but why do you want to mix the 2..grid has an equivalent of almost all graphics functions.. – agstudy Feb 21 '13 at 14:52
  • @agstudy some packages make `grid` plots and I wanted to plot them in one window: http://stackoverflow.com/questions/15000525/how-to-plot-grid-plots-on-a-same-page. Apparently that is not possible. – Mikko Feb 21 '13 at 16:22
  • @Largh i didn't understand your last link. I am confused. You want to arrange some grid plots in the same page? (I guees the result of `tmPlot`) – agstudy Feb 21 '13 at 16:29
  • @agstudy Yes. I also thought about the possibility of using `grid` package as an all-around plotting solution when placing several figures on one page. – Mikko Feb 21 '13 at 17:23

1 Answers1

7

Because no-one answered this, I'll do it myself. As Andrie said, the answer to this question is here. You'll need gridFIG()function from gridBase package to plot R base plots 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

Community
  • 1
  • 1
Mikko
  • 7,530
  • 8
  • 55
  • 92