# A huge plot (scatter plot matrix)

One disadvantage of `recordPlot()` is that it may not be able to record huge plots due to memory limits, e.g. a scatter plot matrix of tens of thousands of points:

```{r gen-data, cache=TRUE}
# generate some random data
dat = matrix(runif(100000), ncol=5)
dat[, 3] = -.2 * dat[, 1] + .8 * dat[, 2] # to make the plot less boring
```{r eval=FALSE}
pairs(dat)
```

But scatter plots with such a large number of points are usually difficult to read (basically you can see nothing), so we'd better use some alternative ways to visualize them. For example, we can use 2D density estimates and draw contour plots, or just plot the LOWESS curve.

```{r line-contour, cache=TRUE, dependson='gen-data'}
dens2d = function(x, y, ...) {
  library(MASS)
  res = kde2d(x, y)
  with(res, contour(x, y, z, add = TRUE))
}
pairs(dat, lower.panel = dens2d, upper.panel = function(x, y, ...) {
  lines(lowess(y ~ x), col = 'red')
})
```
