[R] Plot segments with different colors

Ken Knoblauch knoblauch at lyon.inserm.fr
Tue Nov 13 10:20:21 CET 2007


Dimitris Rizopoulos <dimitris.rizopoulos <at> med.kuleuven.be> writes:
> 
> the 'col' argument of plot() may also be a vector, e.g.,
> 
> x <- sort(rnorm(15))
> y <- rnorm(15)
> plot(x, y,
>     col = rep(c("red", "blue", "magenta"), c(5, 6, 4)))

> Dimitris
> 
> ----
> Dimitris Rizopoulos

> 
> ----- Original Message ----- 
> From: "mysimbaa" <adel.tekari <at> sisltd.ch>
> To: <r-help <at> r-project.org>
> Sent: Tuesday, November 13, 2007 9:46 AM
> Subject: [R] Plot segments with different colors
> 
> >
> > Hello Everybody,
> > I'm trying to plot(x,y) using different colors.
> > I have to cut (x,y) into sub intervals.
> > And I want to plot this sub intervals using colors. But infortunally 
> > I don't
> > know how to do this with R.
> > Can any body help me please?
> >
> > My code is looking like :
> > x<-c(tvar[1:t0],tvar[t0:(ts)],tvar[ts:n])
> > F<-c(var[1:t0],var[t0:(ts)],var[ts:n])
> > plot(x,F,xlab="Zeit [s]",ylab="Variation [%]",col = "red",type="l")
That works great for points but if you want to vary the properties of
segments of lines, then you need the segments() function.

x <- 1:3
y <- c(1, 5, 2)
plot(x, y)
segments(x[1:2], y[1:2], x[2:3], y[2:3], 
col = c("blue", "red"), lwd = c(1, 4), 
lty = 1:2)

> > Thanks
> > -- 

best,

Ken



More information about the R-help mailing list