AnswerBun.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!

Related Questions

Change null to 0 in response

1  Asked on December 3, 2021 by edwin-landsfield

   

Print complete SQL for all queries made by objection.js

2  Asked on December 3, 2021 by eugene-kim

 

Easiest way to convert a char* of hex to actual hex in C?

1  Asked on December 3, 2021 by purplespark

 

Xcode confused with IQAir Api Parsing

1  Asked on December 3, 2021 by unkowncoder

       

gRPC server cannot be built and throws error

0  Asked on December 3, 2021 by jibo_libin

   

How to correctly map huge json file to Java (pojo)?

1  Asked on December 3, 2021 by orkhan-hasanli

     

check all items in csv column except one [python pandas]

2  Asked on December 2, 2021 by blindside044

       

Unpivot a single into two rows – T-SQL

1  Asked on December 2, 2021 by error-1004

       

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP