[R] Help with plotting a line that is multicoloured based on levels of a factor

Pam Allen allen_pam at hotmail.com
Fri Mar 25 20:23:22 CET 2011


Hello Baptiste and others, 

I tried your example with my dataset, and for a few days I thought it worked
for me.  But I realized yesterday that the result wasn't quite what I hoped
for.  In my actual data the flows aren't perfectly sinusoidal, and I used a
series of ifelse queries to code the flows into their different categories
(i.e., extremely high, high, low, extremely low).  Your solution almost
worked, except that some flows are coloured incorrectly.  I think the issue
lies in the use of the "transform" or "approx" functions.  I tried to
understand what they do, but I wasn't able to figure it out.

Is there a way to use the exact data set, i.e.:
date=c(1:300)
flow=sin(2*pi/53*c(1:300))
levels=c(rep(c("high","med","low"),100))
data=cbind.data.frame(date, flow, levels)

With the following colours:
colour=ifelse(data$levels=="high","red",
      ifelse(data$levels=="med","green",
      ifelse(data$levels=="low","blue","")))  

And plot a line without having to create new data, i.e. "d"?

Thank you.

-Pam

allen_pam at hotmail.com

--
View this message in context: http://r.789695.n4.nabble.com/Help-with-plotting-a-line-that-is-multicoloured-based-on-levels-of-a-factor-tp3385857p3406199.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list