如解释 here ,感谢 gridBase
,可以轻松地将图嵌入到现有图中,即使两个图都使用 R 的基本图形系统。但是,将整个图形保存为 pdf 时,第一页始终为空白。如何防止这种情况?
下面是一个例子:
require(gridBase)
## generate dummy data
set.seed(1859)
x <- 1:100
y <- x + rnorm(100, sd=5)
ols <- lm(y ~ x)
pdf("test.pdf")
## draw the first plot
plot.new() # blank page also happens when using grid.newpage()
pushViewport(viewport())
plot(x, y)
## draw the second plot, embedded into the first one
pushViewport(viewport(x=.75,y=.35,width=.2,height=.2,just=c("center","center")))
par(plt=gridPLT(), new=TRUE)
hist(ols$residuals, main="", xlab="", ylab="")
popViewport(2)
dev.off()
最佳答案
我认为这有点黑客但设置 onefile=FALSE 在我的机器上工作:
pdf("test.pdf", onefile=FALSE)
在寻找答案(我在森林中偶然发现的并没有真正找到)时,我遇到了这个 post to Rhelp from Paul Murrell谁承认混合网格和基础图形即使对大师来说也令人困惑。
关于r - 在R中,使用gridBase在绘图中嵌入子图时如何防止pdf中出现空白页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12481267/