1
votes

I am currently playing with R and LaTeX via Knitr.

Trying to do a very simple slide deck with beamer, I run into a little issue. When I use the \includegraphics method to insert an image, it nicely takes the same width as the text length. However when I use R code to generate a ggplot2 chart, the chart width seems limited to something around 2/3 of the text length (which is very small with information heavy charts). This occurs even with a very wide figure width set up...

Is there a way to remove all additional margins that the R figures seems to add?

The code below reproduce the situation.

\documentclass{beamer} 

\begin{document}
\SweaveOpts{concordance=TRUE}

\begin{frame}[fragile]{A sample slide}

Some text to show text length, Some text to show text length, Some text to show text length, Some text to show text length.

\begin{center}

<<figureExample, echo=False, fig=true, out.width=15>>=
library(ggplot2)
qplot(displ, hwy, data = mpg, colour = factor(cyl)) + 
    theme(plot.background = element_rect(fill='green', colour='red')) +
    theme(plot.margin = unit(c(0,0,0,0), "cm"))
@

\end{center}
\end{frame}

\end{document}

Any help would be super appreciated, I tried everything I could find :( Thanks in advance!

1
Did you find the documentation for knitr?joran
Hi jora, I did and thought at the time that "out.width" would do the tricks but no success. Will look again though. Thanks for mentionning it.xav
Well, you aren't using out.width in your example, and using that chunk option works for me. So you'll have to provide a more detailed example that demonstrates it not working. (Unrelated: you appear to be using a rather old version of ggplot2.)joran
You are right, I shall have used non-deprecated code (corrected). If you have time, could you please check the syntax of the "out.width"? It still doesn't work for me.xav
That's because you still haven't read the documentation: "for LaTeX output, they can be .8\\linewidth, 3in or 8cm". (Not those specific numbers, of course, but those are the formats it should be in.)joran

1 Answers

3
votes

As @joran mentioned in the comments, \SweaveOpts{concordance=TRUE} is not supposed to be there. If RStudio insists on adding it to your document, I'm highly skeptical about your RStudio settings -- it seems you are using Sweave instead of knitr to compile this document (in that case, RStudio does add this if you have checked the concordance option). Go to Tools -> Options -> Sweave, and see if you have changed the default Sweave option to knitr.

Sweave sets the figure width to .8\textwidth globally by default, and that might explain the problem you saw.

Also note echo=False, fig=true are not valid knitr chunk options. You need to use TRUE/FALSE. For out.width, you'd better give it an explicit unit such as in or cm.