# Bivariate Kernel Density plot

Suppose that I have two variables, $X$ and $Y$ that I want to correlate, i.e. in the manner of a scatterplot of $(X_i,Y_i)$. But I also want to show the density of each variable. A scatterplot basically does this, but I wanted a more organic display in the fashion of a KDE.

How can I make a plot where $\mathrm{dens}(x,y) = K(X,x,Y,y)$? I imagine plotting $\mathrm{dens}(x,y)$ either as a third variable with a 3D design if the distributions are simple or as pixel color intensity/value.

A sample kernel in this example would be the product of the KDEs for each respective variable.

A motivating example would be to observe bivariate "islands" in heteroscedastic data.

• Something like this? en.wikipedia.org/wiki/File:MultivariateNormal.png – bdeonovic Oct 15 '14 at 3:18
• @Benjamin Not really. Picture a scatterplot except with a properly sized 2D normal distribution superimposed on each point. (shading is fine) – Simon Kuang Oct 15 '14 at 3:28
• ggplot2 can do this using stat_density2d: google.com/… – ziggystar Oct 15 '14 at 7:30
• Perhaps you are looking for something like the image I posted in an answer at stats.stackexchange.com/a/114238, which superimposes the scatterplot over a KDE of the bivariate data with two more (discrete) variables symbolized by color and symbol type. The post includes R code to produce the figure. – whuber Oct 15 '14 at 18:52