[Tex/LaTex] Knitr plotting outside margin

floatsknitrmarginsplot

I like the look of plots that extend beyond the margins

\documentclass{article}
\usepackage{lipsum}
\usepackage[demo]{graphicx}

\begin{document}
  \begin{figure}
    \centering
    \makebox[\textwidth]{\includegraphics[width=0.9\paperwidth]{}}
  \end{figure}
  \lipsum[1-5]

\end{document}

What options do I need to specify in a knitr chunk to achieve this effect? The option fig.width appears to only change the aspect ratio.

Best Answer

Use the chunk option out.width=".9\\paperwidth".


Update:

knitr does not have the full power of LaTeX (that is too much to do in pure R). If you want more control over LaTeX, you can consider the chunk option fig.show='hide' or fig.show='hold' and hard-code the LaTeX part later, e.g.

\documentclass{article}
\usepackage{lipsum}

\begin{document}
<<test-plot, fig.height=3, fig.show='hide'>>=
par(mar=c(4,4,.1,.1)); plot(1:10)
@

\begin{figure}
  \centering
  \makebox[\textwidth]{\includegraphics[width=0.9\paperwidth]{figure/test-plot}}
\end{figure}

\lipsum[1-5]

\end{document}

There is a disadvantage in this approach, which is the figure path is hard-coded instead of dynamically generated from the chunk label, so you will have to be very careful to synchronize these two places.

Related Question