# [R] heavy graphs

Jim Lemon jim at bitwrit.com.au
Wed Apr 16 13:10:27 CEST 2008

```Georg Ehret wrote:
> Dear R community,        I am creating large graphs with hundreds of
> thousands of datapoints. My usual way for output was pdf, but now I am
> getting file sizes of >30Mb that do not open well (or at all) in Adobe. Is
> there a way to reduce the resolution or get rid of overlaying datapoints?
> Any other idea is also warmly welcome!
>
Hi Georg,
As others have noted, bitmapped graphics will handle overlaid points
better than pdf. You might be able to use the count.overplot function
that clumps together points and prints the number of points rather than
the points themselves. However, with that many points, you might finish
up with a jumble of numbers.

Another possibility is to bin the points and print a color matrix that
represents the number of points in each bin.

# not a very elegant function, the example runs pretty slowly
bin.points<-function(x,y,nxbins,nybins) {
binmat<-matrix(0,nrow=nybins,ncol=nxbins)
xrange<-range(x)
yrange<-range(y)
xinc<-diff(xrange)/nxbins
yinc<-diff(yrange)/nybins
for(row in 1:nybins) {
for(col in 1:nxbins) {
binmat[row,col]<-
sum((x >= xrange+xinc*(row-1)) & (x < xrange+xinc*row) &
(y >= yrange+yinc*(col-1)) & (y < yrange+yinc*col))
if(col==nxbins)
binmat[row,col]<-binmat[row,col] + sum((x == xrange) &
(y >= yrange+yinc*(col-1)) & (y < yrange+yinc*col))
}
if(row==nybins)
binmat[row,col]<-binmat[row,col] + sum((y == yrange) &
(x >= xrange+xinc*(row-1)) & (x < xrange+xinc*row))
}
return(binmat)
}
binmat<-bin.points(rnorm(100000),rnorm(100000),20,20)
require(plotrix)
color2D.matplot(binmat,show.legend=TRUE)

Jim

```