# How to plot points, regression line and residuals

June 16, 2011
By

(This article was first published on Statistic on aiR, and kindly contributed to R-bloggers)

```x <- c(173, 169, 176, 166, 161, 164, 160, 158, 180, 187)y <- c(80, 68, 72, 75, 70, 65, 62, 60, 85, 92) # plot scatterplot and the regression linemod1 <- lm(y ~ x)plot(x, y, xlim=c(min(x)-5, max(x)+5), ylim=c(min(y)-10, max(y)+10))abline(mod1, lwd=2)```

``` # calculate residuals and predicted valuesres <- signif(residuals(mod1), 5)pre <- predict(mod1) # plot distances between points and the regression linesegments(x, y, x, pre, col="red") ```

`# add labels (res values) to pointslibrary(calibrate)textxy(x, y, res, cx=0.7)`

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...