如何按日期绘制一系列垂直线

时间:2017-10-25 21:27:37

标签: r ggplot2

我尝试使用基于开始日期和结束日期的时间块绘制图表。然后我在给定的时间间隔添加垂直线。我该怎么做呢?我尝试的代码一直给我一个错误。另外,我相信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)

ym1 <- as.yearmon(min(DF$START)) # convert to yearmon
ym2 <- as.yearmon(max(DF$END)) # ditto
ListStartOfMonthDates <- seq(ym1, ym2, 1/12)
ListStartOfMonthDates <- as.Date(paste('01',  s), format='%d %b %Y')  
ListStartOfMonthDates <- as.POSIXct(paste(ListStartOfMonthDates, "07:00:00 MDT"), "%Y-#m-#d %H:%M")
#looks like it changes the time zone here. 

**Warning messages:**
1: In strptime(xx, f <- "%Y-%m-%d %H:%M:%OS", tz = tz) :
unknown timezone '%Y-#m-#d %H:%M'
2: In as.POSIXct.POSIXlt(x) : unknown timezone '%Y-#m-#d %H:%M'
3: In strptime(x, f, tz = tz) : unknown timezone '%Y-#m-#d %H:%M'
4: In as.POSIXct.POSIXlt(as.POSIXlt(x, tz, ...), tz, ...) :
unknown timezone '%Y-#m-#d %H:%M'

现在我想绘制列表中每个时间点的费率和垂直线&#34; ListStartOfMonthDates&#34;

我的代码:

ggplot(DF)+
geom_segment(aes(x=as.POSIXct(as.character(START)),
                 xend=as.POSIXct(as.character(END)),
                 y=RATE,
                 yend=RATE), size=2)+
geom_vline(aes(xintercept=as.numeric(ListStartOfMonthDates)),
           linetype=4, colour="black")

这会产生错误。

 **Error**: Aesthetics must be either length 1 or the same as the data (25): xintercept

0 个答案:

没有答案