[R] Overlaying 2D kernel density plots on scatterplot matrix

Karl Ove Hufthammer karloh at mi.uib.no
Mon Jun 26 19:57:37 CEST 2006


Michael Hopkins skreiv:

> The kernel density plots don¹t have to be very sophisticated i.e.
> default settings and greyscale are fine ­ we can work on details
> later.  What has stumped us so far is how you ‘attach’ the kernel
> density results to the scatterplot results and then overlay them.
> 
> Any ideas, links or code gratefully received.

Here’s my suggestion¹:

library(lattice)
library(MASS)

splom(~iris[1:4], panel=function(x,y)
{
   xy=kde2d(x,y)
   xy.tr=con2tr(xy)
   panel.contourplot(xy.tr$x, xy.tr$y, xy.tr$z,
                     subscripts=seq(nrow(xy.tr)),
                     contour=TRUE, region=FALSE)
   panel.xyplot(x,y)
}
)

¹ Which is basically Richard M. Heiberger’s solution to a similar
  query I had on this list earlier about a month ago:
  http://tolstoy.newcastle.edu.au/R/help/06/05/27184.html

-- 
Karl Ove Hufthammer



More information about the R-help mailing list