TransWikia.com

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

Stack Overflow Asked by Juan Carlos Rubio Polania on December 13, 2020

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)

The code provided above is an example to obtain multiple map plots;however, the files saved are empty.

One Answer

After looking for information about functions of r in different websites, I found the solution of my problem.

The first step is set figure options in the first part of the loop

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

The second and last step is close the function after box().

dev.off() 

Answered by Juan Carlos Rubio Polania on December 13, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP