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!

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top