简体   繁体   English

带日期轴的ggplot垂直线

[英]ggplot vertical line with date axis

I'm having some trouble adding a vertical line to a plot when the x-axis is a datetime ( POSIXct ) object. 当x轴是datetime( POSIXct )对象时,在向绘图添加垂直线时遇到了一些麻烦。 It seems to always want to put the line at the Epoch. 似乎总是想把时间放在时代。 Here's an example: 这是一个例子:

df <- data.frame(x=ymd('2011-01-01')+hours(0:24), y=runif(25))
ggplot(df, aes(x=x,y=y)) + geom_point()

没有垂直线

Now I try to add a line at the third observation time: 现在,我尝试在第三个观察时间添加一行:

ggplot(df, aes(x=x,y=y)) + geom_point() + geom_vline(aes(x=df$x[3]))

垂直线

Something I'm doing wrong? 我做错了什么?

尝试这样做:

geom_vline(xintercept = df$x[3])
ggplot(df, aes(x=x,y=y)) + geom_point() + geom_vline(aes(xintercept=df$x[3]))

您需要在geom_vline aes xintercept而不是x

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM