使用 knitr 生成密集的 TikZ 图形(14500 点散点图)时出现内存问题

使用 knitr 生成密集的 TikZ 图形(14500 点散点图)时出现内存问题

我的文档中有一系列图表,这些图表都可以用 knitr 正确绘制dev=tikz。然而,图 7 的数据点密度比其他图表高得多,foo但并非如此:

label: foo (with options) 
List of 3
 $ include   : logi TRUE
 $ fig.width : num 3
 $ fig.height: num 3

Quitting from lines 142-149 (myfile.Rnw) # These are chunk's first and list line
Error: failed to compile figure/foo.tikz to PDF

Execution halted

删除所有附件文件、缓存和图形目录并重新运行knitr并不能解决问题。

我听从了这个答案

figure/foo.log结论是

[...other stuff...]
Package microtype Info: Loading generic settings for font family
(microtype)             `zplm' (encoding: OT1).
(microtype)             For optimal results, create family-specific settings.
(microtype)             See the microtype manual for details.
Package microtype Info: Loading generic settings for font family
(microtype)             `cmss' (encoding: OT1).
(microtype)             For optimal results, create family-specific settings.
(microtype)             See the microtype manual for details.
\c__siunitx_mathtt_int=\count441
Package microtype Info: Loading generic settings for font family
(microtype)             `cmtt' (encoding: OT1).
(microtype)             For optimal results, create family-specific settings.
(microtype)             See the microtype manual for details.

Runaway definition?
->
! TeX capacity exceeded, sorry [main memory size=5000000].
\pgf@sys@bp ...rrentprotocol {\the \pgfutil@toks@ 
                                                  }}
l.13807 ...=0.30] ( 49.08, 79.20) circle (  2.13);

If you really absolutely need more capacity,
you can ask a wizard to enlarge me.


Here is how much of TeX's memory you used:
 57490 strings out of 493734
 1253899 string characters out of 6146348
 5000001 words of memory out of 5000000
 60062 multiletter control sequences out of 15000+600000
 16802 words of font info for 45 fonts, out of 8000000 for 9000
 1328 hyphenation exceptions out of 8191
 59i,3n,92p,10362b,776s stack positions out of 5000i,500n,10000p,200000b,80000s
No pages of output.

我没发现什么问题figure/foo.tikz

运行后xelatex foo.tikz我得到了类似的结论

[...other stuff...]

Runaway definition?
->
! TeX capacity exceeded, sorry [main memory size=5000000].
\pgf@sys@bp ...rrentprotocol {\the \pgfutil@toks@ 
                                                  }}
l.13807 ...=0.30] ( 49.08, 79.20) circle (  2.13);

No pages of output.

问题似乎与内存有关(不是吗?)。那么我该如何修复它?

编辑:

我能够用 MWE 复制该错误

\documentclass[a4paper,12pt,twoside]{article}

\begin{document}

<<opt, include=FALSE, cache=TRUE, cache.lazy=FALSE>>=
library(ggplot2)

# Global options
options(tikzDefaultEngine='xetex')
opts_chunk$set(echo=FALSE, results=FALSE, message=FALSE, warning=FALSE, include=FALSE, 
               cache=TRUE, cache.lazy=FALSE,
               dev='tikz', out.width='.90\\linewidth', fig.show='asis')
theme_set(theme_classic())
@

<<foo, include=TRUE, fig.width=3, fig.height=3>>=

set.seed(99)
data <- data.frame(vector1 = as.integer(rlnorm(14500, sdlog = 2)),
                   vector2 = as.integer(rnorm(14500)))

ggplot(data, aes(vector1, vector2)) + geom_point(alpha=.2)
@


\end{document}

vector1通过将和的长度减少vector2到 14,我得到了我的图,那么它显然是一个内存问题,特别是由产生的问题,dev=tikz因为我可以在不使用 TikZ 设备的情况下生成具有相同 14500 长度向量的图。

答案1

在这种情况下,你基本上没运气。你不能使用tikzR 中的设备绘制包含太多图形元素的图。你必须简化你的图,或者使用另一个图形设备(例如默认的pdf;如果你真的非常关心字体系列,你可以看看这篇博文)。

相关内容