Last active
August 24, 2016 05:12
-
-
Save soumyaray/ba40f0f2c9b0bb98a07e6efa9f2d8d60 to your computer and use it in GitHub Desktop.
Create live x-y scatter plot with correlation and regression line
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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