Hello,
Thanks again.
But something wrong with the subset after lm
It is not about drawing plot many times but coding points or graphic objects by some factor. In your case sex.
Instead of
boyline<-lm(body_weight ~ body_length, boy)
use collective data frame together and subset only one sex.
But an error showed, saying:
error in xj[i] : useless type of 'list'
Please kindly help and thanks again.
BTW, the code below might need update as well.
Elaine
In case of more than two sexes (some SF authors mentioned it is possible) you can use simple loop.
for (i in 1:2)) {
subs <- together$sex==levels(together$sex)[i]
line<-lm(body_weight ~ body_length, data=together, subset=subs)
abline(line, col=c("firebrick3","saddlebrown")[i],lwd=2)
}
The original code with advice by Petr
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
boy<-read.csv("H:/boy_data.csv",header=T)
girl<-read.csv("H:/girl_data.csv",header=T)
together <- rbind(boy, girl)
together$sex <- factor(rep(c("boy", "girl"), c(8,7)))
plot(together$body_length, together$body_weight, ....,
col=c("firebrick3","saddlebrown")[as.numeric(together$sex)], ....)
boyline<-lm(body_weight ~ body_length, data=together, subset(sex=="boy"))
abline(boyline,col="firebrick3",lwd=2)