简体   繁体   中英

coloring of data points in R

here i have generated two random variable and sorted it and fit them into linear logistic regression.then colored it blue and red but the problem is i cannot set data points under the fitted line as red and values above ones as blue. Here is my code, any help will be appreciated.

A<-rnorm(100)
B<-runif(100)

r<-sort(A)
s<-sort(B)

reg<- lm(r ~ s)

plot(reg,which=1)
plot(predict(reg),residuals(reg))
plot(predict(reg),residuals(reg),col=c("blue","red"))
abline(h=0,lty=2,col="grey")
lines(lowess(predict(reg),residuals(reg)),col="black",lwd=2)
lines(lowess(s[r==0],residuals(reg)[r==0]),col="red")
lines(lowess(s[r==1],residuals(reg)[r==1]),col="blue")

See graph here: the colouring seems to be difficult to assigned

Does this do what you want ?

plot(predict(reg),residuals(reg),col=ifelse(residuals(reg)<0,"blue","red"))

I just test whether the residual is larger than 0 or not. The main idea is to create a vector of colour of the same length of your data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM