Adding options [keepaspectratio=true, scale = 0.75

2019-02-18 10:49发布

问题:

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

回答1:

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.



回答2:

If you do not have \usepackage{Sweave} in your Sweave file preamble, it will be inserted automatically as the last line before the \begin{document} statement. Therefore you need to put

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

, which is actually the default of Sweave style file as specified in \usepackage{Sweave}.

AFTER the \begin{document} statement. See Sweave document (http://www.statistik.lmu.de/~leisch/Sweave/Sweave-manual.pdf) section 4.1.2 for detail.



标签: r sweave