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

From: Tóth Dénes <tdenes_at_cogpsyphy.hu>
Date: Fri, 18 Mar 2011 02:43:53 +0100 (CET)

Indeed, I forgot about the segments function.

with(d,plot(date,flow,type="n"))
with(d,segments(start,start.y,end,end.y,col=colour))

> Hi,
>
> because each colour is defined on non-consecutive points, you'll
> probably need to cut the intervals to define segments around each
> point. One approach might be the following,
>
> d = transform(data, start = date - c(0, diff(date)/2), end = date +
> c(0, diff(date)/2) )
> d$start.y = approx(d$date, d$flow, d$start)$y
> d$end.y = approx(d$date, d$flow, d$end)$y
> library(ggplot2)
> ggplot(d) + geom_segment(aes(x=start,y=start.y, xend=end, yend=end.y,
> colour=levels))
>
> HTH,
>
> baptiste
>
> On 18 March 2011 11:33, Pamela Allen <pallen_at_hatfieldgroup.com> wrote:
>>
>>
>>
>>
>> Hi All,
>>
>>
>>
>> I'm trying to plot data that is a time series of flows that are
>> associated
>> with a specific level, and I would like each level to represent a colour
>> in a line plot.  Here is some data that approximates what I'm using:
>>
>>
>>
>> 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)
>>
>>
>>
>> the "levels" column represents the levels of flow.  What I've done so
>> far
>> is to plot this data using coloured points corresponding with each flow
>> level:
>>
>>
>>
>> colour=ifelse(data$levels=="high","red",
>>
>>                ifelse(data$levels=="med","green",
>>
>>                ifelse(data$levels=="low","blue","")))
>>
>> plot(date, flow, col=colour)
>>
>>
>>
>> What I would like to do instead is to plot the line of this data, not
>> the
>> points.  i.e.,
>>
>> plot(date, flow, type="l")
>>
>>
>>
>> But I would like the colour of the line to change with each level, i.e.,
>>
>> plot(date, flow, type="l", col=colour)
>>
>>
>>
>> But this doesn't work because the line is continuous and the colours are
>> discrete.  I looked into using clipplot, but I'm not sure how I would
>> specify limits that would give different sections of the line correct
>> colours.  Does anyone know of a way to draw a line with different

>> colours?
>> One way I thought of was to plot each level of flow separately and then
>> build the plot up, i.e.,
>>
>> plot(data$date[data$levels=="high"], data$flow[data$levels=="high"],
>> col="red", type="l")
>>
>> lines(data$date[data$levels=="med"], data$flow[data$levels=="med"],
>> col="green", type="l")
>>
>> lines(data$date[data$levels=="low"], data$flow[data$levels=="low"],
>> col="blue", type="l")
>>
>>
>>
>> But the line fills in data gaps, so this doesn't work.
>>
>>
>>
>> Any help would be much appreciated!  Thank you.
>>
>>
>>
>> -Pam Allen
>>
>> pallen_at_hatfieldgroup.com
>>
>>
>>
>>
>>
>>
>> ______________________________________________
>> R-help_at_r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>>
>
> ______________________________________________
> R-help_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Fri 18 Mar 2011 - 01:48:17 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 18 Mar 2011 - 02:10:23 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive