Hello, I would like to make a plot (preferably lines, but points will do too), where the line segment changes color depending on the value of the y-axis. For example, let's suppose the y-axis range is from -1 to 1. Points close to -1 would be colored blue, while points close to 1 will be colored red. Points in between will be varying degrees of blue/red depending on how close they are to -1 or +1. If the above is not possible, would it be possible to set a decision boundary on the plot, where any point above this boundary will be a particular color, and any point below will be a different color? Any suggestions as to how I might accomplish this would be much appreciated. Regards, Wee-Jin p.s. The data set I'm talking about has about 20,000 points.
Wee-Jin Goh wrote:> Hello, > > I would like to make a plot (preferably lines, but points will do > too), where the line segment changes color depending on the value of > the y-axis. For example, let's suppose the y-axis range is from -1 to > 1. Points close to -1 would be colored blue, while points close to 1 > will be colored red. Points in between will be varying degrees of > blue/red depending on how close they are to -1 or +1. > > If the above is not possible, would it be possible to set a decision > boundary on the plot, where any point above this boundary will be a > particular color, and any point below will be a different color? > > Any suggestions as to how I might accomplish this would be much > appreciated. >Have a look at the examples for color.scale and color.scale.lines in the plotrix package. Jim
I did this with points by supplying the vector in the col= argument. That is, plot(x=x.vector,y=y.vector,col=col.table[y.vector*100+100],pch=16) It is presumed, that col.table contains 200 colors, and y.vector is in (-1,1) range. You can write your own expression to define the color for particular value, depending on your task. pch=16 - afair, it is a big solid square (may be wrong) colors in table should be as character hexadecimal strings of the form "#rrggbb". Did you define the palette? Some cute color tables could be grabbed from here http://www.ncl.ucar.edu/Document/Graphics/color_table_gallery.shtml Also, the GIMP (http://www.gimp.org) can generate palettes and even saves them in the text files, which can be very easily imported in R.
Others have given some fairly basic solutions, but if you want the lines to change color along their length instead of each segment being a constant color, then there are a couple of other options. One is to create the plot using a solid color, then use image manipulation software like imagemagick or gimp to replace the solid color with a gradient. Another option is to use the clipplot function in the most recent version of the TeachingDemos Package (just updated on CRAN). An example would be:> library(TeachingDemos) > x <- sort(runif(100)) > y <- rnorm(100) > plot(x,y,type='b') > > tmp <- seq(par('usr')[3],par('usr')[4], length=11) > tmp2 <- topo.colors(10) # replace with your color gradient > for(i in 1:10) {+ clipplot( points(x,y,type='b', col=tmp2[i]), ylim= tmp[ c(i,i+1) ] ) + }>Hope this helps, -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at intermountainmail.org (801) 408-8111 -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Wee-Jin Goh Sent: Thursday, November 02, 2006 1:55 AM To: r-help at stat.math.ethz.ch Subject: [R] Complex plotting problem Hello, I would like to make a plot (preferably lines, but points will do too), where the line segment changes color depending on the value of the y-axis. For example, let's suppose the y-axis range is from -1 to 1. Points close to -1 would be colored blue, while points close to 1 will be colored red. Points in between will be varying degrees of blue/red depending on how close they are to -1 or +1. If the above is not possible, would it be possible to set a decision boundary on the plot, where any point above this boundary will be a particular color, and any point below will be a different color? Any suggestions as to how I might accomplish this would be much appreciated. Regards, Wee-Jin p.s. The data set I'm talking about has about 20,000 points. ______________________________________________ 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.