Skip to content

R graphs in LaTeX documents?

14 messages · Robert Lundqvist, Brian Ripley, Detlef Steuer +8 more

#
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
On Thu, 7 Feb 2002, Robert Lundqvist wrote:

            
Generate .eps directly by using dev.copy2eps or from the menu on the
graphics window or by following the instructions in ?postscript.
No conversion should be needed.
That driver does work but is minimally-featured.  Not recommended.
It really is a simple task.  Use dev.copy2eps and then the graphicx
package in latex.  You give us no clue as to your problems, so it is
difficult to give any advice.
#
Dear Robert,

You can easily include R-generated pictures in Latex.
One easy way is to use the postscript() command and including
the picures in Latex using \usepackage{graphics} .

It?s as easy as \includegraphics{mygraphicsfile.ps} and you can even do 
size customizing in Latex using some commands in this package. (Good
documentation included.)

Hope this helps,
detlef
On 07-Feb-2002 Robert Lundqvist wrote:
Detlef Steuer --- http://fawn.unibw-hamburg.de/steuer.html

***** Encrypted mail preferred *****
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
On Thu, 7 Feb 2002, Robert Lundqvist wrote:

            
I use LyX (GUI front-end for LaTeX) under linux. For me I just save from R
a postscript file, then in Lyx click "Insert Figure" and stick it in. So
It's pretty automatic for me and have never had problems over 4 years of
use.

LyX (www.lyx.org) runs under windows.

Bill

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
In general it is not difficult inserti .eps graphics in LaTeX
For instance:
1)Do any graphic with postscript() or export it by File|Save as| postscript:

2) in your document put for instance
\usepackage{graphicx}
....
\includegraphics*[width=\textwidth]{graph.eps}

cheers,
vito

----- Original Message -----
From: "Robert Lundqvist" <Robert.Lundqvist at ies.luth.se>
To: "'r-help at lists.R-project.org'" <r-help at stat.math.ethz.ch>
Sent: Thursday, February 07, 2002 9:11 AM
Subject: [R] R graphs in LaTeX documents?
documents,
that
be
-.-.-
http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._.
_._
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
since you are a windows-user: did you consider producing pdf? you don't only get files 
you can print as good as postscript but also the advantage of an easy-to-share file-
format.

for me (using TeXnicCenter as Latex-shell for windows) this solution was the easiest 
one! at the end of ths message you'll find a latex-file which shows how to include an 
pdf-image (in R produced with file->save as->pdf)

