alex
alex

Reputation: 833

Reduce pdf file size of plot in R

i am plotting some data in R using the following commands:

jj = ts(read.table("overlap.txt"))
pdf(file = "plot.pdf")
plot(jj, ylab="", main="")
dev.off()

The result looks like this:

enter image description here

The problem I have is that the pdf file that I get is quite big (25Mb). Is the a way to reduce the file size? JPEG is not an option because I need a vector graphic.

Upvotes: 11

Views: 15783

Answers (5)

hadley
hadley

Reputation: 103948

Take a look at tools::compactPDF - you need to have either qpdf or ghostscript installed, but it can make a huge difference to pdf file size.

If reading a PDF file from disk, there are 3 options for GostScript quality (gs_quality), as indicated in the R help file:

  • printer (300dpi)
  • ebook (150dpi)
  • screen (72dpi)

The default is none. For example to convert all PDFs in folder mypdfs/ to ebook quality, use the command

tools::compactPDF('mypdfs/', gs_quality='ebook')

Upvotes: 12

John
John

Reputation: 23768

You're drawing a LOT of lines or points. Vector image formats such as pdf, ps, eps, svg, etc. maintain logical information about all of those points, lines, or other items that increase complexity, which translates to size and drawing time, as the number of points increases. Generally vector images are the best in a number of ways, most compact, scale best, and highest quality reproduction. But, if the number of graphical elements becomes very large then it's often best to go to a raster image format such as png. When you switch to raster it's best to have a good idea what size image you want, both in pixels and also in things like print measurements, in order to produce the best image.

For information from the other direction, too large a raster image, see this answer.

Upvotes: 7

Yihui Xie
Yihui Xie

Reputation: 30194

Which version of R are you using? In R 2.14.0, pdf() has an argument compress to support compression. I'm not sure how much it can help you, but there are also other tools to compress PDF files such as Pdftk and qpdf. I have two wrappers for them in the animation package, but you may want to use command line directly.

Upvotes: 4

djq
djq

Reputation: 15298

One way of reducing the file size is to reduce the number of values that you have. Assuming you have a dataframe called df:

# take sample of data from dataframe
sampleNo = 10000
sampleData <- df[sample(nrow(df), sampleNo), ]

I think the only other alternative within R is to produce a non-vector. Outside of R you could use Acrobat Professional (which is not free) to optimize the pdf. This can reduce the file size enormously.

Upvotes: 4

Spacedman
Spacedman

Reputation: 94317

Hard to tell without seeing what the plot looks like - post a screenshot?

I suspect its a lot of very detailed lines and most of the information probably isn't visible - lots of things overlapping or very very small detail. Try thinning your data in one dimension or another. I doubt you'll lose visible information.

Upvotes: 1

Related Questions