Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embedding ggplot2 output in LaTeX pdf using knitr and RStudio

In the past, I've used RStudio to create ggplot2 and then exported them as PDFs from within RSudio. This works fantastically.

I'm now trying to automate using knitr, but I'm having trouble figuring out where to set the graph height and weight to create high quality output.

Here is my current attempt, but the "side by side" graphs aren't, the rotated landscape graph isn't, and the resolution seems low.

I'd appreciate any advice. It seems that both ggplot2 and knitr are under active development, which is great, however, internet searching has led my astry. LaTeX is new to me. I'd also appreciate any general workflow strategies for this set of tools. Thanks in advance.

\documentclass[letterpaper]{article}
\usepackage{lscape}
\begin{document}
<<load, echo=FALSE, results='hide', warning=FALSE, message=FALSE>>=
require(ggplot2)
@ 

Two on the first page.
<<first, echo=FALSE, fig.height=3, fig.cap="This is first", fig.pos='h'>>=
ggplot(mtcars, aes(mpg, wt))+geom_point()+facet_grid(vs ~ am, margins=TRUE)
@

Blah, blah, blah.
<<second, echo=FALSE, fig.height=3, fig.cap="This is second", fig.pos='h'>>=
ggplot(mtcars, aes(mpg, wt))+geom_point()+facet_grid(vs ~ am, margins=TRUE)
@
\newpage
Second page.

Side by side images:

<<third, echo = FALSE, out.width="2in", fig.cap='Side by side'>>=
ggplot(mtcars, aes(mpg, wt))+geom_point()+facet_grid(vs ~ am, margins=TRUE)
ggplot(mtcars, aes(mpg, wt))+geom_point()+facet_grid(vs ~ am, margins=TRUE)
@
\newpage
\begin{landscape}
This page is rotated
<<fourth, echo = FALSE, out.width="4in", fig.cap='Landscape'>>=
ggplot(mtcars, aes(mpg, wt))+geom_point()+facet_grid(vs ~ am, margins=TRUE)
@
\end{landscape}
\end{document}
like image 713
JIm Avatar asked Dec 28 '12 19:12

JIm


People also ask

How do you use the Knitr in RStudio?

If you are using RStudio, then the “Knit” button (Ctrl+Shift+K) will render the document and display a preview of it.

How do I compile a PDF in RStudio?

Creating a New Document R chunks can also be inserted to interweave R commands and output into your document. To insert an R chunk, use the Chunks menu at the top right of the source editor. You can compile the Sweave document into a PDF using the Compile PDF button on the toolbar.

How do I show plots in R markdown?

In RStudio, when you open a new RMarkdown file, in the editor pane, there is a cogwheel button / menu where you can choose "Chunk Output Inline". That should put the plots into the document.


2 Answers

I can get you most of the way there:

\documentclass[letterpaper]{article}
\usepackage{lscape}
\usepackage{float}
\begin{document}
<<load, echo=FALSE, results='hide', warning=FALSE, message=FALSE>>=
require(ggplot2)
@ 

Two on the first page.
<<first, echo=FALSE, fig.height=3, fig.cap="This is first", fig.pos='h'>>=
ggplot(mtcars, aes(mpg, wt))+geom_point()+facet_grid(vs ~ am, margins=TRUE)
@

Blah, blah, blah.
<<second, echo=FALSE, fig.height=3, fig.cap="This is second", fig.pos='h'>>=
ggplot(mtcars, aes(mpg, wt))+geom_point()+facet_grid(vs ~ am, margins=TRUE)
@

\newpage
Second page.

Side by side images:

\begin{figure}[H]
<<third, echo = FALSE, out.width="0.48\\linewidth",fig.width = 3.5,fig.height=2>>=
ggplot(mtcars, aes(mpg, wt))+geom_point()+facet_grid(vs ~ am, margins=TRUE)
ggplot(mtcars, aes(mpg, wt))+geom_point()+facet_grid(vs ~ am, margins=TRUE)
@
\caption{Side by side}
\end{figure}

\newpage
\begin{landscape}
This page is rotated.
<<fourth, echo = FALSE, fig.width = 4,fig.height = 3,out.width = "0.9\\linewidth">>=
ggplot(mtcars, aes(mpg, wt))+geom_point()+facet_grid(vs ~ am, margins=TRUE)
@
\end{landscape}
\end{document}

The quality looks fine to me, but only if I use my system PDF viewer (Preview, OS X). The built in RStudio PDF viewer has had some rendering issues for me in the past, so I don't use it.

I'm not sure how to force the figure on the landscape page to be below the text. Typically, I'd do that with the float package as with the previous figures, but it doesn't seem to work in landscape. I'd recommend that you consult the folks over at tex.stackexchange.com on that one, since it's fairly LaTeX specific.

Not the interplay between fig.width,fig.height and out.width. Play with both and see what happens to the size of the image versus the scaling of the elements within the image. One influences the actual figure size when it's created, and the other influences how that image is scaled when it is included in the LaTeX document (I think).

Also note that I used the \caption{} in the figure environment for the side-by-sides, since otherwise it will try to create a caption for each figure.

like image 79
joran Avatar answered Sep 21 '22 17:09

joran


Not sure about the rotated fourth page, but getting the side-by-side plots requires fig.show='hold' and out.width='.45\\linewidth'

<<third, echo = FALSE, out.width="2in", fig.cap='Side by side',out.width='.45\\linewidth',fig.show='hold'>>=
ggplot(mtcars, aes(mpg, wt))+geom_point()+facet_grid(vs ~ am, margins=TRUE)
ggplot(mtcars, aes(mpg, wt))+geom_point()+facet_grid(vs ~ am, margins=TRUE)
@
like image 33
Jared Avatar answered Sep 21 '22 17:09

Jared