开发者

ggplot2 and sweave - plot is in Rplots instead of main pdf?

开发者 https://www.devze.com 2023-04-13 04:09 出处:网络
I\'ve been following the examples on similar posts, but to no avail. Here is an example of the problem I\'m seeing.

I've been following the examples on similar posts, but to no avail. Here is an example of the problem I'm seeing.

Saved in tmp.Rnw:

\documentclass[10pt]{article}
\title{Reproducible Example} 
\begin{document}
\maketitle
\begin{center}

<<echo=FALSE,results=hide>>=
library(ggplot2)
plot.to.print  = qplot( 1:10, 1:10 ) 
@ 

\section{No Figure Below This Section T开发者_StackOverflow中文版itle}
<<<fig=true>>=
print( plot.to.print )
@ 

\end{center}
\end{document}

In tmp.co.r, I put the following code:

Sweave("tmp.Rnw",stylepath=T)

And I create the tex file like this:

/../../2.12.1/bin/R --no-save < tmp.co.r

and then use pdflatex on the tmp.tex file that comes out.

The result is tmp.pdf which contains the title, section name and R code, but no figure. However, and Rplots.pdf file is also generated which contains the figure I want in tmp.pdf.

I'm certain that I'm making a newbie mistake, but I can't find it. Any tips?


Try this one

<< label = figPlot1, include = FALSE >>=
plot.to.print  = qplot( 1:10, 1:10 ) 
print(plot.to.print)
@ 


\begin{figure}
\begin{center}
<< label = fig1, fig = TRUE, echo = FALSE >>=
<<figPlot1>>
@
\end{center}
\caption{Your Caption}
\label{fig:figPlot1}
\end{figure}


The problem is your extra < in your fig chunk.

This causes it to look like <fig=TRUE, so fig isn't actually set to TRUE properly. You'll notice that your .tex file doesn't have the proper includegraphics lines in it either.

Why you get the Rplots.pdf is a little complicated, but worth knowing about. First, every chunk that creates graphics is executed an additional time for each desired graphic type. So if you just make pdf's (the current default, I think), it's run twice; if you make pdf's and eps's it's run three times. The first time it's run, it's run without opening a graphics device; I'm actually not sure why it runs that time, but it does. For multiple files, it's necessary to run it separately which each file open in turn.

Thus the best practice is to do what you did and run all the code creating the figure in one chunk and just plot the figure in the chunk with fig=TRUE; this minimizes the code that is run multiple times. However, watch out if you're using random numbers or incrementing something in fig=TRUE chunks; since it runs multiple times, the behavior probably will not be what you expect.

Second, when code that creates a graphic is run without specifying a graphics device, the default graphic type is opened anyway for the code to work on. When you run interactively, this pops up windows with the pictures in it. When run non-interactively the default is usually to open a pdf file, and the default name is Rplots.pdf. Since this happens with all the chunks that create figures, this file ends up being a multipage pdf with all the figures you created in it.

Finally, methods that create the figure using R code instead of the fig=TRUE mechanism can sometimes be preferred so that the code is only run once; it's usually a little more bookkeeping, though that can be minimized by creating functions to help out. Evidently the AFLP package (see Thierry's answer) has functions like this, though I've never used it. Not too hard at all to write your own though if you'd rather, similar to what is recommended in the Sweave FAQ A.9 for creating multiple plots at once.

Finally (peers into crystal ball...), I see you're using Emacs in Rnw mode, where typing < gives you <<>>= with the cursor in the middle, so typing << gives you <<<>>=.


Have a look at the ggsave.latex() function from the AFLP package which is available on R-Forge

install.packages("AFLP", repos="http://R-Forge.R-project.org")

Then your Sweave file simplifies to this

\documentclass[10pt]{article}
\title{Reproducible Example} 
\begin{document}
\maketitle

<<echo=FALSE,results=hide>>=
library(ggplot2)
#just imports the function you need without loading the entire package
ggsave.latex <- AFLP:::ggsave.latex 

plot.to.print  = qplot( 1:10, 1:10 ) 
@ 

\section{No Figure Below This Section Title}
<<result = tex>>=
ggsave.latex(plot.to.print, caption = "Your caption", label = "ThisIsTheLabel")
@ 
\end{document}

Notice that ggsave.latex will set the figure environment for you. And this allows to create multiple figures and / or other LaTeX output within one chunk.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号