[R] R graphs in LaTeX documents?

Henrik Bengtsson hb at maths.lth.se
Thu Feb 7 13:30:15 CET 2002


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).

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:

  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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list