正如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()
发布于 2012-09-18 19:04:32
我认为这是一个小技巧,但在我的机器上设置onefile=FALSE是有效的:
pdf("test.pdf", onefile=FALSE)
在寻找答案的过程中(我并没有真正找到答案,而是在森林中偶然发现的),我遇到了这个post to Rhelp from Paul Murrell,他承认混合网格和基础图形甚至对大师来说都是令人困惑的。
发布于 2018-04-27 13:08:46
我发现的一种变通方法是在for循环中初始化pdf文件;然后插入一个if子句来评估第一次迭代是否正在运行。当当前迭代是第一次迭代时,继续使用pdf()创建输出设备。将dev.off()放在关闭for循环之后。下面是一个快速示例:
for(i in 1:5){
if (i=1) pdf(file = "test.pdf")
plot(rnorm(50, i, i), main = i)}
dev.off()
https://stackoverflow.com/questions/12481267
复制相似问题