sweave 和 ggplot2:根本没有生成 pdf

发布于 2024-09-11 01:17:31 字数 649 浏览 2 评论 0原文

我正在尝试创建一个 sweave 报告,其中包含一些使用 ggplot2 完成的图形。虽然我正在寻找一些长期运行的环境 - 我只是在这里使用一个简单的 .Rnw 文件,其中仅包含代码和绘图

 \documentclass[a4paper]{article}
 \SweaveOpts{echo=FALSE}
 \usepackage{a4wide}

  \begin{document}

  \begin{figure}[htbp]
  \begin{center}
 <<>>=
 library(ggplot2)
 x=rnorm(100)
 qplot(x)

 @
 \caption{My Graph}
 \end{center}
  \end{figure}
\end{document}

不幸的是,图表没有创建,我只得到了损坏的 .pdf 和 .eps 文件。尽管我得到了一个不错的 .tex 文件,除了图形之外,它似乎可以工作。 我使用以下基本代码来创建它:

 Sweave("myfile.Rnw")

我刚刚在网络上发现了一些较旧的帖子,这些帖子正在讨论透明度和 sweave / ggplot2 的问题,但没有任何帮助。我也尝试过放松套餐,但也没有帮助。顺便问一下,decumar 包有什么消息吗?

I am trying create a sweave report that contains some graphics done with ggplot2. Though I am looking for some environment for the long run – I just use a simple .Rnw file here that only contains the code and the plot

 \documentclass[a4paper]{article}
 \SweaveOpts{echo=FALSE}
 \usepackage{a4wide}

  \begin{document}

  \begin{figure}[htbp]
  \begin{center}
 <<>>=
 library(ggplot2)
 x=rnorm(100)
 qplot(x)

 @
 \caption{My Graph}
 \end{center}
  \end{figure}
\end{document}

Unfortunately the graph is not created, I only get a corrupted .pdf and .eps file. Though I get a nice .tex file that appears to work except for the graphics.
I use the following basic code to create it:

 Sweave("myfile.Rnw")

I just found some older post on the web that were discussing problems with transparency and sweave / ggplot2 but nothing that could have helped. I also tried the relaxed package, which did not help either. Btw, is there any news on decumar package?

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

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

发布评论

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

评论(3

我ぃ本無心為│何有愛 2024-09-18 01:17:31

qplot() 生成对象,而不是图形输出。当你运行它时,它看起来似乎是这样,但那是因为如果没有赋值,R 就会自动打印 qplot() 的输出。要将其集成到 Sweave 中,请将 print() 包裹在 qplot() 周围,或者将 qplot() 的输出分配给某些内容,然后包裹在 print() 中。

...
<<fig = T, echo = F>>=
 library(ggplot2)
 x=rnorm(100)
 p <- qplot(x)
 print(p)
@
...

那应该有效。我一直在我的 sweave 文档中使用 ggplot2 图形。

qplot() produces objects, not a graphic output. It might seem like it does when you run it, but that's because without assignment, R is automatically printing the output of qplot(). To integrate it into Sweave, either wrap print() around qplot(), or assign the output of qplot() to something, then wrap that in print().

...
<<fig = T, echo = F>>=
 library(ggplot2)
 x=rnorm(100)
 p <- qplot(x)
 print(p)
@
...

That should work. I use ggplot2 graphics in my sweave docs all the time.

定格我的天空 2024-09-18 01:17:31

您必须将其包裹在 print() 周围才能使其在 sweave 中工作。

You have to wrap it around print() to make it work in sweave.

月竹挽风 2024-09-18 01:17:31

实际上,虽然前面的两个答案都是正确的,但你的问题是另外一回事。

您需要确保整个代码块位于页面的左侧(函数中的缩进除外)。同样,我不知道为什么,但这会给 Sweave 带来问题。

确保所有代码(以及代码块的页眉/页脚)位于页面左侧(并添加打印语句)后,您的示例对我有用。

顺便说一句,我今天了解到,您可以在 sweave 文档中围绕代码创建一个环境(我不知道,这会节省我很多时间)。很好的旧版 stackoverflow,即使在你回答问题的时候也会教你一些新东西!

希望这有帮助。

Actually, while both previous answers are correct, your problem is something else.

You need to ensure that the entire code block is at the left of the page (apart from iundentation in functions). Again, I have no idea why but this causes problems for Sweave.

After ensuring that all code (and header/footer for code chunk) were at the left of the page (and adding a print statement) then your example works for me.

Incidentally, i learned today that you can create an environment around your code in sweave documents (which i wasn't aware of, and will save me much time). Good old stackoverflow, teaching you something new even when you answer a question!

Hope this helps.

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