[Tex/LaTex] Memory issue when producing dense TikZ figure (14500 points scatterplot) with knitr

knitrrsweave

I have a series of plots in my document which are correctly drawn by knitr with dev=tikz. Yet figure number 7, which is much more dense than the others in terms of data points, foo, doesn't:

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

Deleting all accessory files, cache and figure directories and re-runing knitr doesn't solve the problem.

I followed the suggestions of this answer:

figure/foo.log concluded with

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

I couldn't spot anything wrong in figure/foo.tikz

After running xelatex foo.tikz I got a similar conclusion

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

The problem seems about memory (isn't it?). So how could I fix it?

EDIT:

I was able to replicate the error with a 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}

By reducing the length of vector1 and vector2 to 14 I get my plot, then it is clearly a memory problem and specifically a problem generate by dev=tikz because I can produce a plot with the same 14500 length vectors without using the TikZ device.

Best Answer

In this case, you are basically out of luck. You cannot draw plots with too many graphical elements using the tikz device in R. You have to either simplify your plot, or use another graphical device (such as the default pdf device; if you really care a lot about the font family, you may take a look at this blog post).

Related Question