several hexbin plots on one PDF page in R

て烟熏妆下的殇ゞ 提交于 2019-12-12 15:16:54

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/21110929/several-hexbin-plots-on-one-pdf-page-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!