[R] plot with different color

Jim Lemon jim at bitwrit.com.au
Tue Jun 6 02:25:33 CEST 2006


array chip wrote:
> Hi how can I plot a series of number as a line, but
> with lines above a threshould as one color, and with
> lines below the threshold as another color. for
> example, a numeric vector: rnorm(1:100), and plot
> these numbers in the original order, but lines above
> the horizontal line at 0 are in red, and lines below
> in blue?
> 
Hi array (or is it, Hi chip?),

Is this what you want?

x<-rnorm(100)
x11(width=7,height=7)
oldpar<-par(no.readonly=TRUE)
xmai<-par("mai")
par(yaxs="i")
plot(x,type="n",ylim=c(-3,3),xlab="Observation",ylab="Value")
par(mai=c(3.5,xmai[2:4]),yaxs="i",new=TRUE)
plot(x,type="l",col="blue",ylim=c(0,3),axes=FALSE,xlab="",ylab="")
par(mai=c(xmai[1:2],3.5,xmai[4]),new=TRUE)
plot(x,type="l",col="red",ylim=c(-3,0),axes=FALSE,xlab="",ylab="")
par(oldpar)

This _is_ an ugly kludge.

Jim



More information about the R-help mailing list