plot with different color

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

plot with different color

array chip
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?

Thanks

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: plot with different color

Gregory Snow
Here is one approach.  It uses the function clipplot which is shown
below (someday I will add this to my TeachingDemos package, the
TeachingDemos package is required).

An example of usage:

> x <- rnorm(1:100)
> plot(x, type='l',col='red')
> clipplot( lines(x, col='blue'), ylim=c(-4,0) )

Hope this helps,

The function definition is:

clipplot <- function(fun, xlim=par('usr')[1:2],
        ylim=par('usr')[3:4] ){
  old.par <- par(c('plt','xpd'))


  if( length(xlim) < 2 ) stop('xlim must be a vector with at least 2
elements')
  if( length(ylim) < 2 ) stop('ylim must be a vector with at least 2
elements')

  if( !require(TeachingDemos) ) stop('TeachingDemos package needed')

  xl <- range(xlim)
  yl <- range(ylim)

  pc <- cnvrt.coords(xl,yl)$fig

  par(plt=c(pc$x,pc$y),xpd=FALSE)

  fun

  par(old.par)
  box() # need to plot something to reset

}  
 


--
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
[hidden email]
(801) 408-8111
 

-----Original Message-----
From: [hidden email]
[mailto:[hidden email]] On Behalf Of array chip
Sent: Friday, June 02, 2006 1:10 PM
To: [hidden email]
Subject: [R] plot with different color

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?

Thanks

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: plot with different color

P Ehlers
Greg,

It might be better to use ylim = c(min(x), 0)
in place of ylim = c(-4, 0). I don't think plt can take
negative values.

Peter Ehlers

Greg Snow wrote:

> Here is one approach.  It uses the function clipplot which is shown
> below (someday I will add this to my TeachingDemos package, the
> TeachingDemos package is required).
>
> An example of usage:
>
>
>>x <- rnorm(1:100)
>>plot(x, type='l',col='red')
>>clipplot( lines(x, col='blue'), ylim=c(-4,0) )
>
>
> Hope this helps,
>
> The function definition is:
>
> clipplot <- function(fun, xlim=par('usr')[1:2],
> ylim=par('usr')[3:4] ){
>   old.par <- par(c('plt','xpd'))
>
>
>   if( length(xlim) < 2 ) stop('xlim must be a vector with at least 2
> elements')
>   if( length(ylim) < 2 ) stop('ylim must be a vector with at least 2
> elements')
>
>   if( !require(TeachingDemos) ) stop('TeachingDemos package needed')
>
>   xl <- range(xlim)
>   yl <- range(ylim)
>
>   pc <- cnvrt.coords(xl,yl)$fig
>
>   par(plt=c(pc$x,pc$y),xpd=FALSE)
>
>   fun
>
>   par(old.par)
>   box() # need to plot something to reset
>
> }  
>  
>
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: plot with different color

Jim Lemon
In reply to this post by array chip
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

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html