I'm trying to create a PDF with a number of hexbin plots where I'd like to have a particular number of plots per page.

This one works:

PDFPath = "C:\\temp\\some.pdf"
pdf(file=PDFPath)  
par(mfrow = c(2,2))
for (i in seq(5,10))   
{    
  VAR1 = rnorm(i)  
  VAR2 = rnorm(i)  
  plot(VAR1, VAR2)
} 
dev.off() 

This one doesn't work. It only generates one plot per page:

library(hexbin)
PDFPath = "C:\\temp\\some.pdf"
pdf(file = PDFPath)  
par(mfrow = c(2,2))
for (i in seq(5,10))   
{     
  VAR1 = rnorm(i)  
  VAR2 = rnorm(i)  
  plot(hexbinplot(VAR1 ~ VAR2))
} 
dev.off() 

Any ideas on what's going wrong?

EDIT:

I've just noticed that the reason is that mfrow only refers to base graphics and not to grid graphis. Is there a way to achieve similar results for hexbin?

有帮助吗?

解决方案

Use grid.arrange:

library(hexbin)
plotList <- lapply(1:4, function(i) {
  VAR1 = rnorm(i*100)  
  VAR2 = rnorm(i*100)  
  hexbinplot(VAR1 ~ VAR2)
})

library(gridExtra)    
do.call(grid.arrange, c(plotList, ncol=2))

enter image description here

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top