[R] Computing plot size in Sweave

Marc Schwartz marc_schwartz at me.com
Mon Feb 20 22:34:37 CET 2012


On Feb 20, 2012, at 9:15 AM, BXC (Bendix Carstensen) wrote:

> Sometimes you want to compute the physical size of a plot based on data.
> In R itself this is no problem.
> 
> But is there a way to compute the values of height and width in S-weave, say:
> 
> <<graph,fig=TRUE,height=xx,width=yy>>=
> 
> where xx and yy are computed and not physically written in the document?
> 
> Bendix

Bendix,

By default, Sweave.sty sets:

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

which modifies the default \includegraphics LaTeX command auto-generated during Sweave processing. This means that irrespective of the 'height' and 'width' arguments in the figure chunk header, which do control the size of the PDF/EPS files created, the actual size of the graphic as included in the resultant document will ALWAYS be 80% of the current text width and the height will be scaled accordingly.

In general, if you wanted to control the actual height and width of the figure in the resultant document, you could set:

<<FigureChunkName,include=false,echo=false,fig=true,height=YourHeight,width=YourWidth>>=

Plot Code Here

@

\begin{figure}[tbp]
\centering
\includegraphics{RnwFileName-FigureChunkName}
\caption[LOF Caption]{Some Longer Caption}
\end{figure}

'RnwFileName' is the name of your working .Rnw Sweave file and 'FigureChunkName' is the name of the figure chunk and as a result, the graphic file name being created by Sweave, separated by the hyphen ('-').

By setting the 'include' option to false, Sweave does not auto-generate the \includegraphics line with the "width=0.8\textwidth" argument, and you then explicitly include it in the LaTeX code following the figure chunk. The plot file(s) would then be created with the height and width parameters in the figure chunk header and the resultant document will have a figure of the size you desire, overriding the default behavior.

In my .Rnw files, I actually set:

  \usepackage[nogin]{Sweave}

in my preamble, which overrides the default 'Gin' behavior. Then the height and width parameters in the figure chunks are reflected in the resultant document, but of course, I need to explicitly pre-define those.

If you want to calculate the figure's height and width at run-time, I suspect that the only way to do that would be to have your R code generate all of the LaTeX code output at runtime as well. So something like the following:

<<CodeChunkName,echo=false>>=

# Plot Size Calculations Here
Height <- ResultOfCalcs
Width <- ResultOfCalcs

pdf("MyPlotFileName.pdf", height = Height, width = Width)

Plot Code Here

dev.off()

cat("\\begin{figure}[tbp]\n")
cat("\\centering\n")
cat("\\includegraphics{MyPlotFileName}\n") 
cat("\\caption[LOF Caption]{Some Longer Caption}\n")
cat("\\end{figure}\n")

@


The result of the cat() function calls will be to output the included character vectors to the .tex file being created by Sweave at run-time. So you are using R in a normal code chunk to generate LaTeX code.

If you need an EPS file either in place of the PDF (because you are using postscript stuff like pstricks) or in addition to the PDF, you can replace the pdf() call with postscript() or run a second iteration of the plotting code using postscript()/dev.off() as well.

HTH,

Marc Schwartz



More information about the R-help mailing list