How to display scatter plot matrices with R and lattice

(This article was first published on Omnia sunt Communia! » R-english, and kindly contributed to R-bloggers)

In lattice, there is a function called splom for the display of scatter plot matrices. For large datasets, the panel.hexbinplot from the hexbin package is a better option than the default panel.

As an example, let’s use some meteorological data from MAPA-SIAR:

library(solaR)
library(hexbin)

aranjuez <- readMAPA(prov=28, est=3, start='01/01/2004', end='31/12/2010')

aranjuezDF <- subset(as.data.frame(getData(aranjuez)),
select=c('TempMedia', 'TempMax', 'TempMin',
'Precipitacion', 'G0'))


Now we can use splom with panel.hexbinplot and panel.loess. Besides, I have included some changes to diag.panel in order to show the univariate density of each variable (adapted from here):

splom(aranjuezDF,
panel=panel.hexbinplot,
diag.panel = function(x, ...){
yrng <- current.panel.limits()$ylim d <- density(x, na.rm=TRUE) d$y <- with(d, yrng[1] + 0.95 * diff(yrng) * y / max(y) )
panel.lines(d)
diag.panel.splom(x, ...)
},
lower.panel = function(x, y, ...){
panel.hexbinplot(x, y, ...)
panel.loess(x, y, ..., col = 'red')
},
pscale=0, varname.cex=0.7
)


Finally, it is interesting to identify some points. This task is easy with panel.link.splom. The points are selected via mouse clicks. Clicks other than left-clicks terminate the procedure.

trellis.focus('panel', 1, 1)
aranjuezDF[idx,]