Loop保存png文件错误?

问题描述 投票:0回答:1
Models <- list.files(path = "D:/Final_Projections_Global", full.names = TRUE, pattern = ".asc$")
Models <- lapply(Models, raster)
Models_G <- raster::stack(Models)

world  <- readOGR(dsn = "C:/Users/DELL/OneDrive/Documents/",
                  layer = "World_Map_No_Antarctica")

cl <- colorRampPalette(c("#3E49BB","#3498DB", "yellow", "orange", "red", "darkred"))

Names <- c(names(Models_G))

for (i in 1:19) {
  
    plot(Models_G[[i]], col = cl(400))
  
    plot(world, add = TRUE)
  
     png(filename = paste0(Names[[i]], ".png"),
         units = "px",
         pointsize = 8,
         bg = "white",
         width = 1400, height = 913, 
         res =  300, 
         family = "Helvetica",
         dev.off()
         )
}

我一直在尝试将 19 个模型保存为 png 文件在我的工作目录中。但是,不知何故,名称变得杂乱无章,并且最终的(第 19 张)图像没有正确保存。我可以知道解决方案吗?谢谢

r for-loop save png
1个回答
0
投票

我只是按照@Andrew Gustar在评论中建议的修改进行操作,效果非常好。

© www.soinside.com 2019 - 2024. All rights reserved.