[Tex/LaTex] Sweave and Knitr Figure Size

knitrsweave

I recently made the switch from using Sweave to Knitr in order to gain Knitr's ability to cache R code chunks. So far its been fairly straightforward except for one thing. I cannot seem to find an intelligent way to size the resulting figures. What I don't want:

  1. To use globally set the figure size as a percentage of the text width as shown here. Some of my figures are entire pages, some are small inserts. Is there a way to do this on a figure by figure basis?

  2. To have to manually set the fig.width and fig.height for every figure. Latex include graphics has a very convenient [width=0.8\textwidth,keepaspectratio=true] option that works really nicely for sizing images without ruining the proportions. Searching through the knitr chunk options I cant find anything similar to the keepaspectratio option. Is there such an option in knitr?

Best Answer

With a review of the knitr options in the documentation http://yihui.name/knitr/options/ the figures can be resized keeping the aspect ration and they can be rotated plus other options.

\documentclass[10pt,letterpaper]{article}
\usepackage{graphicx}
\begin{document}
Build figure in R and display in default size
<<out.width='4in'>>=
x=rnorm(50)
qqnorm(x)
qqline(x)
@
Now 1/2 size
<<out.width='2in'>>=
qqnorm(x)
qqline(x)
@
Now 1/4 size 
<<out.width='1in'>>=
qqnorm(x)
qqline(x)
@
Now rotated at 1/2 size 
<<out.width='2in',out.extra='angle=45'>>=
qqnorm(x)
qqline(x)
@
Back to the original

<<out.width='4in'>>=
x=rnorm(50)
qqnorm(x)
qqline(x)
@
\end{document}

Note that each graphic is resized individually.

enter image description here