useR's I am trying to color the points on a scatter plot (code below) with two colors. Red for values 0.5 -1.0 and blue for 0.0 - .49. Does anyone know a easy way to do this? x <- runif(100, 0, 1) y <- runif(100, 0, 1) plot(y ~ x, pch=16) Thanks, dxc13 -- View this message in context: http://www.nabble.com/color-ranges-on-a-2D-plot-tp14893457p14893457.html Sent from the R help mailing list archive at Nabble.com.
You did not say what variable you want to be in the ranges to determine the colors, but try any of these to see if they do what you want:> x <- runif(100) > y <- runif(100) > z <- runif(100) > plot(x,y, pch=16, col=ifelse( x < 0.5, 'blue','red')) > plot(x,y, pch=16, col=ifelse( y < 0.5, 'blue','red')) > plot(x,y, pch=16, col=ifelse( z < 0.5, 'blue','red')) > > library(TeachingDemos) > y[1] <- 0.5 > plot(x,y, pch=16, col='red,) > clipplot( points(x,y,pch=16,col='blue'), ylim=c(0,0.5) )The last one is probably overkill, but can give a nice effect with larger cex values. If none of those work for you, restate the question with better detail. Hope this helps, -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org (801) 408-8111> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of dxc13 > Sent: Wednesday, January 16, 2008 1:35 PM > To: r-help at r-project.org > Subject: [R] color ranges on a 2D plot > > > useR's > > I am trying to color the points on a scatter plot (code > below) with two colors. Red for values 0.5 -1.0 and blue for > 0.0 - .49. Does anyone know a easy way to do this? > > x <- runif(100, 0, 1) > y <- runif(100, 0, 1) > plot(y ~ x, pch=16) > > Thanks, > dxc13 > -- > View this message in context: > http://www.nabble.com/color-ranges-on-a-2D-plot-tp14893457p148 > 93457.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org 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. >
dxc13 wrote:> useR's > > I am trying to color the points on a scatter plot (code below) with two > colors. Red for values 0.5 -1.0 and blue for 0.0 - .49. Does anyone know a > easy way to do this? > > x<- runif(100, 0, 1) > y<- runif(100, 0, 1) > plot(y ~ x, pch=16) > > Thanks, > dxc13You did not specify if this is for just the 'x' values, the 'y' values, or both. If the latter, more than two colors might make sense. For just the 'x' values and two colors, the easiest might be something like: plot(x, y, col = ifelse(x >= 0.5, "red", "blue")) Change 'x' to 'y' for the same on 'y'. Alternatively, you could do something like the following for coloring based upon both 'x' and 'y': cols <- character(length(x)) xgt <- x >= 0.5 ygt <- y >= 0.5 cols[which(xgt & ygt)] <- "black" cols[which(!xgt & ygt)] <- "red" cols[which(xgt & !ygt)] <- "blue" cols[which(!xgt & !ygt)] <- "green" plot(x, y, col = cols, pch = 16) HTH, Marc Schwartz
There must be a better way but this will do it for you. x <- runif(100, 0, 1) y <- runif(100, 0, 1) z <- data.frame(x,y) plot(subset(z, z$y >=.5), col="red", ylim=c(min(z$y), max(z$y)), pch=16) points(subset(z, z$y <=.49), col="blue", pch=15) --- dxc13 <dxc13 at health.state.ny.us> wrote:> > useR's > > I am trying to color the points on a scatter plot > (code below) with two > colors. Red for values 0.5 -1.0 and blue for 0.0 - > .49. Does anyone know a > easy way to do this? > > x <- runif(100, 0, 1) > y <- runif(100, 0, 1) > plot(y ~ x, pch=16) > > Thanks, > dxc13 > -- > View this message in context: >http://www.nabble.com/color-ranges-on-a-2D-plot-tp14893457p14893457.html> Sent from the R help mailing list archive at > Nabble.com. > > ______________________________________________ > R-help at r-project.org 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. >