# R - color scatterplot points by density

By | November 23, 2013

# Motivation

If you've ever had lots of data to examine via a scatterplot, you may find it difficult due to overlapping points. For example, let's examine the following attempt to look at some (x,y) data.

```x = rnorm(100000)
y = rnorm(100000)
plot(x,y)
```

Impossible to understand, right?

# Solution

Here's a much better solution. We will now color the points by their density.

```plot_colorByDensity = function(x1,x2,
ylim=c(min(x2),max(x2)),
xlim=c(min(x1),max(x1)),
xlab="",ylab="",main="") {

df <- data.frame(x1,x2)
x <- densCols(x1,x2, colramp=colorRampPalette(c("black", "white")))
df\$dens <- col2rgb(x)[1,] + 1L
cols <-  colorRampPalette(c("#000099", "#00FEFF", "#45FE4F","#FCFF00", "#FF9400", "#FF3100"))(256)
df\$col <- cols[df\$dens]
plot(x2~x1, data=df[order(df\$dens),],
ylim=ylim,xlim=xlim,pch=20,col=col,
cex=2,xlab=xlab,ylab=ylab,
main=main)
}

x = rnorm(100000)
y = rnorm(100000)
plot_colorByDensity(x,y)
```

And here's what we get:

Now it's easy to see the pattern in the data. There are LOTS of points in the red area, and less and less points as we move out.

Share
Send article as PDF