ggplot2 和 sweave - 绘图在 Rplots 中而不是主 pdf 中?

发布于 2024-12-10 00:49:00 字数 767 浏览 0 评论 0 原文

我一直在关注类似帖子上的示例,但无济于事。 这是我看到的问题的示例。

保存在 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 Title}
<<<fig=true>>=
print( plot.to.print )
@ 

\end{center}
\end{document}

在 tmp.co.r 中,我输入以下代码:

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

我像这样创建 tex 文件:

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

然后在出现的 tmp.tex 文件上使用 pdflatex。

结果是 tmp.pdf,其中包含标题、部分名称和 R 代码,但没有数字。但是,还生成了 Rplots.pdf 文件,其中包含 tmp.pdf 中我想要的图形。

我确信我犯了一个新手错误,但我找不到它。有什么建议吗?

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 Title}
<<<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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

临走之时 2024-12-17 00:49:00

试试这个

<< 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}

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}
浪漫之都 2024-12-17 00:49:00

问题是你的无花果块中额外的 <

这导致它看起来像 ,因此 fig 实际上并未正确设置为 TRUE。您会注意到您的 .tex 文件中也没有正确的 includegraphics 行。

为什么你会得到 Rplots.pdf 有点复杂,但值得了解一下。首先,每个创建图形的块都会针对每种所需的图形类型额外执行一次。因此,如果你只是制作 pdf(我认为是当前的默认设置),它会运行两次;如果你制作pdf和eps,它会运行三次。第一次运行时,无需打开图形设备即可运行;我实际上不确定为什么它会在那个时间运行,但确实如此。对于多个文件,需要单独运行,每个文件依次打开。

因此,最佳实践是按照您所做的操作并运行在一个块中创建图形的所有代码,然后使用 fig=TRUE 在该块中绘制图形;这可以最大限度地减少多次运行的代码。但是,请注意您是否使用随机数或在 fig=TRUE 块中递增某些内容;由于它运行多次,行为可能不会是您所期望的。

其次,当在没有指定图形设备的情况下运行创建图形的代码时,无论如何都会打开默认图形类型以使代码能够运行。当您以交互方式运行时,会弹出其中包含图片的窗口。当非交互运行时,默认通常是打开一个 pdf 文件,默认名称是 Rplots.pdf。由于创建图形的所有块都会发生这种情况,因此该文件最终会成为包含您在其中创建的所有图形的多页 pdf。

最后,有时可以首选使用 R 代码而不是 fig=TRUE 机制创建图形的方法,以便代码仅运行一次;通常需要更多的簿记工作,不过可以通过创建帮助的函数来最大程度地减少簿记工作。显然 AFLP 包(参见 Thierry 的回答)具有这样的功能,尽管我从未使用过它。如果您愿意的话,编写自己的代码一点也不难,类似于 Sweave FAQ A.9 用于一次创建多个绘图。

最后(仔细观察水晶球......),我看到您在 Rnw 模式下使用 Emacs,其中输入 < 会得到 <<>>= 光标位于中间,因此输入 << 会得到 <<<>>=

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 <<<>>=.

冷情妓 2024-12-17 00:49:00

查看 R-Forge 上提供的 AFLP 包中的 ggsave.latex() 函数

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

然后您的 Sweave 文件简化为以下

\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}

注意 ggsave.latex 将为您设置图形环境。这允许在一个块内创建多个图形和/或其他 LaTeX 输出。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文