[R] issues with Sweave and inclusion of graphics in a document

Prof Brian Ripley ripley at stats.ox.ac.uk
Sun Aug 20 08:10:52 CEST 2006

savePlot is just an internal version of dev.copy, part of the support for
the menus on the windows() graphics device.

It is described in An Introduction to R' (the most basic R manual).

On Sat, 19 Aug 2006, Thomas Harte wrote:

> the problem is a little hard to explain; the .Rnw files (below)
> probably do a better job, but here goes ...
>
> Sweave doesn't like it when i size a graphical device in a code
> chunk using either, e.g.:
>
> 	windows(width=20, height=5)
>
> in Windows, or, e.g.
>
> 	x11(width=20, height=5)
>
> under X, when i then plot something in said device and try to
> include this graphical output in the resulting document.
>
> Sweave does not object to my writing code chunks in the above
> manner, so long as i do not wish to include the code in a LaTeX
> figure environment.
>
> oftentimes i want to do precisely what Sweave doesn't appear
> to allow. for example, with time-series data, i want to see a
> wide window on the screen as i code, and then i want to include
> the graphical output in my document the way that i fine tuned
> it on the screen. i don't want to write two pieces of code:
> the first, to view output on the sceen; the second, to save
> the output to a .pdf file for inclusion in the document.
>
> some example .Rnw files should illustrate my plight.
> suggestions on a workaround (i.e. how to do what i describe in
> linux/X) welcome.
>
>
> % >>>>>>>>>>>>>>>>>>>>>>>>>>>>>> example-windows.Rnw
> \documentclass[a4paper]{article}
>
> \begin{document}
>
> \noindent This is an example of what I can do on Windows. Unhappily, I seem to be
> able to squeeze marginally more out of \texttt{Sweave} \emph{chez\/} Bill Gates
> than I can under Linux. Ho, hum.
>
> <<echo=false,results=hide>>=
> 	# create a simple AR process:
> 	make.ar.1<- function(alpha=1,n=300) {
> 		Z<- rnorm(n);
> 		Y<- numeric(n);
> 		Y[1]<- Z[1];
> 		for (i in 2:n) Y[i]<- alpha*Y[i-1]+Z[i];
> 		return(Y)
> 	}
> @
>
> <<label=ar.1>>=
> 	# a long AR process is best viewed in a wide window:
> 	windows(width=20, height=5)
> 	sp<- make.ar.1(alpha=.5, n=800)
> 	plot(sp, type="l", col="blue")
> 	# WISIWIS: What I See Is What I Save ;)
> 	savePlot("ar",type="pdf")
> @
> \begin{figure}
> \begin{center}
> % 	imporantly, by saving the plot i have direct control over graphics in LaTeX,
> % 	and i can fine-tune the the graphics placement as much as i want:
> 	\includegraphics[width=14.5cm]{./ar.pdf}
> \caption{An AR(1) process of length~\protect\Sexpr{length(sp)}
> is best viewed in a wide window.}
> \end{center}
> \end{figure}
>
>
> \noindent Had I tried to do the following, \texttt{Sweave} would have blown up!
> \begin{verbatim}
> 	<<label=ar.1>>=
> 		windows(width=20, height=5) 	# <- this is the offending command:
> 		sp<- make.ar.1(alpha=.5, n=800)
> 		plot(sp, type="l", col="blue")
> 	@
> 	\begin{figure}
> 	\begin{center}
> 	<<fig=true>>=
> 	<<ar.1>>
> 	@
> 	\caption{An AR(1) process of length~\protect\Sexpr{length(sp)}
> 	is best viewed in a wide window.}
> 	\end{center}
> 	\end{figure}
> \end{verbatim}
>
>
> \noindent The take-home message is that \texttt{savePlot} saves the day under Windows.
> As far as I know, there is no equivalent under Linux, or rather, under X.
>
> In Windows, then,
> \begin{itemize}
> \item I can plot the way I want on the screen;
> \item I can save that plot to a file without writing any other code;
> \item I can include the saved plot in my \LaTeX\ figure, allowing me to
> 	fine-tune with the \verb@\includegraphics{}@ command.
> \end{itemize}
> Strike one for the Evil Empire.
>
> \end{document}
> % <<<<<<<<<<<<<<<<<<<<<<<<<<<<<< example-windows.Rnw
>
>
>
> % >>>>>>>>>>>>>>>>>>>>>>>>>>>>>> example-linux.Rnw
> \documentclass[a4paper]{article}
>
> \begin{document}
>
> \noindent This is an example of the hapless state of my \texttt{Sweave}ing under Linux.
>
> <<echo=false,results=hide>>=
> 	# create a simple AR process:
> 	make.ar.1<- function(alpha=1,n=300) {
> 		Z<- rnorm(n);
> 		Y<- numeric(n);
> 		Y[1]<- Z[1];
> 		for (i in 2:n) Y[i]<- alpha*Y[i-1]+Z[i];
> 		return(Y)
> 	}
> @
>
> \noindent Because of the \verb at x11(width=20, height=5)@ command,
> I can't embed the graphical output that the following piece of code
> produces in my document, although I can view the results on screen:
> <<label=first.ar.1>>=
> 	# a long AR process is best viewed in a wide window:
> 	x11(width=20, height=5)
> 	sp<- make.ar.1(alpha=.5, n=800)
> 	plot(sp, type="l", col="blue")
> 	# no savePlot ... can't seem to do anything with this plot
> 	# if i try to include this code in a figure environment then
> 	# Sweave blows up
> 	# so i have to stop here :(
> @
>
> \noindent Instead, I have to do something like the following, which has the unfortunate
> side effects of disallowing me from seeing the graphical output on the screen, and,
> probably
> more importantly, of duplicating the above code:
> <<label=ar.1,echo=true>>=
> 	sp<- make.ar.1(alpha=.5, n=800)
> 	pdf("ar.pdf", width=20, height=5)
> 	plot(sp, type="l", col="blue")
> 	dev.off()
> @
> \begin{figure}
> \begin{center}
> % 	at least i still retain direct control over graphics in LaTeX; i can fine-tune the
> % 	the graphics placement as much as i want:
> 	\includegraphics[width=14.5cm]{./ar.pdf}
> \caption{An AR(1) process of length~\protect\Sexpr{length(sp)}
> is best viewed in a wide window.}
> \end{center}
> \end{figure}
>
> Under X, then,
> \begin{itemize}
> \item I have to use a device such as \texttt{pdf} and I lose the ability to first
> 	see the output on screen;
> \item I can still save that plot to a file without writing any other code;
> \item I can still include the saved plot in my \LaTeX\ figure, allowing me to
> 	fine-tune with the \verb@\includegraphics{}@ command.
> \end{itemize}
>
> \end{document}
> % <<<<<<<<<<<<<<<<<<<<<<<<<<<<<< example-linux.Rnw
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> and provide commented, minimal, self-contained, reproducible code.
>

--
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

`