将 R 绘图导入 LaTeX?

发布于 2024-08-14 15:53:43 字数 196 浏览 6 评论 0原文

我是 R 和 LaTeX 的新手,最近刚刚发现如何使用 R 绘制标准时间序列图并将其保存为 png 图像。我担心的是,将其保存为图像然后将其嵌入到 LaTeX 中会缩放它并使其看起来很难看。

有没有办法让 R 的 plot() 函数输出矢量图形并将其嵌入到 LaTeX 中?我在这两个方面都是初学者,所以请温柔点:) 代码片段非常感谢!

I'm a newbie to both R and LaTeX and have just recently found how to plot a standard time series graph using R and save it as a png image. What I'm worried about is that saving it as an image and then embedding it into LaTeX is going to scale it and make it look ugly.

Is there a way to make R's plot() function output a vector graphic and embed that into LaTeX? I'm a total beginner in both so please be gentle :) Code snippets are highly appreciated!

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

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

发布评论

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

评论(4

十六岁半 2024-08-21 15:53:43

我建议使用 tikzDevice 包来生成包含在 LaTeX 文档中的输出:

http://cran.r-project.org/web/packages/tikzDevice/index.html

tikzDevice 将 R 中生成的图形转换为可由 LaTeX 包解释的代码 tikz。 TikZ 为 LaTeX 提供了一个非常好的矢量绘图系统。 TikZ 输出的一些很好的示例位于:

http://www.texample.net/

tikzDevice 可以像任何其他 R 图形设备一样使用:

require( tikzDevice )

tikz( 'myPlot.tex' )

plot( 1, 1, main = '\\LaTex\\ is $\\int e^{xy}

请注意,LaTeX 宏中的反斜杠必须加倍,因为 R 将单个反斜杠解释为转义字符。要在 LaTeX 文档中使用绘图,只需包含它:

\include{path/to/myPlot.tex}

pgfSweave 包包含可以为您处理上述步骤的 Sweave 功能。确保您的文档在 LaTeX 序言中的某处包含 \usepackage{tikz}

http://cran.r-project.org/

tikz()<的优点< /code> 函数与 pdf() 相比是:

  • 图中标签和标题的字体始终与 LaTeX 文档中使用的字体相匹配。这为您的文档提供了统一的外观。

  • 您拥有可用于创建数学注释的 LaTeX 排字机的所有功能,并且可以在图形文本中使用任意 LaTeX 代码。

    您拥有可

tikz() 函数的缺点是:

  • 它不能很好地扩展来处理具有大量组件的绘图。这些是大型矩阵的 persp() 图。图形元素的剪切数量可能会导致 LaTeX 速度缓慢或耗尽内存。

  • 该软件包当前被标记为测试版。这意味着,如果作者找到令人信服的理由,该软件包的界面或功能可能会发生变化。

在这篇文章的结尾,我应该声明我是 tikzDevicepgfSweave 包的作者,所以我的观点可能有偏见。然而,我在去年使用这两个软件包制作了几份学术报告,并对结果非常满意。

) dev.off()

请注意,LaTeX 宏中的反斜杠必须加倍,因为 R 将单个反斜杠解释为转义字符。要在 LaTeX 文档中使用绘图,只需包含它:

pgfSweave 包包含可以为您处理上述步骤的 Sweave 功能。确保您的文档在 LaTeX 序言中的某处包含 \usepackage{tikz}

http://cran.r-project.org/

tikz()<的优点< /code> 函数与 pdf() 相比是:

  • 图中标签和标题的字体始终与 LaTeX 文档中使用的字体相匹配。这为您的文档提供了统一的外观。

  • 您拥有可用于创建数学注释的 LaTeX 排字机的所有功能,并且可以在图形文本中使用任意 LaTeX 代码。

    您拥有可

tikz() 函数的缺点是:

  • 它不能很好地扩展来处理具有大量组件的绘图。这些是大型矩阵的 persp() 图。图形元素的剪切数量可能会导致 LaTeX 速度缓慢或耗尽内存。

  • 该软件包当前被标记为测试版。这意味着,如果作者找到令人信服的理由,该软件包的界面或功能可能会发生变化。

在这篇文章的结尾,我应该声明我是 tikzDevicepgfSweave 包的作者,所以我的观点可能有偏见。然而,我在去年使用这两个软件包制作了几份学术报告,并对结果非常满意。

I would recommend the tikzDevice package for producing output for inclusion in LaTeX documents:

http://cran.r-project.org/web/packages/tikzDevice/index.html

The tikzDevice converts graphics produced in R to code that can be interpreted by the LaTeX package tikz. TikZ provides a very nice vector drawing system for LaTeX. Some good examples of TikZ output are located at:

http://www.texample.net/

The tikzDevice may be used like any other R graphics device:

require( tikzDevice )

tikz( 'myPlot.tex' )

plot( 1, 1, main = '\\LaTex\\ is $\\int e^{xy}

Note that the backslashes in LaTeX macros must be doubled as R interprets a single backslash as an escape character. To use the plot in a LaTeX document, simply include it:

\include{path/to/myPlot.tex}

The pgfSweave package contains Sweave functionality that can handle the above step for you. Make sure that your document contains \usepackage{tikz} somewhere in the LaTeX preamble.

http://cran.r-project.org/

The advantages of tikz() function as compared to pdf() are:

  • The font of labels and captions in your figures always matches the font used in your LaTeX document. This provides a unified look to your document.

  • You have all the power of the LaTeX typesetter available for creating mathematical annotation and can use arbitrary LaTeX code in your figure text.

Disadvantages of the tikz() function are:

  • It does not scale well to handle plots with lots of components. These are things such as persp() plots of large matricies. The shear number of graphic elements can cause LaTeX to slow to a crawl or run out of memory.

  • The package is currently flagged as beta. This means that the interface or functionality of the package is subject to change if the authors find a compelling reason to do so.

I should end this post by disclaiming that I am an author of both the tikzDevice and pgfSweave packages so my opinion may be biased. However, I have used both packages to produce several academic reports in the last year and have been very satisfied with the results.

) dev.off()

Note that the backslashes in LaTeX macros must be doubled as R interprets a single backslash as an escape character. To use the plot in a LaTeX document, simply include it:

The pgfSweave package contains Sweave functionality that can handle the above step for you. Make sure that your document contains \usepackage{tikz} somewhere in the LaTeX preamble.

http://cran.r-project.org/

The advantages of tikz() function as compared to pdf() are:

  • The font of labels and captions in your figures always matches the font used in your LaTeX document. This provides a unified look to your document.

  • You have all the power of the LaTeX typesetter available for creating mathematical annotation and can use arbitrary LaTeX code in your figure text.

Disadvantages of the tikz() function are:

  • It does not scale well to handle plots with lots of components. These are things such as persp() plots of large matricies. The shear number of graphic elements can cause LaTeX to slow to a crawl or run out of memory.

  • The package is currently flagged as beta. This means that the interface or functionality of the package is subject to change if the authors find a compelling reason to do so.

I should end this post by disclaiming that I am an author of both the tikzDevice and pgfSweave packages so my opinion may be biased. However, I have used both packages to produce several academic reports in the last year and have been very satisfied with the results.

寄风 2024-08-21 15:53:43

Shane 很准确,你确实想要 Sweave。最终。

作为新手,你最好还是分开任务。为此,请执行以下操作:

  1. 打开设备:pdf("figures/myfile.pdf", height=6, width=6)
  2. 绘制 R 对象:plot(1:10, type='l', main='boring') - 请记住,lattice 和 ggplot 需要显式的 print情节。
  3. 重要提示:关闭您的设备:dev.off() 以完成文件。
  4. 可选:检查 pdf 文件。
  5. 在 LaTeX 中,在文档标题中使用 usepackage{graphicx},使用
    \includegraphics[width=0.98\textwidth]{figures/myfile} 以包含之前创建的图形,并注意文件扩展名是可选的。
  6. 通过 pdflatex 运行它并享受。

Shane is spot-on, you do want Sweave. Eventually.

As a newbie, you may better off separating task though. For that, do this:

  1. open a device: pdf("figures/myfile.pdf", height=6, width=6).
  2. plot your R object: plot(1:10, type='l', main='boring') -- and remember that lattice and ggplot need an explicit print around plot.
  3. important: close your device: dev.off() to finalize the file.
  4. optional: inspect the pdf file.
  5. in LaTeX, use usepackage{graphicx} in the document header, use
    \includegraphics[width=0.98\textwidth]{figures/myfile} to include the figure created earlier and note that file extension is optional.
  6. run this through pdflatex and enjoy.
呆橘 2024-08-21 15:53:43

您可能需要考虑使用 Sweave。 Sweave 网站(以及其他地方)上有很多关于这方面的优秀文档。它的语法非常简单:只需将 R 代码放在 <<>>=@ 之间即可。

这是一个简单的示例,最终看起来像这样

\documentclass[a4paper]{article}

\title{Sweave Example 1}
\author{Friedrich Leisch}

\begin{document}

\maketitle

In this example we embed parts of the examples from the
\texttt{kruskal.test} help page into a \LaTeX{} document:

<<>>=
data(airquality)
library(ctest)
kruskal.test(Ozone ~ Month, data = airquality)
@
which shows that the location parameter of the Ozone 
distribution varies significantly from month to month. Finally we
include a boxplot of the data:

\begin{center}
<<fig=TRUE,echo=FALSE>>=
boxplot(Ozone ~ Month, data = airquality)
@
\end{center}

\end{document}

要构建文档,您只需调用 R CMD Sweave file.Rnw 或从 R 中运行 Sweave(file) 即可。

You might want to consider using Sweave. There is a lot of great documentation available for this on the Sweave website (and elsewhere). It has very simple syntax: just put your R code between <<>>= and @.

Here's a simple example that ends up looking like this:

\documentclass[a4paper]{article}

\title{Sweave Example 1}
\author{Friedrich Leisch}

\begin{document}

\maketitle

In this example we embed parts of the examples from the
\texttt{kruskal.test} help page into a \LaTeX{} document:

<<>>=
data(airquality)
library(ctest)
kruskal.test(Ozone ~ Month, data = airquality)
@
which shows that the location parameter of the Ozone 
distribution varies significantly from month to month. Finally we
include a boxplot of the data:

\begin{center}
<<fig=TRUE,echo=FALSE>>=
boxplot(Ozone ~ Month, data = airquality)
@
\end{center}

\end{document}

To build the document, you can just call R CMD Sweave file.Rnw or run Sweave(file) from within R.

久伴你 2024-08-21 15:53:43

这是一个关于SO的问题,我找不到。

但:
http://r-forge.r-project.org/projects/tikzdevice/< /a> -- r 的 tikz 输出

http://www.rforge.net/pgfSweave/ 通过 sweave 的 tikz 代码。

使用 tikz 将使您的外观与文档的其余部分保持一致,而且它将使用 Latex 来排版图表中的所有文本。

编辑
将 LaTeX 放入 R 图中

This is a dupe of a question on SO that I can't find.

But:
http://r-forge.r-project.org/projects/tikzdevice/ -- tikz output from r
and
http://www.rforge.net/pgfSweave/ tikz code via sweave.

Using tikz will give you a look consistent with the rest of your document, plus it will use latex to typeset all the text in your graphs.

EDIT
Getting LaTeX into R Plots

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