Edward Ruchevits
Edward Ruchevits

Reputation: 6696

LaTeX and R bundle?

I am using R to analyse statistical data and plot histograms, scatter plots etc.

And then I have to export all plots as PDFs to manually include them in LaTeX report.

I wonder if there is any way for simplification of this process?

I would be happy to write something like:

\chapter{One}
\begin{r}
    qplot(...)
\end{r}

So that the code between \begin{r} and \end{r} would generate a plot, save it somewhere as PDF and produce TeX like this:

\begin{figure}[ht!]
    \includegraphics[width=1\textwidth,height=1\textheight]{/path/to/plot.pdf}
\end{figure}

Upvotes: 6

Views: 373

Answers (3)

mnel
mnel

Reputation: 115435

What you want is knitr.

The website has lots of examples

within your document you can do something like

<<boring-plots, fig.width=4, fig.height=4, out.width='.4\\linewidth'>>=
## two plots side by side (option fig.show='hold')
par(mar=c(4,4,.1,.1),cex.lab=.95,cex.axis=.9,mgp=c(2,.7,0),tcl=-.3,las=1)
boxplot(x)
hist(x,main='')
@

Or even set it up so your

\begin{r}

\end{r}

syntax would work.

The pdf output of the minimal example from which the example above comes

Upvotes: 6

Yihui Xie
Yihui Xie

Reputation: 30174

See if you can be convinced by the 5-min video on knitr's homepage: http://yihui.name/knitr/ If you only care about LaTeX, start from 2:54.

Your source code would be like this:

\chapter{One}
<<plot, out.width='1\textwidth', out.height='1\textheight', fig.pos='!ht', fig.cap='your caption'>>=
    qplot(...)
@

Upvotes: 10

agstudy
agstudy

Reputation: 121598

Rstudio +knitr are great

http://www.rstudio.com/ide/docs/authoring/overview

Upvotes: 5

Related Questions