I'm a newbie to both R and LaTeX and have just recently found how to plot a standard time series graph using R and save it as a png image. What I'm worried about is that saving it as an image and then embedding it into LaTeX is going to scale it and make it look ugly.
Is there a way to make R's plot()
function output a vector graphic and embed that into LaTeX? I'm a total beginner in both so please be gentle :) Code snippets are highly appreciated!
a) Formats of Graphics Supported by LaTeXeps) can be included in a LaTeX document using the graphicx package, i.e., putting \usepackage{graphics} to the beginning of the tex file. Thus, if a student starts to draw figures or graphs, the files should be created in the ps or eps forms.
If that is the case, my favourite solution is the R package “knitr” that allows us to create LaTeX documents directly from R code. Combining the flexibility of a programming language and the LaTeX aesthetics.
R has a number of built-in tools for basic graph types such as histograms, scatter plots, bar charts, boxplots and much more. Rather than going through all of different types, we will focus on plot() , a generic function for plotting x-y data.
I would recommend the tikzDevice
package for producing output for inclusion in LaTeX documents:
http://cran.r-project.org/web/packages/tikzDevice/index.html
The tikzDevice converts graphics produced in R to code that can be interpreted by the LaTeX package tikz
. TikZ provides a very nice vector drawing system for LaTeX. Some good examples of TikZ output are located at:
http://www.texample.net/
The tikzDevice
may be used like any other R graphics device:
require( tikzDevice ) tikz( 'myPlot.tex' ) plot( 1, 1, main = '\\LaTex\\ is $\\int e^{xy}$' ) dev.off()
Note that the backslashes in LaTeX macros must be doubled as R interprets a single backslash as an escape character. To use the plot in a LaTeX document, simply include it:
\include{path/to/myPlot.tex}
The pgfSweave
package contains Sweave
functionality that can handle the above step for you. Make sure that your document contains \usepackage{tikz}
somewhere in the LaTeX preamble.
http://cran.r-project.org/
The advantages of tikz()
function as compared to pdf()
are:
The font of labels and captions in your figures always matches the font used in your LaTeX document. This provides a unified look to your document.
You have all the power of the LaTeX typesetter available for creating mathematical annotation and can use arbitrary LaTeX code in your figure text.
Disadvantages of the tikz()
function are:
It does not scale well to handle plots with lots of components. These are things such as persp()
plots of large matricies. The shear number of graphic elements can cause LaTeX to slow to a crawl or run out of memory.
The package is currently flagged as beta. This means that the interface or functionality of the package is subject to change if the authors find a compelling reason to do so.
I should end this post by disclaiming that I am an author of both the tikzDevice
and pgfSweave
packages so my opinion may be biased. However, I have used both packages to produce several academic reports in the last year and have been very satisfied with the results.
Shane is spot-on, you do want Sweave. Eventually.
As a newbie, you may better off separating task though. For that, do this:
pdf("figures/myfile.pdf", height=6, width=6)
.plot(1:10, type='l', main='boring')
-- and remember that lattice and ggplot need an explicit print
around plot
.dev.off()
to finalize the file.usepackage{graphicx}
in the document header, use\includegraphics[width=0.98\textwidth]{figures/myfile}
to include the figure created earlier and note that file extension is optional.pdflatex
and enjoy.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With