7

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?

  • I don't know knitr but you don't need to set keepaspectratio unless you specify both width and height, so if knitr lets you just specify the width the aspect ratio will be kept – David Carlisle Oct 12 '14 at 18:35
  • @David Carlisle - From what I have seen so far Knitr defaults the figure size to the entire page. If you try to set just fig.width or fig.height it does not keep the aspect ratio. Thanks for the tip about aspect ratio though, I didn't know that. – JHowIX Oct 12 '14 at 18:43
5

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.