在y轴上基于小时分钟绘制垂直线

时间:2013-01-18 17:11:53

标签: r ggplot2

X

structure(list(Date = structure(c(15358, 15359, 15362, 15363, 
15364, 15365), class = "Date"), EndTime1 = structure(list(sec = c(0, 
0, 0, 0, 0, 0), min = c(45L, 25L, 7L, 19L, 5L, 23L), hour = c(5L, 
8L, 3L, 4L, 4L, 3L), mday = c(18L, 18L, 18L, 18L, 18L, 18L), 
    mon = c(0L, 0L, 0L, 0L, 0L, 0L), year = c(113L, 113L, 113L, 
    113L, 113L, 113L), wday = c(5L, 5L, 5L, 5L, 5L, 5L), yday = c(17L, 
    17L, 17L, 17L, 17L, 17L), isdst = c(0L, 0L, 0L, 0L, 0L, 0L
    )), .Names = c("sec", "min", "hour", "mday", "mon", "year", 
"wday", "yday", "isdst"), class = c("POSIXlt", "POSIXt")), EndTime2 = structure(list(
    sec = c(0, 0, 0, 0, 0, 0), min = c(45L, 41L, 11L, 27L, 19L, 
    34L), hour = c(7L, 15L, 5L, 7L, 8L, 5L), mday = c(18L, 18L, 
    18L, 18L, 18L, 18L), mon = c(0L, 0L, 0L, 0L, 0L, 0L), year = c(113L, 
    113L, 113L, 113L, 113L, 113L), wday = c(5L, 5L, 5L, 5L, 5L, 
    5L), yday = c(17L, 17L, 17L, 17L, 17L, 17L), isdst = c(0L, 
    0L, 0L, 0L, 0L, 0L)), .Names = c("sec", "min", "hour", "mday", 
"mon", "year", "wday", "yday", "isdst"), class = c("POSIXlt", 
"POSIXt")), EndTime3 = structure(list(sec = c(0, 0, 0, 0, 0, 
0), min = c(7L, 59L, 30L, 48L, 46L, 58L), hour = c(8L, 15L, 5L, 
7L, 8L, 5L), mday = c(18L, 18L, 18L, 18L, 18L, 18L), mon = c(0L, 
0L, 0L, 0L, 0L, 0L), year = c(113L, 113L, 113L, 113L, 113L, 113L
), wday = c(5L, 5L, 5L, 5L, 5L, 5L), yday = c(17L, 17L, 17L, 
17L, 17L, 17L), isdst = c(0L, 0L, 0L, 0L, 0L, 0L)), .Names = c("sec", 
"min", "hour", "mday", "mon", "year", "wday", "yday", "isdst"
), class = c("POSIXlt", "POSIXt"))), .Names = c("Date", "EndTime1", 
"EndTime2", "EndTime3"), row.names = c(NA, 6L), class = "data.frame")

\ n

y_limits = as.POSIXct(c(strptime("00:00", "%H:%M"), strptime("23:29", "%H:%M")))
y_breaks = seq(from=strptime("00:00", "%H:%M"), 
to=strptime("23:29", "%H:%M"), by="2 hours")
y_labels = format(y_breaks, "%H:%M")
s<-as.POSIXlt("09:00", format="%H:%M")

ggplot(x, aes(Date, EndTime1, group=1, colour="Team1")) + geom_line() + scale_y_datetime(limits=y_limits, breaks=y_breaks, labels=y_labels) + geom_line(aes(Date, EndTime2, colour="Team2")) + geom_line(aes(Date, EndTime3, colour="Team3")) + geom_hline(yintercept=s, colour="red")

我喜欢在s上绘制一个geom_line但是没有工作。得到错误:

Error : Invalid intercept type: should be a numeric vector, a function, or a name of a function
Error in if (nrow(layer_data) == 0) return() : argument is of length zero

任何想法如何在给定的s上绘制垂直线?

1 个答案:

答案 0 :(得分:2)

s<-as.POSIXct(c("09:00"), format="%H:%M")
s<-as.numeric(s)