Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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:

set.seed(2)
x = sort(runif(100))*10
y = rgamma(100,shape = x*rexp(100),scale=1/exp(rnorm(100)))
plot(x,y)
for (i in 1:200){
y = rgamma(100,shape = x*rexp(100),scale=1/exp(rnorm(100)))
fit = loess(y~x)
points(x,y,col=rgb(0,1,0,.3))
lines(x,predict(fit),col=rgb(1,0,0,.3))
}

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

1 Answer 1

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.