Plot a visualization of the correlation using colored rectangles
correct.RdThis function creates a scatterplot of the data, then adds colored rectangles between the points and the mean of x and y to represent the idea of the correlation coefficient.
Usage
cor.rect.plot(x, y, corr = TRUE, xlab = deparse(substitute(x)),
ylab = deparse(substitute(y)), col = c("#ff000055", "#0000ff55"),
...)Arguments
- x
The
xvalue or any object that can be interpreted byxy.coords- y
The
yvalue- corr
Should the standardized axes (right and top) show the values divided by the standard deviation (TRUE, which shows correlation ideas) or not (FALSE, shows covariance idea)
- xlab
The label for the
xaxis- ylab
The label for the
yaxis- col
A vector of length 2 with the colors to use for the fill of the rectangles, the 1st value will be used for "positive" rectangles and the 2nd value will be used for the "negative" rectangles.
- ...
Possible further arguments, currently ignored
Details
This will create a scatterplot of the data, draw refrence lines at
the mean of x and the mean of y, then draw rectangles
from the mean point to the data points. The right and top axes will
show the centered (and possibly scaled if corr=TRUE) values.
The idea is that the correlation/covariance is based on summing the area of the "positive" rectangles and subtracting the sum of the areas of the "negative" rectangles (then dividing by n-1). If the positive and negative areas are about the same then the correlation/covariance is near 0, if there is more area in the positive rectangles then the correlation/covariance will be positive.
Author
Greg Snow, 538280@gmail.com
Examples
## low correlation
x <- rnorm(25)
y <- rnorm(25)
cor(x,y)
#> [1] 0.383861
cor.rect.plot(x,y)
## Positive correlation
x <- rnorm(25)
y <- x + rnorm(25,3, .5)
cor(x,y)
#> [1] 0.9218861
cor.rect.plot(x,y)
## negative correlation
x <- rnorm(25)
y <- rnorm(25,10,1.5) - x
cor(x,y)
#> [1] -0.4362058
cor.rect.plot(x,y)
## zero correlation but a definite relationship
x <- -5:5
y <- x^2
cor(x,y)
#> [1] 0
cor.rect.plot(x,y)