I am grateful that we live in an age in which individuals like Yihui Xie make things that help others make things. Aside from his extraordinary work with knitr, I am enjoying the ability to make any kind of animation I want with his animation package. My introductory stats lectures are less boring when I can make simple graphs move:

Code:

library(Cairo) library(animation) library(mvtnorm) n <- 1000 #Sample size d <- rmvnorm(n, mean = c(0, 0), sigma = diag(2)) #Uncorrelated z-scores colnames(d) <- c("X", "Y") #Variable names m <- c(100, 100) #Variable means s <- c(15, 15) #Variable standard deviations cnt <- 1000 #Counter variable for (i in c(-0.9999, -0.999, seq(-0.995, 0.995, 0.005), 0.999, 0.9999, 0.999, seq(0.995, -0.995, -0.005), -0.999)) { Cairo(file = paste0("S", cnt, ".png"), bg = "white", width = 700, height = 700) #Save to file using Cairo device cnt <- cnt + 1 #Increment counter rho <- i #Correlation coefficient XY <- matrix(rep(1, 2 * n), nrow = n) %*% diag(m) + d %*% chol(matrix(c(1, rho, rho, 1), nrow = 2)) %*% diag(s) #Make uncorrelated data become correlated plot(XY, pch = 16, col = rgb(0, 0.12, 0.88, alpha = 0.3), ylab = "Y", xlab = "X", xlim = c(40, 160), ylim = c(40, 160), axes = F, main = bquote(rho == .(format(round(rho, 2), nsmall = 2)))) #plot data lines(c(40, 160), (c(40, 160) - 100) * rho + 100, col = "firebrick2") #Plot regression line axis(1) #Plot X axis axis(2) #Plot Y axis dev.off() #Finish plotting } ani.options(convert = shQuote("C:/Program Files/ImageMagick-6.8.8-Q16/convert.exe"), ani.width = 700, ani.height = 800, interval = 0.05, ani.dev = "png", ani.type = "png") #Animation options im.convert("S*.png", output = "CorrelationAnimation.gif", extra.opts = "-dispose Background", clean = T) #Make animated .gif