# More efficient plot functions in R when millions of points are present?

I find R can take a long time to generate plots when millions of points are present - unsurprising given that points are plotted individually.

Many of the points overlap and form a black mass and a lot of time is spent plotting more points into that mass.

Are there any plotting functions that trade off processing time to work out in advance what plotting is redundant?

I am not looking for a density plot (although those are often useful), I would want the same output as a simple plot call but much faster than millions of overplots if possible.

-
Please don't cross post. At the very minimum, you could tell people you're cross-posting. – Joshua Ulrich Feb 17 '11 at 22:33
Fair enough, I confess I wasn't aware of the non-crossposting etiquette. I haven't had a question where I was unsure in my own mind about were to post it before. It fell between data-visualization and r programming. I have deleted the SO question for now. – Alex Stoddard Feb 17 '11 at 22:51

This is a hard task with no ready solutions (this is of course because density plot is so a tempting fallback than no one really cares). So, what can you do?

If they really overlap (i.e. have exactly the same X & Y coordinates) and you are not using alpha, the best idea would be just to reduce the overlap using unique (with alpha, it may be summed over such groups).

If not, you may manually round the coordinates to the nearest pixels and use the previous method (yet this is a dirty solution).

Finally, you can make a density plot only to use it to subsample the points in the most dense areas. This on the other hand will not make the exactly same plot and may introduce artifacts if not precisely tuned.

-
Reducing the overlap with unique or by rounding can result in biased (deceptive) plots. It's important to somehow indicate the amount of overlap through some graphical means such as lightness or with sunflower plots. – whuber Feb 18 '11 at 17:01

Look at the hexbin package which implements paper/method by Dan Carr. The pdf vignette has more details which I quote below:

## 1 Overview

Hexagon binning is a form of bivariate histogram useful for visualizing the struc- ture in datasets with large n. The underlying concept of hexagon binning is extremely simple;

1. the xy plane over the set (range(x), range(y)) is tessellated by a regular grid of hexagons.
2. the number of points falling in each hexagon are counted and stored in a data structure
3. the hexagons with count > 0 are plotted using a color ramp or varying the radius of the hexagon in proportion to the counts. The underlying algorithm is extremely fast and eective for displaying the structure of datasets with $n \ge 10^6$

If the size of the grid and the cuts in the color ramp are chosen in a clever fashion than the structure inherent in the data should emerge in the binned plots. The same caveats apply to hexagon binning as apply to histograms and care should be exercised in choosing the binning parameters

-
That's a nice one. Just what the doctor ordered. – Roman Luštrik Feb 18 '11 at 8:56
(+1) Also of interest, smoothScatter {RColorBrewer} and densCols {grDevices}. I can confirm it works pretty well with thousand to million of points from genetic data. – chl Feb 18 '11 at 9:30

I must admit that I do not fully understand your last paragraph:

"I am not looking for a density plot (although those are often useful), I would want the same output as a simple plot call but much faster than millions of overplots if possible."

It is also unclear what type of plot (function) you are looking for.

Given that you have metric variables, you might find hexagon binned plots or sunnflower plots usefull. For further references, see

-

Another direct answer to the question is the rgl package, which can plot millions of points using OpenGL. Also, specify a point size (e.g. 3) and zoom out to see these centers of masses as monolithic blocks, or zoom in and see the structure of what used to be monolithic - the point sizes are constant but the distances among them on the screen depend on the zooming. Alpha levels can also be used.

-