|
|
|
||||
|
|
|||||
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("http://www.ats.ucla.edu/stat/R/notes/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) par(cex=.8) plot(read, write) abline(reg1)
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.
plot(read, write) abline(h=45)

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.
plot(read, write) abline(0, 1)
UCLA Researchers are invited to our Statistical Consulting Services
We recommend others to our list of Other Resources for Statistical Computing Help
These pages are Copyrighted (c) by UCLA Academic Technology Services