如何在 LaTeX 文档中包含复杂的 R 绘图?
我遇到以下代码片段的问题:
\documentclass{article}
\usepackage{graphicx}
\begin{document}
\includegraphics{myscatterplot.pdf}
\end{document}
其中“myscatterplot.pdf”是由 R 中的以下代码生成的:
library(scatterplot3d)
pdf("myscatterplot.pdf")
scatterplot3d(rnorm(100), rnorm(100), 1:100, highlight.3d = TRUE)
dev.off()
问题是使用 texworks (pdfLatex+makeindex+bibtex) 编译时生成的 LaTeX 文档具有图形轴,但绘图或轴标签中没有任何点(在这种情况下,它只是 3d 轴本身)。 R 或 LaTeX 没有输出错误或警告消息。 我正在使用:
- Windows 7 上的 R 2.12.1、
- MiKTeX 2.8、
- TeXworks
- Adobe Reader 9(不确定这是否是 相关...)
我已经能够使用 \includegraphics 命令包含该图的 png 版本,并使用 adobe 打开“myscatterplot.pdf”显示我想要在文档中显示的图。
我尝试使用 tikz 包作为解决方法,但似乎 scatterplot3d 生成的信息太多,由于内存大小,结果图无法包含在乳胶文档中(错误(我的实际绘图将有 10000 + 点! )
我怀疑问题是由于“.pdf”文件中的字体引起的,但我尝试使用 \includegraphics(changefont.pdf) 来更改 pdf 字体,
pdf("changefont.pdf")
par(family = "Helvetica")
scatterplot3d(rnorm(100), rnorm(100), 1:100, highlight.3d = TRUE)
dev.off()
完全相同。
结果 我正在考虑的问题是,也许 scatterplot3d 输出实际上是多个图像,而 \includegraphics 仅从 pdf 文件中获取第一个数字(轴),在这种情况下,我仍然不确定如何解决它。 我真的很感激一个解决方法,
因为我最终想用 Sweave 来完成所有这些,这让我对这个包原本漂亮的输出感到痛苦!
编辑
1:
所以,第一个 。建议使用 EPS 格式而不是 pdf。这产生了一些结果,但不是我想要的。我运行了以下命令:
\documentclass{article}
\usepackage{graphicx,epstopdf}
\begin{document}
\begin{figure}
\includegraphics[angle = 270, width= 6in, keepaspectratio=true]{change.eps}
\end{figure}
\end{document}
我使用
postscript("change.eps")
scatterplot3d(rnorm(100), rnorm(100), 1:100, highlight.3d = TRUE)
dev.off()
This did 生成了“change.eps”,产生了改进(尽管奇怪的是,它在乳胶输出中将绘图顺时针旋转了 90 度!),并且我现在我的乳胶输出中有来自散点图的轴和点!然而,轴标签仍然不在图上,即使我已经使用 Ghostview 打开“change.eps”,并且轴位于图中!似乎 scatterplot3d 输出数字的方式与 \includegraphics 搜索数字的方式不一致......
所以,我仍在寻找包含轴标签的解决方案。
I'm having the problem with the following code snippet:
\documentclass{article}
\usepackage{graphicx}
\begin{document}
\includegraphics{myscatterplot.pdf}
\end{document}
Where "myscatterplot.pdf" is generated by the following code in R:
library(scatterplot3d)
pdf("myscatterplot.pdf")
scatterplot3d(rnorm(100), rnorm(100), 1:100, highlight.3d = TRUE)
dev.off()
The problem is that the resulting LaTeX document when compiling with texworks (pdfLatex+makeindex+bibtex) has the graph axes, but none of the points in the plot or axis labels(In this case, it is just the 3d axes themselves). There are no error or warning messages output by R or LaTeX.
I am using:
- R 2.12.1 on Windows 7,
- MiKTeX 2.8,
- TeXworks
- Adobe Reader 9 (not sure if this is
relevant...)
I have been able to use the \includegraphics command to include a png version of the figure, and opening "myscatterplot.pdf" with adobe shows the figure I want in my document.
I have tried to use the tikz package as a workaround, but it seems there is so much information generated by scatterplot3d that the resulting figure cannot be included in the latex document due to memory size (error (my actual plot will have 10000 + points!).
I have a suspicion that the problem is due to the fonts in the ".pdf" file, but I have tried to change the pdf fonts using
pdf("changefont.pdf")
par(family = "Helvetica")
scatterplot3d(rnorm(100), rnorm(100), 1:100, highlight.3d = TRUE)
dev.off()
with precisely the same result when using \includegraphics(changefont.pdf).
The other possible problem I am considering is that maybe the scatterplot3d output is actually multiple images, and \includegraphics is only taking the first of the figures (the axes) from the pdf file. In this case, I'm still not sure how to work around it.
I would really appreciate a workaround, as I would eventually like to do all of this with Sweave and this is making me bitter toward the otherwise beautiful output of the package!
Thank you in advance for your responses.
Edit 1:
So, the first recommendation was to use the EPS format instead of pdf. This yielded some results, but not what I wanted. I ran the following:
\documentclass{article}
\usepackage{graphicx,epstopdf}
\begin{document}
\begin{figure}
\includegraphics[angle = 270, width= 6in, keepaspectratio=true]{change.eps}
\end{figure}
\end{document}
I generated "change.eps" using
postscript("change.eps")
scatterplot3d(rnorm(100), rnorm(100), 1:100, highlight.3d = TRUE)
dev.off()
This did yield an improvement (despite the fact that it, strangely enough, rotated the plot 90 degrees clockwise in the latex output!), and I now have the axes and the points from the scatterplot in my latex output! However, the axis labels are still not on the figure, even though I have opened "change.eps" using ghostview, and the axes are in the plot! It seems the way scatterplot3d outputs figures doesn't agree with the way \includegraphics searches for figures...
So, I'm still looking for a solution to this that will include axes labels.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以尝试 Sweave:http://www.statistik.lmu.de/~leisch/Sweave / (Sweave 是一个允许在 Latex 文档中嵌入 R 代码以进行完整数据分析的工具)
You can try Sweave: http://www.statistik.lmu.de/~leisch/Sweave/ (Sweave is a tool that allows to embed the R code for complete data analyses in latex documents)
如果你想将 pdf 图像插入文件中,我认为你必须使用 pdflatex 而不是标准乳胶。如果未安装,我会尝试从 R 生成 eps 格式的数字,而不是 pdf。
If you want to insert pdf images into your file, I think you have to use pdflatex instead of standard latex. If that isn't installed, I'd try generating the figures in eps format from R, instead of pdf.
我记得曾遇到过类似的问题。但不记得我是否能在图中看到标签。回顾代码,我在 R 级别修改了两个参数:
也用于 postscript,以免图形旋转 90 度:
HTH
I recall struggling with similar problem. Don't remember if I could see labels in the figure though. Looking backwards at the code there are two parameters which I modified at R level:
also for postscript in order to not have graphs rotated by 90 degrees:
HTH
实际上,我来到这里时遇到了完全相同的问题(TexWorks、pdfLatex、Windows 7)。我发现了一些有趣的事情:当我第一次启动 Latex 时,图形出现时带有轴等等,当我重新运行 pdfLatex 时,轴再次消失。这给了我一种预感,我想通了:尽管乳胶预览不显示轴,但 PDF 格式却显示轴。您甚至不必告诉
\includegraphics
命令图形是 PDF 格式。I actually came here with the exact same problem (TexWorks, pdfLatex, Windows 7). And I found something interesting: when I first start Latex, the figures appeared with axes and all, and when I re-ran the pdfLatex, the axes disappeared again. This gave me a hunch, and I figured it out: even though the latex preview does not show the axes, the PDF format does. And you don't even have to tell the
\includegraphics
command that the graphics is in PDF format.这是与 LaTeX 超出其可用内存相关的旁注。
我刚刚使用你的例子和 tikzDevice 做了一些压力测试,看起来结果很大程度上取决于使用哪个 TeX 引擎。特别值得注意的是
luatex
,它是pdftex
的后继者,事实证明它能够处理包含许多图形元素的绘图。pdflatex
:通过了1000分的剧情,超出了记忆,在10,000时死亡。
xelatex
:同样超出内存,在10000点时死亡。
lualatex
:在大约 45 秒内咀嚼了 10,000 个点并生成了 1 MB PDF 文件。在约 7.5 分钟内艰难地完成 100,00 个点(10 MB 输入文件),生成 8.5 MB PDF 文件,内存使用量达到峰值约 750 MB。没有尝试 100 万个基准。
看起来
pdftex
和xetex
在程序启动时预先分配了所有内存,这就是它们所能获得的全部内存。另一方面,luatex 似乎可以动态分配内存,因此仅受可用 RAM 量的限制。因此,如果 pdflatex 给您带来“内存不足”错误,请尝试使用 lualatex 来试试!
这些测试是使用 TeX Live 2010 发行版中包含的 TeX 编译器完成的。我也是 tikzDevice 的作者之一
This is a side note related to LaTeX exceeding its available memory.
I just used your example with the tikzDevice to do some stress tests and it looks like the results are pretty dependent on which TeX engine is used. Of particular note is
luatex
, the sucessor topdftex
, which proved capable of handling plots with many graphical elements.pdflatex
:Made it through a plot with 1000 points, exceeded memory and died at 10,000.
xelatex
:Also exceeded memory and died at 10,000 points.
lualatex
:Chewed through 10,000 points in ~45 seconds and produced a 1 MB PDF file. Slogged through 100,00 points (a 10 MB input file) in ~7.5 minutes, spat out a 8.5 MB PDF file and peaked at ~750 MB of memory usage. Didn't try the 1 million benchmark.
It looks like
pdftex
andxetex
allocate all of their memory up front when the program lanches and that is all they will ever get.luatex
on the other hand seems like it can dynamically allocate memory and would therefore only be constrained by the amount of RAM available.So, if
pdflatex
is giving you "out of memory" errors, try takinglualatex
for a spin!These tests were done using TeX compilers included in the TeX Live 2010 distribution. I am also one of the authors of the tikzDevice
查看使用您列出的命令生成的 myscatterplot.pdf,轴和标签都在那里。然而,pdf 相当大(7 英寸 x 7 英寸)。
如果您使用 pdf() 的体重/身高参数有帮助吗?
Looking at myscatterplot.pdf as generated with the commands you listed, the axes and labels are there. However, the pdf is rather large (7in x 7in).
Does it help if you play with the weight/height parameters to pdf()?