sweave 和 ggplot2:根本没有生成 pdf
我正在尝试创建一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
qplot()
生成对象,而不是图形输出。当你运行它时,它看起来似乎是这样,但那是因为如果没有赋值,R 就会自动打印qplot()
的输出。要将其集成到 Sweave 中,请将print()
包裹在qplot()
周围,或者将qplot()
的输出分配给某些内容,然后包裹在print()
中。那应该有效。我一直在我的 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 ofqplot()
. To integrate it into Sweave, either wrapprint()
aroundqplot()
, or assign the output ofqplot()
to something, then wrap that inprint()
.That should work. I use
ggplot2
graphics in my sweave docs all the time.您必须将其包裹在
print()
周围才能使其在 sweave 中工作。You have to wrap it around
print()
to make it work in sweave.实际上,虽然前面的两个答案都是正确的,但你的问题是另外一回事。
您需要确保整个代码块位于页面的左侧(函数中的缩进除外)。同样,我不知道为什么,但这会给 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.