The plot as a raster image in PDF

I am currently working on the results of the CGH array, which include several graphs from tens of thousands of points, and I would like to take advantage of the multi-page function of the PDF device and the ease of the PNG image format.

The problem is that the PDF device saves graphics as vector drawings, so PDF files are huge and take a few minutes. I wonder if R can display as several bitmaps embedded in a single PDF file, since I know the PDF format that can handle it.

Here is a simple example: a PDF file is about 2 Mo, and png is about 10 codes, so I would like to get a PDF file about 20 Ko.

png("test%i.png") plot(rnorm(2e4), rnorm(2e4), pch="+", cex=0.6) plot(rnorm(2e4), rnorm(2e4), pch="+", cex=0.6) dev.off() pdf("test.pdf", onefile=TRUE) plot(rnorm(2e4), rnorm(2e4), pch="+", cex=0.6) plot(rnorm(2e4), rnorm(2e4), pch="+", cex=0.6) dev.off() 
+4
source share
4 answers

Here you will find a solution (50kb) for your desired file size (25kb) without requiring LaTeX installation and / or learning Sweave. (Not that any of them are undesirable in the long run!)

It uses the grid grid.cap() and grid.raster() functions. More information and ideas are in a recent article by Paul Merrell R-Journal (warning: PDF) :

 require(grid) # Make the plots dev.new() # Reducing width and height of this device will produce smaller raster files plot(rnorm(2e4), rnorm(2e4), pch="+", cex=0.6) cap1 <- grid.cap() plot(rnorm(2e4), rnorm(2e4), pch="+", cex=0.6, col="red") cap2 <- grid.cap() dev.off() # Write them to a pdf pdf("test.pdf", onefile=TRUE) grid.raster(cap1) plot.new() grid.raster(cap2) dev.off() 

The resulting pdf images appear to preserve more detail than your test1.png and test2.png , so you can get closer to your goal by reducing their resolution.

+2
source

Use the png driver to create a PNG file of acceptable resolution. Make your plot. Close the png device.

Then use readPNG from the package: png to read it.

Then open the PDF driver, create an empty area without borders and borders in (0,0) (1,1) and draw png with rasterImage. Add extra pages by creating new stories. Close the PDF driver.

This should give you a PDF version with raster versions of the graphs. There are a few tricky bits to set the graphics correctly, and the png resolution is crucial, but I think the above has all the ingredients.

 > png("plot.png") > makeplot(100000) # simple function that plots 100k points > dev.off() X11cairo 2 > plotPNG = readPNG("plot.png") > pdf("plot.pdf") > par(mai=c(0,0,0,0)) > plot(c(0,1),c(0,1),type="n") > rasterImage(plotPNG,0,0,1,1) > dev.off() 

Then check plot.pdf ...

+3
source

To include multiple graphs in your pdf, set onefile = TRUE .

 pdf("test.pdf", onefile = TRUE) plot(1:5) plot(6:10) dev.off() 

To make these graphics, PNG, not native PDF graphics, will take a little more effort. Create all your plots as PNG, for example:

 png("test%01d.png") plot(1:5) plot(6:10) dev.off() 

Then create a LaTeX document containing these PNGs. You can do it from R using Sweave (but how to do it enough to be his own question). There is a decent introductory example here .

+1
source

How about a Sweave solution?

 \documentclass[a4paper]{article} \usepackage[OT1]{fontenc} \usepackage{Sweave} \SweaveOpts{pdf = FALSE, eps = FALSE} \DeclareGraphicsExtensions{.png} \begin{document} \title{Highly imaginative title} \author{romunov} \maketitle <<fig = TRUE, png = TRUE, echo = FALSE>>= plot(1:10, 1:10) @ \end{document} 
0
source

Source: https://habr.com/ru/post/1380151/


All Articles