How can I create a scatterplot with a regression line or other types of lines in R?

How can I create a scatterplot with a regression line or other types of lines in R?

Creating a scatterplot with a regression line or other types of lines in R can be done by using the “plot” function and specifying the type of line desired in the “type” argument. This will generate a scatterplot with the specified line overlaid on the plot. Additionally, the “abline” function can be used to add a regression line to the plot, with options to customize the line’s slope and intercept. This allows for easy visualization and analysis of the relationship between two variables in a dataset.

How can I do a scatterplot with regression line or any other lines? | R FAQ

R makes it very easy to create a scatterplot and regression line using an lm
object created by lm function. We will illustrate this using the hsb2
data file.

hsb2<-read.table("https://stats.idre.ucla.edu/stat/data/hsb2.csv", sep=",", header=T)
head(hsb2)

   id female  race    ses schtyp     prog read write math science socst
1  70   male white    low public  general   57    52   41      47    57
2 121 female white middle public vocation   68    59   53      63    61
3  86   male white   high public  general   44    33   54      58    31
4 141   male white   high public vocation   63    44   47      53    56
5 172   male white middle public academic   47    52   57      53    61
6 113   male white middle public academic   44    52   51      63    61

Here we can make a scatterplot of the variables write with read.

	reg1 <- lm(write~read,data=hsb2) 
	summary(reg1)
	
	with(hsb2,plot(read, write))
	abline(reg1)

Image scatte1

The abline function is actually very powerful. We can add any
arbitrary lines using this function. For example, we can add a horizontal
line at write = 45 as follows.

	with(hsb2, plot(read, write))
	abline(h=45)

Image scatte2

Here is another example where we add a line of 45 degree angle passing
through the origin. In this type of syntax, the first parameter is the
intercept and the second one the slope.

	with(hsb2,plot(read, write))
	abline(0, 1)
Image scatte3

Cite this article

stats writer (2024). How can I create a scatterplot with a regression line or other types of lines in R?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-create-a-scatterplot-with-a-regression-line-or-other-types-of-lines-in-r/

stats writer. "How can I create a scatterplot with a regression line or other types of lines in R?." PSYCHOLOGICAL SCALES, 30 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-create-a-scatterplot-with-a-regression-line-or-other-types-of-lines-in-r/.

stats writer. "How can I create a scatterplot with a regression line or other types of lines in R?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-create-a-scatterplot-with-a-regression-line-or-other-types-of-lines-in-r/.

stats writer (2024) 'How can I create a scatterplot with a regression line or other types of lines in R?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-create-a-scatterplot-with-a-regression-line-or-other-types-of-lines-in-r/.

[1] stats writer, "How can I create a scatterplot with a regression line or other types of lines in R?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I create a scatterplot with a regression line or other types of lines in R?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top