Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding options [keepaspectratio=true, scale = 0.75] to \includegraphics{} in Sweave

Tags:

r

sweave

I have the following R code

<<Q1b1, fig=TRUE>>=  
qqnorm(resid(model1), main=NULL)
@

and I would like to add the option [keepaspectratio=true, scale = 0.75] to the \includegraphics{} call so that the above R code chunk generates

\begin{figure}[ht]
\caption{}
\begin{center}
\includegraphics[keepaspectratio=true, scale = 0.75]{filename.pdf}
\label{fig:1}
\end{center}
\end{figure}

I know you can specify the width and height using \SweaveOpt{width=x, height=y} but I want to specify the scale and have it keep the aspect ratio of the figure when it's generated.

Is there an easy way to do this?

Thanks

like image 908
Mark Clements Avatar asked Mar 10 '11 11:03

Mark Clements


1 Answers

You can set the width for every figure in your Sweave document with \setkeys{Gin} instruction. For example, the following in the preamble of your document makes every figure width 80% of the text width on your page :

\setkeys{Gin}{width=0.8\textwidth}

Otherwise you can add the include=FALSE argument to your code chunk and then create the \includegraphics instruction manually. By default, if your Sweave file is called test.Rnw, then the name of the pdf file generated by the code chunk will be test-Q1b1.pdf. So you can do something like :

<<Q1b1,fig=true,include=false,echo=false>>=  
qqnorm(rnorm(100))
@

\begin{figure}[ht]
\caption{}
\begin{center}
\includegraphics[keepaspectratio=true,scale=0.1]{test-Q1b1.pdf}
\label{fig:1}
\end{center}
\end{figure}

However, I've made some tests on your example but the scale parameter doesn't seem to have any effect on the figure here. Others like angle do, though.

like image 130
juba Avatar answered Oct 13 '22 17:10

juba