[R] Changing colour of continuous time-series in ggplot2

Hadley Wickham hadley at rice.edu
Tue Mar 15 16:15:56 CET 2011


You need to specify the group aesthetic - that defines how
observations are grouped into instances of a geom.

Hadley

On Tue, Mar 15, 2011 at 8:37 AM, joeP <joseph.parrott at bt.com> wrote:
> Hi,
>
> This seems like there should be a simple answer, but having spent most of
> the day trying to find it, I'm becoming less convinced and as such am asking
> it here.
>
> Here's a sub-set of my data (a data.frame in R):
>
> myDF
>                                time     value      trial
> 1   2011-03-01 01:00:00  64092  FALSE
> 2   2011-03-01 02:00:00  47863  FALSE
> 3   2011-03-01 03:00:00  43685  FALSE
> 4   2011-03-01 04:00:00  44821   TRUE
> 5   2011-03-01 05:00:00  48610   TRUE
> 6   2011-03-01 06:00:00  44856   TRUE
> 7   2011-03-01 07:00:00  55199   TRUE
> 8   2011-03-01 08:00:00  69326  FALSE
> 9   2011-03-01 09:00:00  84048  FALSE
> 10 2011-03-01 10:00:00  81341  FALSE
>
> >From this, I can plot a simple time-series in ggplot:
>
> ggplot(myDF, aes(time,value)) + geom_line()
>
> but I'd like to change the colour of the line based on whether the trial
> value is TRUE or FALSE, so I try:
>
> ggplot(myDF, aes(time,value)) + geom_line(aes(colour=trial))
>
> but this draws a line from the value on row 3 to that on row 8 (essentially
> plotting TRUE and FALSE as separate data-sets).  I've tried using various
> other geometries (inc. geom_path()) but all have produced similar events.
> Is there a way I can plot the time-series in a continuous way (i.e. as one
> data-set) and change only the colour of the line?
>
> Thanks,
> Joe
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Changing-colour-of-continuous-time-series-in-ggplot2-tp3356582p3356582.html
> Sent from the R help mailing list archive at Nabble.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.
>



-- 
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/



More information about the R-help mailing list