Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

writing png plots into a pdf file in R

Tags:

plot

r

pdf

png

graphic

I have to create a bunch of graphs with a lot of data points. So far, I've been doing this by plotting all of them into one pdf-file:

pdf("testgraph.pdf")  
par(mfrow=c(3,3))

for (i in 2:length(names(mtcars))){
  plot(mtcars[,1], mtcars[,i])
}

dev.off()

However, with a lot of data points the pdf file becomes too large. As I'm not interested in outstanding quality, I don't care if my plots are vector graphics or not. So I thought of creating the plots as png and subsequently inserting them into a pdf file. Is there a way to do this except of creating R graphs and inserting them into pdf with knitr (which I think is too tedious for such a simple job)?

like image 670
AnjaM Avatar asked Sep 17 '13 14:09

AnjaM


People also ask

How do I make multiple plots into one PDF in R?

To save multiple plots to the same page in the PDF file, we use the par() function to create a grid and then add plots to the grid. In this way, all the plots are saved on the same page of the pdf file. We use the mfrow argument to the par() function to create the desired grid.

How do I Export a graph from R to PDF?

Plots panel –> Export –> Save as Image or Save as PDF Specify files to save your image using a function such as jpeg(), png(), svg() or pdf(). Additional argument indicating the width and the height of the image can be also used.

How do I convert a PNG file to PDF?

It's easy — simply go to Adobe Acrobat online services from any web browser and navigate to the convert JPG to PDF page. Click the Select A File button or drag and drop the image file into the drop zone to upload. You can upload a variety of image types to convert to a PDF, including a: PNG.

How do I Export a plot in R?

(1) The first (and easiest) is to export directly from the RStudio 'Plots' panel, by clicking on Export when the image is plotted. This will give you the option of png or pdf and selecting the directory to which you wish to save it to.


2 Answers

You can

  1. create .png files of each plot
  2. use the png package to read those back in and
  3. plot them in a pdf using grid.arrange
library(png)
library(grid)
library(gridExtra)

thePlots <- lapply (2:length(names(mtcars)), function(i) {
  png("testgraph.png")
  plot(mtcars[,1], mtcars[,i])

  dev.off()
  rasterGrob(readPNG("testgraph.png", native = FALSE),
    interpolate = FALSE)
})

pdf("testgraph.pdf")
do.call(grid.arrange, c(thePlots, ncol = 3))
dev.off()
like image 102
BenBarnes Avatar answered Sep 22 '22 06:09

BenBarnes


If the source of the problem is too many points in the plot then you might want to consider using hexagonal binning instead of a regular scatterplot. You can use the hexbin package from bioconductor or the ggplot2 package has hexagonal binning capabilities as well. Either way you will probably get a more meaningful plot as well as smaller file size when creating a pdf file directly.

like image 21
Greg Snow Avatar answered Sep 23 '22 06:09

Greg Snow