Search code examples
rggplot2covariancelattice

How to reproduce this graphical explanation (a scatter plot) of how covariance works?


I found this graphical intuitive explanation of covariance: 32 binormal points drawn from distributions with the given covariances, ordered from most negative (bluest) to most positive (reddest)

enter image description here

The whole material can be found at: https://stats.stackexchange.com/questions/18058/how-would-you-explain-covariance-to-someone-who-understands-only-the-mean

I would like to recreate this sort of graphical illustration in R, but I'm not sufficiently familiar with R's plotting tools. I don't even know where to start in order to get those colored rectangles between each pair of data points, let alone make them semi-transparent.

I think this could make a very efficient teaching tool.


Solution

  • The cor.rect.plot function in the TeachingDemos package makes plots similar to what is shown. You can modify the code for the function to make the plot even more similar if you desire.