簡體   English   中英

如何在 r 中保存使用循環和 mapplot 包構建的多個圖形?

[英]How to save multiple figures built with a loop and mapplot packages in r?

set.seed(1000);DF=data.frame(Lon=runif(20,-93,-87),Lat=runif(20,27,30),
          Pie1=sample(50, 20),Pie2=sample(50, 20),Pie3=sample(50, 20));head(DF);NGoM<-getNOAA.bathy(-93,-87,28,31, resolution=1,keep=FALSE)
for(i in colnames(DF[,3:5])){
z=DF[,i]
dev.new()
plot(NGoM,col=brewer.pal(n=6,name="Blues"),bg="blue",xaxt="n", yaxt="n",xlab="",ylab="")
map(add=TRUE, col="gray", fill = TRUE)
compassRose(-92, 30.5, rot=0, cex=1)
scalebar(200, xy= c(-90,19.4), type = "bar", divs = 4, cex = 0.7, below = "kilometers", lwd=2, family="Arial")
Ad<-aggregate(list(z=z), list(x=DF$Lon, y=DF$Lat),sum)
draw.bubble(Ad$x, Ad$y, Ad$z,maxradius= 0.25, pch=21, bg=("red"))
legend.bubble(x=-90, y=30.8, z=max(Ad$z), maxradius=0.25, 
          bg="gray78",txt.cex=0.8, pch=21, pt.bg="red")
text(St$Lon,St$Lat,St$Station, family="Arial",cex=0.8, pos=1)
mtext(text="Longitude",side=1, line=2, cex=1)
mtext(text="Latitude",side=2, line=4, cex=1)
degAxis(1, cex.axis=1)
degAxis(2, las=1, cex.axis=1)
box()
ggsave(filename = paste0("C:/Users/xxx/Documents/Fig_", i,.tiff"),plot =last_plot(), device = "png", dpi = 600)

上面提供的代碼是獲取多個地圖的示例;但是,保存的文件是空的。

在不同的網站上查找有關r的功能的信息后,我找到了我的問題的解決方案。

第一步是在循環的第一部分設置圖形選項

png(paste0("Amap",i,".tiff"),width = 1024, height = 768)

第二步也是最后一步是在 box() 之后關閉函數。

dev.off() 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM