Skip to content

Instantly share code, notes, and snippets.

@soumyaray
Last active August 24, 2016 05:12
Show Gist options
  • Save soumyaray/ba40f0f2c9b0bb98a07e6efa9f2d8d60 to your computer and use it in GitHub Desktop.
Save soumyaray/ba40f0f2c9b0bb98a07e6efa9f2d8d60 to your computer and use it in GitHub Desktop.
Create live x-y scatter plot with correlation and regression line
scatter_play <- function() {
# create a blank plot (use and then remove a fake point)
points = data.frame(x=c(-99), y=c(-99))
plot(points, xlim=c(-5,50), ylim=c(-5,50))
points = data.frame(x=c(), y=c())
cat("Click on plot to add points; [Esc] to quit")
repeat {
location <- locator(1)
if (is.null(location)) break
points <- rbind(points, location)
plot(points, xlim=c(-5,50), ylim=c(-5,50),
pch = 16, col=rgb(0.5, 0.5, 0.5, 0.5))
if (nrow(points) < 2) next
abline(lm(points$y ~ points$x),
col=rgb(0.0, 0.0, 0.5, 0.5), lwd=2)
text(0, 48, paste("r: ", round(cor(points$x, points$y), 2)))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment