简体   繁体   中英

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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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