Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm using R to generate graphics with many transparent layers. I want the output as a pdf, but filesizes explode when there are so many transparent layers. I can manually open each one in GIMP and flatten it there, but I'd like to do this via a system() call in R, so that the process can be automated. Does anybody know of a way to do this?

Here is a silly reproducible example:

x = sort(runif(100))*10
y = rgamma(100,shape = x*rexp(100),scale=1/exp(rnorm(100)))
for (i in 1:200){
y = rgamma(100,shape = x*rexp(100),scale=1/exp(rnorm(100)))
fit = loess(y~x)

Now, I want to be able to do dev.copy2pdf(file="dumb_graph.pdf"), and then do system(something) to flatten the image. Or most likely, a number of different system calls. Does anyone know of any utilities that will do this?

Edit: I'm looking for linux-specific solutions, but I guess that solutions on other platforms would be useful posts for others who may search for this one day.

Edit2: the output should be vector.

Edit3: This is migrated from SO

share|improve this question

Are you sure of your diagnosis that the PDF file is large because of the presence of "many transparent layers"?

It seems to me it could be the sheer number of objects, in your example you have 200x100 pairs of coordinates, PDF is 1.2 MB, so you are complaining that each pair of coordinate is using up 60 B.

At some stage and for some target resolution (differing for screen or print, e.g.), there is a cross-over between the efficiency of storing vector graphics and bitmap graphics. You could thus export a PNG rather than PDF in the present case.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .