Sorry if this is a duplicate question but other answers I have tried have not resulted in my desired plot.
I would like to colour code this graph based upon whether the value is positive or negative which I have termed the "phase".
Where the geom_line() hits just above or below 0 I would like it to colour either "positive" or "negative" - I thought the following would work but it doesn't seem to. Can someone tell me where I am going wrong? Thanks.
]1
Data
structure(list(Date = c(1876, 1877, 1878, 1879, 1880, 1881),
value = c(4.95, -10.0416666666667, 2.95, 12.85, 7.8, -4.76666666666667
), phase = structure(c(3L, 1L, 3L, 3L, 3L, 1L), .Label = c("negative",
"neutral", "positive"), class = "factor")), row.names = c(NA,
-6L), class = c("data.table", "data.frame"), .internal.selfref = <pointer: 0x7ffb06817ce0>)
Code
## create a column to determine if the phase is negative, positive or neutral i.e. 0
dd_avg$phase<-factor(sign(dd_avg$value), (-1):1, c('negative', 'neutral', 'positive'))
dd_avg
## plot it up
ggplot(data=dd_avg, aes(x=Date, y=value, group=Date, colour=phase))+
geom_line()