Skip to content

Instantly share code, notes, and snippets.

@bjurban
Created September 3, 2015 14:01
Show Gist options
  • Save bjurban/6fed941516250d947357 to your computer and use it in GitHub Desktop.
Save bjurban/6fed941516250d947357 to your computer and use it in GitHub Desktop.
plot bivariate kernel density plot
library(lattice)
library(MASS)
splom(iris, upper.panel = function(x, y, subscripts, ...) {
if(is.numeric(x) & is.numeric(y)){
# calculate bivariate kernel density
v <- current.panel.limits() # allows full bleed by setting limits explicitly
f1 <- kde2d(x = x, y = y, n = 50, lims = c(v$xlim, v$ylim))
f <- data.frame(x = f1$x, y = rep(f1$y, each = length(f1$x)),
z = as.vector(f1$z))
panel.contourplot(f$x, f$y, f$z, contour = TRUE,
subscripts = 1:dim(f)[1], ...)
}
panel.xyplot(x, y, subscripts = subscripts, ...)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment