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

David Winsemius dwinsemius at comcast.net
Fri Mar 25 21:35:37 CET 2011


On Mar 25, 2011, at 3:23 PM, Pam Allen wrote:

> 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)

Don't use `data` as a name. It's a function.
>
> 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"?

  with(dat,plot(date,flow,type="n"))
  with(dat, segments(date[1:299],flow[1:299],  # starting points for  
segments
                     date[2:300],flow[2:300],  # ending points offset  
by 1
             col=c("red","green","blue")[levels[1:299]]))

I'mn ot sure the colors line up because you didn't define your factor  
in a manner that was properly ordered;
 > str(dat)
'data.frame':	300 obs. of  3 variables:
  $ date  : int  1 2 3 4 5 6 7 8 9 10 ...
  $ flow  : num  0.118 0.235 0.348 0.457 0.559 ...
  $ levels: Factor w/ 3 levels "high","low","med": 1 3 2 1 3 2 1 3 2  
1 ...

Better would have been to relevel after creating `levels` AND NOT USE  
`levels` as a name. It's an argument name in factor:

  levels=factor(levels, levels=c("high","med","low") )

Now you know what order they will be handled when used as an index.

-- 

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list