Search code examples
rlatexsweave

R / Sweave: Prefixing and figure file naming


I am new to R and much newer to Sweave and I am experimenting with graphics. Here is a sample code:

\documentclass[a4paper]{article}
\usepackage{Sweave}  %%%%%%
\SweaveOpts{eps=true}
\begin{document}

<<echo=FALSE>>=
test.frame<-data.frame(year=8:11, value= c(12050,15292,23907,33991))
@

\SweaveOpts{prefix.string=Evolution}
<<label=amount,echo=FALSE,results=hide>>=
postscript('doudou.eps',
            width=6, height=7,
            colormodel="cmyk",
            family = "ComputerModern")
with(test.frame,plot(year, value))
dev.off()
@

\begin{figure}[htbp]
\begin{center}
\includegraphics[width=1\textwidth,angle=90]{doudou.eps}
\end{center}
\end{figure}

\end{document}

What I want to do above?

To have manual control over the EPS file that I am inserting so that I can have the \includegraphicscommand in the Sweave file itself.

And I am trying to give a proper file name to the figure: with prefix Evolution and label amount such that the EPS figure produced will be named Evolution-amount.eps.

What is going wrong?

As you can see, I am inserting a file name in the R postscript option i.e. doudou.eps . If I don't do this a file named Rplots.ps is created by R.

So my code is well ignoring the prefix and label that I want to give to my figure file.

And I am explicitly asking later on by \includegraphics to put doudou.eps.

How I want it to be?

To be able to have prefix and label as I mentioned above in the figure file name, while I still have manual control over the \includegraphics command in the Sweave file. Is this possible?

What is the use of this?

Say I am writing a paper and I have figures in different sections. So it would be nice to have something like:

\SweaveOpts{prefix.string=Paper2}

<<label=section2,echo=FALSE,results=hide>>=

and for example I specify in the postscript option: model.eps.

Then the figure would be named Paper2-section2.model.eps for example. Is that feasible?

And I would need to put this name somehow manually ?? in the \includegraphics command that follows.

Thanks a lot...


Update: 09 dec 2011.

A close solution with the help of cbeleites is:

\documentclass[fleqn, a4paper,12pt]{article}
\usepackage[latin1]{inputenx}
\usepackage[T1]{fontenc}
\usepackage{Sweave} %%%%%%
\SweaveOpts{eps=TRUE}



\begin{document}

<<echo=FALSE>>=
test.frame<-data.frame(year=8:11, value= c(12050,15292,23907,33991))
@

\SweaveOpts{prefix.string=Paper2}

<<label=section2, echo=FALSE,results=hide>>=
ps.options ( width=6, height=7, colormodel="cmyk", family = "ComputerModern")
@


<<label=section2, fig=TRUE, include = TRUE, echo=FALSE>>=
with(test.frame,plot(year, value))
@

\end{document} 

On compilation I get the EPS and PDF files named Paper2-section2. This is as close as we can get I think.


Solution

  • You need to use a figure chunk.

        <<fig=TRUE, include = FALSE>>=
        with(test.frame,plot(year, value))
        @
    
    • You can pass more options to the figure chunk, such as width and height.

    • for more advanced options, use R's postscript options:

      <<>>=
      ps.options (colormodel="cmyk", family = "ComputerModern")
      @
      
    • if the options apply only to one figure, you'd probably be easier off without figure chunk and with manual \includegraphics. You could give the filename to the ps file as if it was produced by a Sweave figure chunk, though.

    • If the only reason for \includegraphics is that you want to control its options, have a look at graphicx' Gin:

      \begin{figure}[htbp]
      \begin{center}
      \setkeys{Gin}{width=\linewidth}
      <<fig=TRUE>>=
      with(test.frame,plot(year, value))
      @
      \end{center}
      \end{figure}
      

    I thought Gin would work with the rotation as well, but it doesn't. see https://tex.stackexchange.com/a/31013 for explanation and possible solution. However, if the rotation is only because the figures appear sideways (as opposed to you wanting them to be sideways) this may be solved with correct ps settings in R (RSiteSearch () and ? postscript should help).