Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot2 - printing plot balloons memory

Tags:

r

ggplot2

Is it expected that printing a large-ish ggplot to PDF will cause the RSession memory to balloon? I have a ggplot2 object that is around 72 megabytes. My RSession grows to over 2 gig when printing to PDF. Is this expected? Are there ways to optimize performance? I find that the resulting PDFs are huge ~25meg and I have to use an external program to shrink them down (50kb with no visual loss!). Is there a way to print to PDF with lower quality graphics? Or perhaps some parameter to print or ggplot that I haven't considered?

like image 214
SFun28 Avatar asked Jul 02 '26 08:07

SFun28


2 Answers

For large data sets, I find it helpful to pre-process the data before putting together the ggplot (even if ggplot offers the same calculations).

ggplot has to be very general: it cannot predict what stat or geom you want to add later on, so it is very difficult to optimize things there (the split-apply-combine strategy can lead to exploding intermediat memory requirements). OTOH, you know what you want and can pre-calculate accordingly.

The large pdf indicates that you either have a lot of overplotting or you produce objects that are too small to be seen. In both cases, you could gain a lot by applying appropriate summary statistics (e.g. hexbin or boxplot instead of scatterplot).

I think we cannot tell you more without details of what you are doing. So please create a minimal example and/or upload the compressed plot you are producing.

like image 109
cbeleites unhappy with SX Avatar answered Jul 04 '26 22:07

cbeleites unhappy with SX


Addressing the second part of your question, R makes no attempt to optimize PDFs. If you are overplotting a lot of points, this results in some ridiculous behavior. You can use qpdf to post-process the PDF.

Addressing the first question anecdotally, it does seem that plots on medium-sized datasets take up a lot of memory, but that is merely my experience. Others may have more opinions as to why or more facts as to whether this is so.

like image 33
Ari B. Friedman Avatar answered Jul 05 '26 00:07

Ari B. Friedman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!