Dear All, I am using the following commands to do the scatter plot of two vectors, say X and Y. plot(X,Y, col="blue") abline(a=1,b=1, col="red") abline(a=-1,b=1, col="green") I would like to split the scatter plot into 3 part with 3 different colors: (i) points lies between 2 lines, (ii) points above line 1, and (iii) points below line 2. I am struggling to do this. I would greatly appreciate any help in doing this. Thanks in advance. Kind regards, Ezhil
Quoting A Ezhil <ezhil02 at yahoo.com>:> Dear All, > > I am using the following commands to do the scatter > plot of two vectors, say X and Y. > > plot(X,Y, col="blue") > abline(a=1,b=1, col="red") > abline(a=-1,b=1, col="green") > > I would like to split the scatter plot into 3 part > with 3 different colors: (i) points lies between 2 > lines, (ii) points above line 1, and (iii) points > below line 2. I am struggling to do this. I would > greatly appreciate any help in doing this. > > Thanks in advance. > > Kind regards, > Ezhilcheck ?points 'points' allows you to plot points using different plotting parameters (type, size, colours...). You can subset the points in your scatter plot into three groups, and use 'points' on each group separately. For instance: x<-sample(c(-10:10),size=21) y<-sample(c(-10:10),size=21) plot(x,y, col="blue",pch=16) abline(a=1,b=1, col="red") abline(a=-1,b=1, col="green") # then find the red and green groups: reds<-which(y>x+1) greens<-which(x>y+1) points(x[reds],y[reds], col="red",pch=16) points(x[greens],y[greens], col="green",pch=16) you could also choose to not plot anything at first (use parameter type="n"), and define not just the red and green groups, but also teh blue ones too. You may need to do this if you want to use different plotting characters or sizes and overplotting doesn't look good. Jose -- Dr. Jose I. de las Heras Email: J.delasHeras at ed.ac.uk The Wellcome Trust Centre for Cell Biology Phone: +44 (0)131 6513374 Institute for Cell & Molecular Biology Fax: +44 (0)131 6507360 Swann Building, Mayfield Road University of Edinburgh Edinburgh EH9 3JR UK
Here is a recent posting by Petr Klasterecky that does not seem to be on the archive yet that may help. ---------------------------------------------- What do you mean by background? Maybe this is enough: plot(seq(-3,3,.01), dnorm(seq(-3,3,.01)), type="n", xlab="x", ylab="f(x)", main="Normal density") polygon(x=c(-4,0,0,-4), y=c(-1,-1,.5,.5), col="red") polygon(x=c(4,0,0,4), y=c(-1,-1,.5,.5), col="blue") lines(seq(-3,3,.01), dnorm(seq(-3,3,.01)), type="l", lwd=2) Play a little bit with the polygon margins to get what you need. You can even generate them automatically based on your data. Petr ----------------------------------------------- --- A Ezhil <ezhil02 at yahoo.com> wrote:> Dear All, > > I am using the following commands to do the scatter > plot of two vectors, say X and Y. > > plot(X,Y, col="blue") > abline(a=1,b=1, col="red") > abline(a=-1,b=1, col="green") > > I would like to split the scatter plot into 3 part > with 3 different colors: (i) points lies between 2 > lines, (ii) points above line 1, and (iii) points > below line 2. I am struggling to do this. I would > greatly appreciate any help in doing this. > > Thanks in advance. > > Kind regards, > Ezhil > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, > reproducible code. >