使用一系列开始结束日期绘制数据

时间:2017-08-17 22:24:42

标签: r

我如何使用一系列开始结束日期范围绘制数据?

我有一个类似于:

的数据框
START<-as.POSIXct(c("2017-07-13 01:40:00 MDT", "2017-07-21 06:00:00 MDT", "2017-07-21 14:00:00 MDT", "2017-07-24 11:00:00 MDT",
        "2017-07-24 12:00:00 MDT", "2017-07-25 05:00:00 MDT", "2017-07-25 17:00:00 MDT", "2017-07-26 12:00:00 MDT",
        "2017-07-30 12:00:00 MDT", "2017-07-31 04:00:00 MDT", "2017-07-31 15:00:00 MDT", "2017-08-03 18:30:00 MDT",
        "2017-08-03 23:30:00 MDT", "2017-08-09 05:00:00 MDT", "2017-08-09 20:00:00 MDT", "2017-08-14 09:00:00 MDT",
        "2017-08-16 05:00:00 MDT", "2017-08-16 07:00:00 MDT", "2017-08-16 19:00:00 MDT", "2017-08-17 18:00:00 MDT",
        "2017-08-20 05:00:00 MDT", "2017-08-23 06:00:00 MDT", "2017-08-23 14:00:00 MDT", "2017-08-24 17:00:00 MDT",
        "2017-08-28 00:00:00 MDT"))

END<-as.POSIXct(c("2017-07-21 06:00:00 MDT", "2017-07-21 14:00:00 MDT", "2017-07-24 11:00:00 MDT", "2017-07-24 12:00:00 MDT",
    "2017-07-25 05:00:00 MDT", "2017-07-25 17:00:00 MDT", "2017-07-26 12:00:00 MDT", "2017-07-30 12:00:00 MDT",
    "2017-07-31 04:00:00 MDT", "2017-07-31 15:00:00 MDT", "2017-08-03 18:30:00 MDT", "2017-08-03 23:30:00 MDT",
    "2017-08-09 05:00:00 MDT", "2017-08-09 20:00:00 MDT", "2017-08-14 09:00:00 MDT", "2017-08-16 05:00:00 MDT",
    "2017-08-16 07:00:00 MDT", "2017-08-16 19:00:00 MDT", "2017-08-17 18:00:00 MDT", "2017-08-20 05:00:00 MDT",
    "2017-08-23 06:00:00 MDT", "2017-08-23 14:00:00 MDT", "2017-08-24 17:00:00 MDT", "2017-08-28 00:00:00 MDT",
    "2017-09-28 13:00:00 MDT"))

RATE<-c(1485, 0, 1485,  880, 1485, 0, 1485, 1100, 1485, 0, 1485, 1483, 1485, 0, 1485, 1419, 880, 0, 1419, 1485, 1419, 0, 1100, 419, 1100)

DF<-data.frame(START, END, RATE)

我想绘制&#39; Rate&#39;在y轴上和x轴上的日期。在ggplot或其他绘图包中有没有一个很好的方法呢?

注意:结束日期恰好是下一个价格开始的时间。另外,我希望这是一个折线图。

1 个答案:

答案 0 :(得分:1)

我认为这样做会......

library(ggplot2)
ggplot(DF)+
    geom_rect(aes(xmin=as.POSIXct(as.character(START)),
                  xmax=as.POSIXct(as.character(END)),
                  ymin=0,
                  ymax=RATE))

enter image description here

或者,对于折线图,请尝试

ggplot(DF)+
    geom_segment(aes(x=as.POSIXct(as.character(START)),
                     xend=as.POSIXct(as.character(END)),
                     y=RATE,
                     yend=RATE), size=2)

enter image description here