0

I would like to plot grid objects and base R objects on the same png. The package I am using (meta) uses the grid graphics system.

I would like to use mfrow for this if possible, as it is what I am using elsewhere.

If I plot:

png("test.png",width=297,height=210,units="mm",res=300)
par(mfrow=c(2,2))
plot(1,1)
plot(1,1)
plot(1,1)
plot(1,1)
dev.off()

Everything works fine.

However if I plot:

library(meta)
m <- metaprop(4:1, c(10, 20, 30, 40))
png("test.png",width=297,height=210,units="mm",res=300)
par(mfrow=c(2,2))
plot(1,1)
forest(m,new=F)
plot(1,1)
forest(m,new=F)
dev.off()

Things aren't right as the forest plots try to take up the whole page rather than being restricted to their corners.

Thanks in advance for your help

person
  • 96
  • 12

2 Answers2

0

I bet it behaves similar to heatmap() plots, see here.

Possible example for a solution from the post (didn't test):

--snip // Citation (Dr Paul Murrell)--

library(gridGraphics) 
grid.newpage() 
pushViewport(viewport(0, .5, .5, .5, just=c("left", "bottom"))) 
grid.echo(function() { heatmap(test) }, newpage=FALSE) 
popViewport() 
pushViewport(viewport(.5, 0, .5, .5, just=c("left", "bottom"))) 
grid.echo(function() { heatmap(test) }, newpage=FALSE) 
popViewport() 

--snip--

Dharman
  • 30,962
  • 25
  • 85
  • 135
Andre Wildberg
  • 12,344
  • 3
  • 12
  • 29
0

Modifying this answer, you can combine them into a plot, but I think the margins and settings for forest() needs to be optimized so that the titles etc can be seen properly:

library(gridBase)
library(gridExtra)

layout(matrix(c(1,2),nrow=1),widths=c(1,3))
par(mar=c(2.5,2.5,2.5,2.5))

plot(1,1)

plot.new()
vps <- baseViewports()
pushViewport(vps$figure)
vp1 <-plotViewport()
forest(m,new=FALSE)
popViewport()

enter image description here

StupidWolf
  • 45,075
  • 17
  • 40
  • 72