2

I have 2 gList objects (grid) that plots fine when I do

grid.draw(plot1)
grid.draw(plot2)

But I want these to be side by side in a pdf. Something like

pdf(test.pdf)
par(mfrow=c(1,2))
plot(1:10)
plot(10:1)
dev.off

But this doesn't work.

zx8754
  • 52,746
  • 12
  • 114
  • 209
monkeyking
  • 6,670
  • 24
  • 61
  • 81

1 Answers1

4

To arrange grid objects , you can use grid.layout within a viewport. here an example.

pushViewport(plotViewport(layout=grid.layout(1, 2),gp=gpar(cex=2)))
pushViewport(plotViewport(layout.pos.col=1))
  grid.draw(getPlot())
popViewport()
pushViewport(plotViewport(layout.pos.col=2, clip="on"))
  grid.draw(getPlot(col.fill='black',col.text='red',text='Rouge',x=0))
popViewport()
popViewport()

enter image description here

here getPlot is a function that retuen a gList;

getPlot <- function(col.fill="red",col.text='black',text="Noir",x=1){
  rect1 <- rectGrob(gp=gpar(fill=col.fill))
  text1 <- textGrob(text,gp=gpar(col=col.text))
  text2 <- textGrob("&", x=x,gp=gpar(col=col.text))
  gList(rect1,text1,text2)
}
agstudy
  • 119,832
  • 17
  • 199
  • 261