6

I'm drawing several ggplot2 objects and placing them on a grid.arrange inside a call to a 'pdf' device. I've found that the PDF performs about a billion times better (generates faster, renders faster) if I rasterize the plots first. So inside a parallel dlply loop, I'm using ggsave to write the ggplot2 as a PNG, then using readPNG to read it back in and rasterGrob to convert return it to the dlply. The dlply puts it into a list of grobs which grid.arrange then draws to the PDF device.

Some of this seems unwieldy, so in general, is there a better approach? But what really bugs me is writing the PNGs to disk when all I do with them is read them back in. Is there a way to save a grob directly to a rasterGrob?

plot.list <- dlply( ... {
        ggsave(filename= fname
               ,plot= my.plot
               ,device= "png" 
               ,scale = 1, width= 1.1, height= 2.125, units = "in" 
               ,dpi = dpi)

        # return it as a list of rasters
        rasterGrob(readPNG( source= fname, info= TRUE))
}
RHA
  • 3,677
  • 4
  • 25
  • 48
woodvi
  • 1,898
  • 21
  • 27
  • 1
    Hmm. Doesn't look easy. This may be helpful: http://stackoverflow.com/questions/7171523/in-r-how-to-plot-into-a-memory-buffer-instead-of-a-file . You would use the Cairo graphics device directly rather than going though `ggsave` – MrFlick Apr 28 '16 at 20:13
  • I sympathise with the question (I've always wanted a way to rasterise a specific plot layer), but if you're rasterising the whole image I'm not sure what you gain by making a pdf as opposed to a png? – baptiste Apr 28 '16 at 21:00

1 Answers1

0

I ended up using the Cairo graphics device as outlined in @Yang 's In R, how to plot into a memory buffer instead of a file? answer suggested by @MrFlick

woodvi
  • 1,898
  • 21
  • 27