# [R] Rainbow in loop

WRAY NICHOLAS nicholas.wray at ntlworld.com
Thu Jun 8 14:20:18 CEST 2017

```Hi R folk  I have a distance time graph for a locomotive and at various times
different events occur on board the loco.  I want to put a vertical line on the
speed time graph for each event, but I want to colour each different kind of
event differently to see visually whether there's any pattern to these events
happening.  I could just create a vector of colours and use abline which is easy
obviously, but there's a different number of events for each loco and what I
would like to do is to use rainbow to create a new palette for each graph

To illustrate I have some model code (made-up and v simplified)  Real times are
not necessarily whole numbers so  there's  not a one to one correspondence
between times and index of time elements

sec.time<-seq(0,100)

distance<-c(rep(0,
10),rep(1,5),rep(2,20),rep(3,10),rep(4,20),rep(5,5),rep(6,31))
plot(sec.time,distance,type="l")
horntime<-c(7,23,52,67,81,90)
wipertime<-c(4,18,34,47,62,78,89)
calltime<-c(27,58,93)

abline(v=sec.time[horntime], col="red")
abline(v=sec.time[wipertime], col="blue")
abline(v=sec.time[calltime], col="green")

what I want, in this case as there are three events, is to have horn in red,
wiper in blue and call in green using rainbow with 3.  The problem is that I
can't see how to call rainbow using a sequence (for loop doesn't work) and
putting horn/wiper/call as vectors in a list doesn't work as it's too recursive.
I suppose that I could put horn/wiper/call etc into a matrix of width the
longest vector and fill the other spaces with dummy -1 but this seems a bit
inelegant and also in principle I'd like to be able to call rainbow in the
desired way as I have to prepare various graphs of different aspects of loco
behaviour

If anyone has any ideas I'd be v grateful

Thanks Nick
[[alternative HTML version deleted]]

```

More information about the R-help mailing list