ggTimeSeries手动连续颜色

时间:2017-02-28 16:24:14

标签: r ggplot2 time-series

我的df:

prod
# A tibble: 695 × 3
   REPORT_DATE  UNIT   PROD
        <date> <chr>    <dbl>
1   2015-03-28 DEP11 2.043962
2   2015-03-29 DEP11 2.788490
3   2015-03-30 DEP11 2.795274
4   2015-03-31 DEP11 3.100589
5   2015-04-01 DEP11 2.882843
6   2015-04-02 DEP11 2.987861
7   2015-04-03 DEP11 3.123047
8   2015-04-04 DEP11 3.264180
9   2015-04-05 DEP11 2.987729
10  2015-04-06 DEP11 3.222573
# ... with 685 more rows

我创建了一个ggTimeSeries图,如下所示: enter image description here

我想改变颜色方案......并希望将颜色分为3类:

  1. 低于3.0 =红色
  2. 3.0 - 3.2 = amber
  3. 大于3.2 =绿色
  4. 我尝试了以下内容:

    ggplot_calendar_heatmap(
       prod,
       'REPORT_DATE',
       'PROD'
    ) +
       xlab('') + 
       ylab('') + 
       scale_fill_continuous(low = 'red', high = 'green') + 
       facet_wrap(~Year, ncol = 1)
    

    还试图使用scale_colour_gradientnscale_colour_manuel,但没有运气......任何想法?

1 个答案:

答案 0 :(得分:2)

这样的事情应该有效:

set.seed(1)
# generate some random data
prod <- data.frame(REPORT_DATE=seq.Date(as.Date('2015/01/03'), as.Date('2017/02/28'), by='day')) 
prod$PROD <- runif(nrow(prod), 0, 5)
prod <- transform(prod, PROD.cut=cut(PROD, breaks=c(-Inf,3, 3.2,Inf)))           # bin data
library(ggTimeSeries)
ggplot_calendar_heatmap(
  prod,
  'REPORT_DATE',
  'PROD.cut'
) +
  xlab('') + 
  ylab('') + 
  scale_fill_manual(values = c("red", "orange", "green")) +
  #scale_fill_continuous(low = 'red', high = 'green') + 
  facet_wrap(~Year, ncol = 1)

enter image description here