简体   繁体   中英

R: plot grid objects using par(mfrow)

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

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

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

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM