Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reduce pdf file size of plot in R

Tags:

plot

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.

like image 363
alex Avatar asked Dec 15 '11 14:12

alex


2 Answers

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')

like image 143
hadley Avatar answered Sep 28 '22 00:09

hadley


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.

like image 27
John Avatar answered Sep 28 '22 01:09

John