0

I would like to produce a series of plots in both high-resolution and low-resolution versions, or stated differently using two different file types (.png and .eps). I'd like to know the best/least repetetive way to do this. I am using the gplot function in sna, and the plot has a custom legend outside the plot area. I wrote a function something like this:

library(sna)
plotfun <- function(net){
   png("test.png",width=800)
     p <- gplot(net)
     par(xpd=T)   
     legend(max(p[,1])+1,max(p[,2]),legend=letters[1:10],title="custom legend")
   dev.off()
   seteps()
   postscript(test.eps)
      #repeat all the plotting commands, which are much longer in real life
   dev.off()
}
#try it with some random data
plotfun(rgraph(10))

This is perfectly functional but seems inefficient and clumsy. The more general version of this question is: if for any reason I want to create a plot (including extra layers like my custom legend), store it as an object, and then plot it later, is there a way to do this? Incidentally, this question didn't seem sna specific to me at first, but in trying to reproduce the problem using a similar function with plot, I couldn't get the legend to appear correctly, so this solution to the outside-the-plot-area legend doesn't seem general.

tvg
  • 388
  • 2
  • 13

1 Answers1

0

I would recommend generate graphs only in Postscript/PDF from R and then generate bitmaps (e.g. PNG) from the Postscript/PDF using e.g. ImageMagick with -density parameter (http://www.imagemagick.org/script/command-line-options.php#density) set appropriately to get desired resolution. For example

convert -density 100 -quality 100 picture.pdf picture.png

assuming picture.pdf is 7in-by-7in (R defaults) will give you a 700x700 png picture.

With this approach you will not have to worry that the picture comes out formatted differently depending which R device (pdf() vs png()) is used.

Michał
  • 2,755
  • 1
  • 17
  • 20