2017-02-19 55 views
0

一個xlab考慮提供與下面的代碼情節一個情節:如何垂直線添加到具有在時間格式

x<-seq(as.POSIXct("2016-01-01 00:05:00"), as.POSIXct("2016-01-02 00:00:00"), by = '5 min') 
t<-as.POSIXct("2016-01-01 07:08:32") 
y<-c(1:288) 
df<-data.frame(x,y) 
library('ggplot2') 
p<-ggplot(data=df,aes(x,y))+geom_point() 
p 

現在我想添加一個垂直線,其定位在2016-01-01 07:08:32,所以我嘗試以下:

p+geom_vline(xintercept=as.POSIXct("2016-01-01 07:08:32")) 

然而,這不是解決方案,它返回:

Error in Ops.POSIXt((x - from[1]), diff(from)) : 
    '/' not defined for "POSIXt" objects 

我怎樣才能得到正確的結果?

回答

0

你只需要包裝的日期as.numeric

p+geom_vline(xintercept=as.numeric(as.POSIXct("2016-01-01", format="%Y-%m-%d")))