Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

combine multiple pdf plots into one file

Tags:

r

I am trying to merge multiple pdf plots into one master pdf file.

Example:

Input: I have three pdf files: "1.pdf", "2.pdf", and "3.pdf"

Output: How to combine these three plots into one file called "combine.pdf"

I tried using pdf(), and pdftk(), but not successful yet, may be I am missing something simply. Thought to request for help. Greatly appreciate any response.

like image 792
user961932 Avatar asked Aug 05 '13 22:08

user961932


2 Answers

I asked a similar question a while back and Ananda Mahto generously provide time and code to help make a github package that can combine multiple plots of different sizes. I use it in my workflow a bit but have no plans to push it to CRAN but you can download it with the devtools package. Note that you have to have ghostscript installed and on your path for this to work:

## Getting the plotflow github package:

library(devtools)
install_github("plotflow", "trinker")
library(plotflow)

## 2 Examples using the package to merge multiple pdfs

## Example 1
merge_pdf(3, file = "foo.pdf", widths = c(7, 7, 10), heights = c(6, 10, 7))
plot(1:10)
plot(1:10, pch=19)
plot(1:10, col="red", pch=19)

## Example 2
library(ggplot2)
p <- ggplot(mtcars, aes(factor(cyl), mpg)) + geom_boxplot()
merge_pdf(2, file = "bar.pdf", widths = c(7, 10), heights = c(6, 10))
plot(1:10)
print(p)

Note that if you already have the pdfs you may want to look at the plotflow:::mergePDF function.

like image 127
Tyler Rinker Avatar answered Nov 13 '22 18:11

Tyler Rinker


You can use sweave/knitr to get more flexibility and merge easily new plots ,old ones and texts:

\documentclass{article}
\usepackage{pdfpages}
\begin{document}
 this my plot 1:    % write some texts here
\includepdf{1.pdf} 
 this my plot 2:
\includepdf{2.pdf} 
 this my plot 3:
\includepdf{3.pdf} 
 this my plot 4:
\includepdf{4.pdf} 
 a new plot:
<<echo=FALSE>>=         % chunk for new plots
x <- rnorm(100)
hist(x)
@
\end{document}
like image 39
agstudy Avatar answered Nov 13 '22 18:11

agstudy