# R scatterplot matrix with nonparametric density

I normally use MATLAB, or JMP but right now am working with R.

I have ~150 dimensional data with a few hundred thousand rows. Some of the columns are non-informative, they only have one value. This makes some of the descendants of "pairs" to fail.

I wish that I could say "pairs(mydata)" but that doesn't work well.

I know a few (~18) higher value variables that I think are more informative so I can, if I like, enter "pairs(mydata[,indices])" and get an 18x18 scatterplot matrix plot. I don't get the nonparametric density on top of them. Can you tell me how to get this?

Now JMP has a switch that allows adding what they call a nonparametric density to scatterplots. (link, link,) I want to do that for my data.

Here are things that I have looked out but they have not been helpful:

I really want something like this, with my data, as each of the scatterplot panes in the scatterplot matrix:

• Do you really plan to look at over 22,000 of these plots after you have waited for them all to be computed? Instead of creating such detailed images, you would seem to have a pressing need to screen the data first for "interesting" pairs of variables and to display their covariation in an extremely succinct and simplified fashion. – whuber Jul 16 '14 at 15:25
• Of course I am doing pre-filtering to reduce the variable size. I'm going to look at the scatterplot matrices in subgroups, probably less than 20x20, but maybe larger than that. – EngrStudent Jul 16 '14 at 16:01

One option is the hexbin package, the display is different from what you show, but will convey the similar information and there already exists a function in the package to do the hexbin equivalent to the scatterplot matrix.

The other option is to write your own custom panel function to use with the pairs function. You could use the kde2d function from the MASS package to estimate the density and then use the output with one of the contour routines to draw the lines.

Here is some example code of the 2 ways I mentioned (neither use ggplot2, someone with more expertise there may be able to show a way to do this using ggplot2).

library(hexbin)
hexplom(iris[,1:4])

library(MASS)
library(colorspace)
myfunc <- function(x,y) {
points(x,y)
k <- kde2d(x,y)
cnt <- contourLines(k$x, k$y, k\$z)
n <- length(cnt)
cols <- rev(sequential_hcl(n))
for( i in seq_len(n) ) lines(cnt[[i]], col=cols[i])
}

pairs(iris[,1:4], panel=myfunc)
• can you give me sample r-code? – EngrStudent Jul 16 '14 at 17:40
• @EngrStudent, I added some sample code to the answer above. – Greg Snow Jul 16 '14 at 18:16
• That is beautiful. – EngrStudent Jul 16 '14 at 18:32