Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a table of contents to PDF with R plots

Tags:

r

pdf

I'm creating a larger number of plots in R and save them as PDF (using grDevices / pdf). Is there an easy way to add a (meta-data) table of contents to the PDF as it is created?

I don't mean to add a separate page, but a TOC that PDF viewers like Preview.app display in the sidebar to make navigation easier.)

Example of such a TOC:

Example table of contents

like image 733
Michael Kuhn Avatar asked Oct 13 '11 13:10

Michael Kuhn


2 Answers

The only way I know is with LaTeX, but you don't necessarily need Sweave; perhaps you could just generate the LaTeX file directly with your RPython code. If you've got two pictures which are 6x6 (the default size) named tmp-001.pdf and tmp-002.pdf, this is how you'd make a section called Section A with two subsections for the two pictures.

\documentclass{article}
\usepackage[paperwidth=6in, paperheight=6in, margin=0in]{geometry}
\usepackage{hyperref}
\usepackage{graphicx}
\parindent 0pt
\begin{document}
\pdfbookmark[1]{Section A}{anchorname_aa}

\pdfbookmark[2]{plot aa}{anchorname_aa}
\includegraphics{tmp-001.pdf}

\newpage
\pdfbookmark[2]{plot bb}{anchorname_bb}
\includegraphics{tmp-002.pdf}

\end{document}
like image 186
Aaron left Stack Overflow Avatar answered Oct 15 '22 12:10

Aaron left Stack Overflow


You could create a first page using the names of your objects or plots as the arguments to text in columns.

plot(1:10,1:10, type="n", main ="Index", axes=FALSE,frame.plot=FALSE,xlab="")

text(1,10, "plot")
text(3,10, "page")
text(5,1:10, rev(1:10+1))
text(2,1:10, rev(letters[1:10]))

Base graphics version

I cannot think of a way to generate a navigable TOC, but this option may be more pleasing and easier to integrate with Beamer-type displays. The Hmisc package function latex provides an interface to the Latex longtable package. My Latex-fu is weak but if yours is stronger, you could also divert the dvi code that is created for integration within other applications. I get an intermediate dvi file put in a temporary directory which then opens my dvi viewer and allows saving as pdf:

require(Hmisc)
?latex  # you will probably want to review this
latex(Plot_list<-data.frame(Plots=letters[1:10], Pages=2:11))

enter image description here

like image 33
IRTFM Avatar answered Oct 15 '22 13:10

IRTFM