kurdtc
kurdtc

Reputation: 1621

R: a faster way to export plots as .pngs with a lot of data?

I am plotting a lot of data in R in a loop and export the plots to png in 300dpi resolution using png(). With all my data I am creating about 2000 plots in the end. This takes about 15 minutes to execute. When exporting to postscript using postscript() it takes about 20 seconds to process the whole data. The approximate file size for a resulting .png is about 300KB and for a .ps it is about 5KB

Is anyone aware of a faster png export method than this? Thank you for your suggestions.

# Plot NAME and ID as filename
for(i in 1:length(ind)){
  png(names(ind[i]), width=3358, height=2329, res=300)
  # if postscript; uncomment following line 
  # postscript(names(ind[i]))
  par(mar=c(6,8,6,5))
  plot(ind[[i]][,c('YEAR','VALUE')],
       type='b',
       main=ind[[i]][1,'NAME'],
       xlab="Time [Years]", 
       ylab="Value [mm]")

  dev.off() 
}

Upvotes: 4

Views: 969

Answers (1)

Berry Boessenkool
Berry Boessenkool

Reputation: 1538

So a reproduicible example would be:

dir.create("DummyPlots")
setwd("DummyPlots")

system.time( for(i in 1:500)
  {  png(paste0("Image", i, ".png")) ; plot( i ) ; dev.off()  })
# 7.5 s

Here's a way that's a little faster:

system.time(  {png("FastImage%03d.png")
for(i in 1:500)
plot( i )
dev.off()  })
# 5.2 s

setwd("..")
unlink("DummyPlots", recursive=TRUE)

Upvotes: 2

Related Questions