title: Speeding up ggplot2-plotting for animated plots in big dataset
body: <p>I built an animated plot using <code>ggplot2</code> and <code>magick</code> from a large dataset containing tenthousands of entries.</p>
  
  <p>Based on my post <a href="https://stackoverflow.com/questions/50697548/how-to-manage-parallel-processing-with-animated-ggplot2-plot/50702959#50702959">How to manage parallel processing with animated ggplot2-plot?</a>, I'm now no longer concerned with speeding up the process for plotting, but the process of saving the plot by using parallel processing through <code>snowfall</code>, since this is most time-consuming part of the code.
  The problem is, saving the plot requires to go through all plots in the list, which makes the code really slow. </p>
  
  <p>Here's a sample code demonstrating my problem:</p>
  
  <pre><code>library(parallel)
  library(snowfall)
  library(ggplot2)
  library(magick)
  
  # creating some sample data for one year
  # 4 categories; each category has a specific value per day
  set.seed(1)
  x &lt;- data.frame(
    rep(as.Date((Sys.Date()-364):Sys.Date(), origin="1970-01-01"),4),
    c(rep("cat01",length.out=365),
      rep("cat02",length.out=365),
      rep("cat03",length.out=365),
      rep("cat04",length.out=365)),
    sample(0:50,365*4, replace=TRUE)
  )
  colnames(x) &lt;- c("date", "category", "value")
  x$category &lt;- factor(x$category)
  
  # creating a cumulative measure making the graphs appear "growing"
  x$cumsum &lt;- NA
  for(i in levels(x$category)){
    x$cumsum[x$category == i] &lt;- cumsum(x$value[x$category == i])
  }
  x &lt;- x[order(x$date),]
  
  # number of cores
  cores &lt;- detectCores()
  
  # clustering
  sfInit(parallel = TRUE, cpus = cores, type = "SOCK")
  
  # splitting data for plotting
  datalist &lt;- split(x, x$date)
  
  # making everything accessible in the cluster
  sfExportAll()
  sfLibrary(ggplot2)
  sfLibrary(magick)
  
  # plotting
  out &lt;- sfLapply(datalist, function(data){
    plot &lt;- ggplot(data)+
      geom_bar(aes(category, cumsum), stat = "identity")+
      # holding breaks and limits constant per plot
      scale_y_continuous(expand = c(0,0), 
                         breaks = seq(0,max(x$cumsum)+500,500), 
                         limits = c(0,max(x$cumsum)+500))+
      ggtitle(data$date)
  plot
  })
  # accessing changed data
  sfExportAll()
  
  # opening magick-device
  img &lt;- image_graph(1000, 700, res = 96)
  
  sfLapply(names(out), function(x){out[[x]]}) 
  
  dev.off()
  
  # animation
  animation &lt;- image_animate(img, fps = 5)
  animation
  
  # closing cluster
  sfRemoveAll()
  sfStop()
  </code></pre>
  
  <p>Any suggestions how to speed up the saving-process?</p>
  
  <p>I'm concerned with the speed of these lines:</p>
  
  <pre><code>img &lt;- image_graph(1000, 700, res = 96) 
  sfLapply(names(out), function(x){print(out[[x]])}) 
  dev.off()
  </code></pre>
  
  <p>Thank you!</p>