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.

  • $\begingroup$ Something like this? en.wikipedia.org/wiki/File:MultivariateNormal.png $\endgroup$ – bdeonovic Oct 15 '14 at 3:18
  • $\begingroup$ @Benjamin Not really. Picture a scatterplot except with a properly sized 2D normal distribution superimposed on each point. (shading is fine) $\endgroup$ – Simon Kuang Oct 15 '14 at 3:28
  • $\begingroup$ ggplot2 can do this using stat_density2d: google.com/… $\endgroup$ – ziggystar Oct 15 '14 at 7:30
  • $\begingroup$ 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. $\endgroup$ – whuber Oct 15 '14 at 18:52

Package ggplot2 can do this using stat_density2d, see the documentation.

enter image description here

  • $\begingroup$ Also see the overlaid contours on the scatterplot in the documentation link provided above! $\endgroup$ – Dianne Cook Oct 15 '14 at 16:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.