[R] R graphs in LaTeX documents?

Prof Brian D Ripley ripley at stats.ox.ac.uk
Thu Feb 7 13:45:03 CET 2002


On Thu, 7 Feb 2002, Henrik Bengtsson wrote:

> You have to save your plots/figures in EPS format. EPS is basically the
> same as PS (postscript), but it is a one-page document with a
> "BoundingBox".
>
> Example
>
> In [R]:
>
> Alternative 1: Generate the plot *first* and then the eps file
>   ...
>   plot(sin(1:10), pch="+")
>   dev.print(device=postscript, "myFigure.eps",
>             onefile=FALSE, horizontal=FALSE)
>   ...
>
> Alternative 2: Generate the eps file directly *without* opening a window.
>
>   ...
>   postscript(file="myFigure.eps", onefile=FALSE, horizontal=FALSE)
>   plot(sin(1:10), pch="+")
>   dev.off()
>   ...
>
>
> Note that the arguments 'onefile=FALSE, horizontal=FALSE' are important!
> In the help for 'postscript' it also says that you "probably want to set
> ... paper = "special", but I never used this option and it has worked fine
> for me without it (actually I don't know what it does).

Well, you have just asked for an a4 portait plot, and normally I want to
select the plot aspect ratio myself.  With something like

postscript(file="myFigure.eps", onefile=FALSE, horizontal=FALSE, width=8,
height=5)

you get an eps file centered on an a4 page.  With paper = "special"
you get it on an 8" x 5" `paper'.  It may not matter for some pupoes, but
it does for others.

>
> In LaTeX:
>
>   ...
>   Bla bla see Figure~\ref{figMyFigure}.
>
>   \begin{figure}[hbtp]
>     \begin{center}
>       % Scale the figure to have the height 80mm keeping
>       % the width/height ratio.
>       \resizebox{!}{80mm}{\includegraphics{myFigure.eps}}
>     \end{center}
>     \caption{The data used...}
>     \label{figMyFigure} % <-- NOTE: After \caption!
>   \end{figure}
>   ...
>
> This requires that you use "\usepackage{graphics}" or
> "\usepackage{graphicx}" at the top your LaTeX document.
>
> [This answer is a correction of my own answer in the "Re: [R] Latex
> Question" thread from Dec 7, 2001:

Better to use \includegraphics[height=80mm]{myFigure.eps}

>
>   http://www.r-project.org/nocvs/mail/r-help/2001/6836.html
>
> where I forgot the argument 'horizontal=FALSE'.]
>
> Good luck!
>
> Henrik Bengtsson
>
> Dept. of Mathematical Statistics @ Centre for Mathematical Sciences
> Lund Institute of Technology/Lund University, Sweden (+2h UTC)
> Office: P316, +46 46 222 9611 (phone), +46 46 222 4623 (fax)
> h b @ m a t h s . l t h . s e
> http://www.maths.lth.se/matstat/staff/hb/
>
> On Thu, 7 Feb 2002, Robert Lundqvist wrote:
> > I have tried to find a neat way to include graphs from R in LaTeX documents,
> > but have not succeeded (I work with a WinEdt/MikTeX combination). The two
> > roads I have stumbled along so far are the following:
> >
> > *Generate postscript files and convert them into EPS files by means of
> > GhostScript or other not so straightforward tools.
> >
> > *Generate pictex files and include these.
> >
> > None of these solutions have worked so far. I haven't spent so much time
> > trying to find out what goes wrong, but since there might both LaTeX gurus
> > and R wizards among you, I simply thought it would be better to lean on that
> > expertize first. Any suggestions about how to achieve what really should be
> > a simple task?
> >
> > --robert
> > ****************
> > Robert Lundqvist
> > Dept of Quality Technology & Statistics
> > Lulea University of Technology
> > Sweden
> > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> > r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> > Send "info", "help", or "[un]subscribe"
> > (in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
> > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
> >
>
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> Send "info", "help", or "[un]subscribe"
> (in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list