R-如何将垂直线添加到具有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

如何获得正确的结果?

您只需要包装as.numeric

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

最新更新