The examples here are created using R 2.6.2 for Windows XP. It uses the standard graphics package that R loads automatically when it starts.
The examples use hsb2 data set that can be downloaded as follows.
hsb2 <- read.table('http://www.ats.ucla.edu/stat/r/modules/hsb2.csv', header=T, sep=",")
attach(hsb2)
![]() |
plot(math, write) |
![]() |
plot(math, write) abline(lsfit(write, math)) #alternatively plot(math, write) fit<-lm(write ~ math) abline(fit) |
![]() |
# pch for plotting characters, or symbol to use # see help(points) for more information on pch for hsb2_female<-hsb2[female==1,] hsb2_male<-hsb2[female==0,] with(hsb2_female, plot(math, write, pch=20, col="darkred")) with(hsb2_male, points(math, write, pch=22, col="darkblue")) # alternatively
math_male<-hsb2$math[female==0]
write_male<-hsb2$write[female==0]
math_female<-hsb2$math[female==1]
write_female<-hsb2$write[female==1]
plot(math_female, write_female, type="p",
pch=20, col="darkred")
points(math_male, write_male, pch=22, col="darkblue")
|
![]() |
# adding legend hsb2_female<-hsb2[female==1,]
hsb2_male<-hsb2[female==0,]
with(hsb2_female, plot(math, write, pch=20,
col="darkred", ylim=c(25, 70)))
with(hsb2_male, points(math, write, pch=22,
col="darkblue"))
legend(65, 35, c("female", "male"), pch=c(20, 22),
cex=.8, col=c("darkred", "darkblue"))
|
![]() |
math_male<-hsb2$math[female==0] write_male<-hsb2$write[female==0] math_female<-hsb2$math[female==1] write_female<-hsb2$write[female==1] plot(math_female, write_female, type="p", pch=20,
col="darkred", ylim=c(25, 70))
points(math_male, write_male, pch=22, col="darkblue")
abline(lsfit(write_female, math_female), col="darkred")
abline(lsfit(write_male, math_male), col="darkblue")
legend(65, 35, c("female", "male"), pch=c(20, 22),
cex=.8, col=c("darkred", "darkblue"))
|
The content of this web site should not be construed as an endorsement of any particular web site, book, or software product by the University of California.