also, i highly recommend having a look to TEXNICCENTER! nicer than any other 
latex-shell for windows and also free.
(http://www.toolscenter.org/products/texniccenter/) 

cu
till

%## begin of latex-file
\documentclass[a4paper,12pt]{report}
\usepackage[latin1]{inputenc}

\usepackage[pdftex]{graphicx}

\begin{document}

\DeclareGraphicsExtensions{.jpg,.pdf,.mps,.png}

\pdfpkresolution=1200
\pdfimageresolution=1200

\immediate
Here is your R-Graphic:

\pdfximage width 12cm {map.pdf}
\pdfrefximage 1


\end{document}
%## end of latex-file
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
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:
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
[Sorry, I forgot the dev.copy2eps alternative and to make the answer
complete so this answer could be refered to when others ask the same
question again I also added the 'paper="special' argument (as dev.copy2eps
do)]

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.copy2eps("myFigure.eps")
  ...

Alternative 2: Generate the plot *first* and then the eps file
               (basically the same result as the above)
  ...
  plot(sin(1:10), pch="+")
  dev.print(device=postscript, "myFigure.eps", 
            onefile=FALSE, horizontal=FALSE, paper="special")
  ...

Alternative 3: Generate the eps file directly *without* opening
               a plotting window first.
  ...
  postscript(file="myFigure.eps",
             onefile=FALSE, horizontal=FALSE, paper="special")
  plot(sin(1:10), pch="+")
  dev.off()
  ...

Note that the arguments 'onefile=FALSE, horizontal=FALSE' (and
'paper="special"') are important!

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:
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
On Thu, 7 Feb 2002, Henrik Bengtsson wrote:

            
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.
Better to use \includegraphics[height=80mm]{myFigure.eps}

  
    
#
--- Robert Lundqvist <Robert.Lundqvist at ies.luth.se>
wrote:
Hi Robert,

Perhaps your difficulties lie on the LaTeX side of the
equation. I've used R versions 1.23, 1.31 and now
1.41; gone thru 'mass production' of numerous
postscript graphics using both the menu and coding;
and pulled those graphics into LaTeX (and
Word)directly without a hitch--EXCEPT IN THE
BEGINNING. And those problems were
first-time-using-LaTeX-related.

Assuming I had saved the R graphic as 'ipu238.eps' the
following (edited) has worked for me:


\documentclass[10pt,letterpaper]{report}
...
\usepackage{graphicx}
...

\begin{document}

...as can be seen in Figure \ref{fig:ipu238}. The
confidence bands are wide and ...

\begin{figure}
	\begin{center}
		\includegraphics[scale=0.9]{ipu238}
	\end{center}
  \caption{Intervals for plutonium 238 versus europium
154 using all samples.}
	\label{fig:ipu238}
\end{figure}

...

\end{document}


FYI, I use TeXnicCenter/MikTeX for the LaTeX. They
work very well together.(Note: LyX on windows is
cygwin based and requires an Xserver, e.g. XFree86.
Might as run Linux to begin with! Hmmm, not a bad
idea, but RWin is getting pretty sweet.) 

Regards,
Mike

__________________________________________________



-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
The following codes come from R example. I save its
graph as rpart.ps, but the graph did not display the
whole word, e.g. just display "bsent", not "absent" in
the most left. can you advise me how to solve it?

many thanks,

p.s, file rpart.ps is also attached.

library(rpart)
data(kyphosis)
fit <- rpart(Kyphosis ~ Age + Number + Start, data =
kyphosis)
fit2 <- rpart(Kyphosis ~ Age + Number + Start, data =
kyphosis, 
parms = list(prior = c(0.65, 0.35), split =
"information"))
postscript("rpart.ps", width=960, height=960)
par(mfrow = c(1, 2))
plot(fit)
text(fit, use.n = TRUE)
plot(fit2)
text(fit2, use.n = TRUE)
dev.off()

__________________________________________________

Everything you'll ever need on one web page
from News and Sport to Email and Music Charts
-------------- next part --------------
A non-text attachment was scrubbed...
Name: rpart.ps
Type: application/postscript
Size: 4116 bytes
Desc: rpart.ps
Url : https://stat.ethz.ch/pipermail/r-help/attachments/20020208/e8b9fbfe/rpart.ps
#
Hi,

Try to enlarge the width and height in postscript command ("rpart.ps",
width=..., height=...)

Vlad
dechao wang wrote:

            
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
1 day later
#
Hi
The text is being clipped to the plot boundaries.  The following minor
adjustment to your code shows where the clipping is occurring [indicated by
a dashed box around the plot boundary] and a possible fix [via par(xpd)]:

library(rpart)
data(kyphosis)
fit <- rpart(Kyphosis ~ Age + Number + Start, data =
kyphosis)
fit2 <- rpart(Kyphosis ~ Age + Number + Start, data =
kyphosis,
parms = list(prior = c(0.65, 0.35), split =
"information"))
postscript("rpart.ps", width=960, height=960)
par(mfrow = c(1, 2))
par(xpd=NA)  # or par(xpd=T)
plot(fit)
text(fit, use.n = TRUE)
box("plot", lty="dashed")
plot(fit2)
text(fit2, use.n = TRUE)
box("plot", lty="dashed")
dev.off()

Paul



-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
1 day later
#
Hi Paul,

Many thanks for your help. It is very clear
expalnation.

Dechao
 --- Paul Murrell <p.murrell at auckland.ac.nz> wrote: >
Hi
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._ 

__________________________________________________

Everything you'll ever need on one web page
from News and Sport to Email and Music Charts

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