如何在LaTeX文档中包含复杂的R图?

时间:2021-07-20 06:13:08

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:

“myscatterplot的地方。pdf"由以下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:

问题是,在使用texworks (pdfLatex+makeindex+bibtex)编译时,生成的乳胶文档具有图形轴,但没有图形或轴标签中的点(在本例中,这只是3d轴本身)。不存在由R或LaTeX输出的错误或警告消息。我用:

  • R 2.12.1 on Windows 7,
  • Windows 7上的R 2.12.1,
  • MiKTeX 2.8,
  • MiKTeX 2.8,
  • TeXworks
  • TeXworks
  • Adobe Reader 9 (not sure if this is relevant...)
  • adobereader9(不确定这是否相关…)

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.

我已经能够使用\includegraphics命令包含图形的png版本,并打开“myscatterplot”。使用adobe的pdf“显示了我在文档中需要的图形。

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!).

我尝试使用tikz包作为一个解决方案,但是由于scatterplot3d产生了太多的信息,所以由于内存大小(错误(我的实际情节将会有10000 +点),所以无法将生成的数据包含在latex文档中。

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“文件,但我已尝试更改pdf字体使用

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

使用\includegraphics时,结果完全相同(changefon .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.

我正在考虑的另一个可能的问题是,散倍图3d输出实际上是多个图像,而\includegraphics只是从pdf文件中获取第一个图像(坐标轴)。在这种情况下,我仍然不确定如何解决它。

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!

我真的很喜欢一个变通方案,因为我最终想用Sweave来完成所有这些,这让我对这个包的漂亮输出感到痛苦!

Thank you in advance for your responses.

提前感谢您的回复。

Edit 1:

编辑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:

所以,第一个建议是使用EPS格式而不是pdf格式。这产生了一些结果,但不是我想要的。我跑以下:

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

这确实带来了改进(尽管奇怪的是,它在乳胶输出中以顺时针方向旋转了90度),现在我的乳胶输出中有了散点和轴!但是,尽管我打开了“更改”,axis标签仍然不在图上。eps“使用ghostview,和轴线都在图中!似乎散倍体3d输出图形的方式与用图形搜索图形的方法不一致……

So, I'm still looking for a solution to this that will include axes labels.

所以,我仍然在寻找一个包含坐标轴标签的解决方案。

6 个解决方案

#1


5  

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

看着myscatterplot。与您列出的命令一起生成的pdf,有坐标轴和标签。但是,pdf相当大(7英寸x7英寸)。

Does it help if you play with the weight/height parameters to pdf()?

如果您使用pdf()中的体重/身高参数,会有帮助吗?

pdf("myscatterplot.pdf", height=3.5, width=3.5)

#2


4  

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 to pdftex, which proved capable of handling plots with many graphical elements.

我刚刚用tikzDevice的例子做了一些压力测试,结果看起来非常依赖于使用哪个TeX引擎。特别值得注意的是luatex, pdftex的sucessor,它被证明能够处理带有许多图形元素的绘图。

  • pdflatex:

    pdflatex:

    Made it through a plot with 1000 points, exceeded memory and died at 10,000.

    通过一个1000点的情节,超过了记忆,死于10000点。

  • xelatex:

    xelatex:

    Also exceeded memory and died at 10,000 points.

    也超过记忆,死于10000点。

  • lualatex:

    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.

    在45秒内咀嚼10000点并生成一个1mb的PDF文件。在大约7.5分钟内读了100,00点(一个10mb的输入文件),输出了一个8.5 MB的PDF文件,内存使用量最高达到了750 MB。没有尝试100万的基准。

It looks like pdftex and xetex 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.

看起来pdftex和xetex会在程序挂起时预先分配它们所有的内存,这就是它们所能得到的。另一方面,luatex似乎可以动态地分配内存,因此只受可用RAM数量的限制。

So, if pdflatex is giving you "out of memory" errors, try taking lualatex for a spin!

所以,如果pdflatex给你“内存不足”的错误,试试使用lualatex !


These tests were done using TeX compilers included in the TeX Live 2010 distribution. I am also one of the authors of the tikzDevice

这些测试是使用TeX Live 2010发行版中包含的TeX编译器完成的。我也是tikzDevice的作者之一

#3


3  

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)

您可以尝试Sweave: http://www.statistics k.lmu.de/~leisch/Sweave/ (Sweave是一种允许在latex文档中嵌入完整数据分析的R代码的工具)

#4


1  

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.

如果您想在文件中插入pdf图像,我认为您必须使用pdflatex而不是标准的latex。如果没有安装,我将尝试从R而不是pdf生成eps格式的图形。

#5


1  

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:

我记得我也曾遇到过类似的问题。不记得我能不能在图中看到标签。回顾一下代码,我在R级修改了两个参数:

par(xpd=NA)
par(oma=c(3, 3, 0, 0))

also for postscript in order to not have graphs rotated by 90 degrees:

对于后记,为了不让图形旋转90度:

postscript(file="xx.eps", horizontal=FALSE)

HTH

HTH

#6


1  

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.

实际上,我带着同样的问题来到这里(TexWorks、pdfLatex、Windows 7),我发现了一些有趣的事情:当我第一次开始使用Latex时,这些数字都会出现在坐标轴上,当我重新运行pdfLatex时,这些坐标轴又会消失。这给了我一个直觉,我明白了:尽管乳胶预览没有显示坐标轴,但PDF格式显示了。你甚至不需要告诉\includegraphics命令,图形是PDF格式的。

pdf("C:/Users/Orr/Documents/Leiden University/Master thesis/Chapters/Images/bioRes/Boxplots mouse raw data.pdf")
boxplot(mouse_data_raw,main="Mouse raw data")
dev.off()

\begin{figure}[t]
\includegraphics[scale=0.5]{mouse-box-raw}
\includegraphics[scale=0.5]{human-box-raw}
\end{figure}

#1


5  

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

看着myscatterplot。与您列出的命令一起生成的pdf,有坐标轴和标签。但是,pdf相当大(7英寸x7英寸)。

Does it help if you play with the weight/height parameters to pdf()?

如果您使用pdf()中的体重/身高参数,会有帮助吗?

pdf("myscatterplot.pdf", height=3.5, width=3.5)

#2


4  

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 to pdftex, which proved capable of handling plots with many graphical elements.

我刚刚用tikzDevice的例子做了一些压力测试,结果看起来非常依赖于使用哪个TeX引擎。特别值得注意的是luatex, pdftex的sucessor,它被证明能够处理带有许多图形元素的绘图。

  • pdflatex:

    pdflatex:

    Made it through a plot with 1000 points, exceeded memory and died at 10,000.

    通过一个1000点的情节,超过了记忆,死于10000点。

  • xelatex:

    xelatex:

    Also exceeded memory and died at 10,000 points.

    也超过记忆,死于10000点。

  • lualatex:

    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.

    在45秒内咀嚼10000点并生成一个1mb的PDF文件。在大约7.5分钟内读了100,00点(一个10mb的输入文件),输出了一个8.5 MB的PDF文件,内存使用量最高达到了750 MB。没有尝试100万的基准。

It looks like pdftex and xetex 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.

看起来pdftex和xetex会在程序挂起时预先分配它们所有的内存,这就是它们所能得到的。另一方面,luatex似乎可以动态地分配内存,因此只受可用RAM数量的限制。

So, if pdflatex is giving you "out of memory" errors, try taking lualatex for a spin!

所以,如果pdflatex给你“内存不足”的错误,试试使用lualatex !


These tests were done using TeX compilers included in the TeX Live 2010 distribution. I am also one of the authors of the tikzDevice

这些测试是使用TeX Live 2010发行版中包含的TeX编译器完成的。我也是tikzDevice的作者之一

#3


3  

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)

