我需要保存多页栅格地图。我使用“trellis.device”来保存它们,但它失败了。有关详细信息,请参阅以下示例。
library(raster)
r1 <- raster(nrow=10, ncol=10)
r1[] <- sample(c(0,1),100,replace=TRUE)
r2 <- raster(nrow=10, ncol=10)
r2[] <- sample(c(0,1),100,replace=TRUE)
sp1 <- stack(r1,r2)
library(rasterVis)
## save as pdf with two pages (It works in this way)
trellis.device(pdf, file="Test.pdf")
levelplot(sp1,col.regions=c("grey","darkgreen"))
levelplot(sp1,col.regions=c("grey","darkgreen"))
dev.off()
## save as pdf (It doesn't work when including a for loop)
trellis.device(pdf, file="Test.pdf")
for (i in 1:2){
levelplot(sp1,col.regions=c("grey","darkgreen"))}
dev.off()
答案 0 :(得分:3)
这列在R FAQ中。默认情况下,R不会像在命令提示符下输入单个值时那样打印循环内返回的值。你需要明确地打印情节。
trellis.device(pdf, file="Test.pdf")
for (i in 1:2){
print(levelplot(sp1,col.regions=c("grey","darkgreen")))}
dev.off()