By Jochen Voss, on
I am currently in the process of writing a book, and I plan to produce several figures for this project using the statistical computing package R. While R is generally quite good at creating scientific graphics, some adjustments help to create high-quality images for inclusion in another document. This blog entry gives some simple hints about the required steps.
\documentclass{article} \usepackage{graphicx} \begin{document} Some useful information is shown in figure~\ref{figure1}. \begin{figure} \begin{center} \includegraphics{figure1} \end{center} \caption{\label{figure1}This is a figure. It shows useful information.} \end{figure} \end{document}
pdf()
command. The required calls are as follows:
pdf("figure1.pdf", width=4.6, height=3) ...commands to generate the plot invisible(dev.off())
The options width
and height
give the outer
dimensions of the plot (including labels and margins) in inches. The
dev.off()
command at the end is required to make sure all output is written to the
PDF file. I enclose dev.off
inside
invisible()
in order to suppress an annoying message about the "null device" being
printed to the screen.
figure1.R
might contain the R
code required to generate figure1.pdf
. On Linux and MacOS we
can use a
shebang-line to
turn the R-script into a stand-alone program. This requires two steps:
First, we have to add a line like the following at the start of the R
script:
#! /usr/bin/env Rscript pdf("figure1.pdf", width=4.6, height=3) ...commands to generate the plot invisible(dev.off())
And secondly, we need to make the script executable by using (in a
terminal window) a command like
.
After these steps, chmod +x figure1.R
figure1.R
can simply be executed to
regenerate figure1.pdf
. This particularly useful for use in
Makefiles to automatically regenerate the figure every time the R script
is changed.
This is an excerpt from Jochen's blog.
Newer entry: neat trick!
Older entry: Wisent release 0.6.2
Copyright © 2012 Jochen Voss. All content on this website (including text, pictures, and any other original works), unless otherwise noted, is licensed under the CC BY-SA 4.0 license.