您可以尝试Sweave: http://www.statistics k.lmu.de/~leisch/Sweave/ (Sweave是一种允许在latex文档中嵌入完整数据分析的R代码的工具)

#4


1  

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.

如果您想在文件中插入pdf图像,我认为您必须使用pdflatex而不是标准的latex。如果没有安装,我将尝试从R而不是pdf生成eps格式的图形。

#5


1  

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:

我记得我也曾遇到过类似的问题。不记得我能不能在图中看到标签。回顾一下代码,我在R级修改了两个参数:

par(xpd=NA)
par(oma=c(3, 3, 0, 0))

also for postscript in order to not have graphs rotated by 90 degrees:

对于后记,为了不让图形旋转90度:

postscript(file="xx.eps", horizontal=FALSE)

HTH

HTH

#6


1  

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.

实际上,我带着同样的问题来到这里(TexWorks、pdfLatex、Windows 7),我发现了一些有趣的事情:当我第一次开始使用Latex时,这些数字都会出现在坐标轴上,当我重新运行pdfLatex时,这些坐标轴又会消失。这给了我一个直觉,我明白了:尽管乳胶预览没有显示坐标轴,但PDF格式显示了。你甚至不需要告诉\includegraphics命令,图形是PDF格式的。

pdf("C:/Users/Orr/Documents/Leiden University/Master thesis/Chapters/Images/bioRes/Boxplots mouse raw data.pdf")
boxplot(mouse_data_raw,main="Mouse raw data")
dev.off()

\begin{figure}[t]
\includegraphics[scale=0.5]{mouse-box-raw}
\includegraphics[scale=0.5]{human-box-raw}
\end{figure}