What you're looking for is ggplot2::scale_colour_gradient2()
. Since you didn't provide any example data (which I highly recommend in the future; it encourages answers and helps answerers tailor their responses to your actual data structure), I concocted the following simple example:
library(ggplot2)
set.seed(123)
n <- 1000
corrs <- seq(-0.9, 0.9, length.out = 10)
vals <- matrix(0, nrow = 0, ncol = 2)
for ( corr in corrs ) {
tmp <- mvtnorm::rmvnorm(n/10, sigma = matrix(c(1, corr, corr, 1), nrow = 2))
# print(cor(tmp)) # If you want to do QA
vals <- rbind(vals, tmp)
}
df <- data.frame(var1 = vals[ , 1], var2 = vals[ , 2],
corr = rep(corrs, each = n/10))
ggplot(df, aes(x = var1, y = var2, colour = corr)) +
geom_point(shape = 1) +
scale_colour_gradient2(low = "darkred", mid = "gray", high = "darkgreen")
