Search code examples
rplotrstudiosweave

increase image size in Sweave using RStudio


I would like to increase the size of images in a Sweave document so that its width matches that of the text. The only way that is supposed to do that which I could find is using width and height, e.g. <>=, but the only thing this does is decrease the size of the points, axe ticks and labels. The total size of the image stays the same. And the parameter for one R code block seems to affect all other code blocks. I'm using Sweave in RStudio.

\documentclass{article}

\begin{document}
\SweaveOpts{concordance=TRUE}

blablablabla blablabla bla bla blabla blablablabla bla bla bla blablablablablablabla blabla
\begin{figure}[h]
<<bla1, fig=TRUE, echo=FALSE>>=
plot(1:100,1:100, xlab="blabla",ylab="blabla",main="blabla")
@
%\caption{blablabla}
\end{figure}

% \begin{figure}[H]
% <<bla2, fig=TRUE, echo=FALSE, width=30, height=30>>=
% plot(1:100,1:100, xlab="blabla",ylab="blabla",main="blabla")
% @
% \caption{blablabla2}
% \end{figure}
% 
% \begin{figure}[H]
% <<bla3, fig=TRUE, echo=FALSE, width=2, height=2>>=
% plot(1:100,1:100, xlab="blabla",ylab="blabla",main="blabla")
% @
% \caption{blablabla3}
% \end{figure}


\end{document}

Solution

  • I can't replicate this. I'm using RStudio 0.99.489, R version 3.2.2, packageVersion("knitr") is 1.11, using the "Compile PDF" button. I tweaked the file a little bit to take advantage of some of knitr's capabilities (e.g., specifying fig.caption and fig.pos automatically embeds the picture in a figure environment with the appropriate positioning spec ...) I also removed fig=TRUE and your SweaveOpts{} statements since these are obsolete ... and made the text width even wider (50% wider than \textwidth) so the effect would be obvious.

    \documentclass{article}
    
    \begin{document}
    
    blablablabla blablabla bla bla blabla 
     blablablabla bla bla bla blablablablablablabla blabla
    <<bla1, echo=FALSE, fig.pos="h", fig.caption="BLA", out.width="1.5\\textwidth">>=
    plot(1:100,1:100, xlab="blabla",ylab="blabla",main="blabla")
    @
    
    
    \end{document}
    

    enter image description here