ggplot垂直线与日期轴 [英] ggplot vertical line with date axis
本文介绍了ggplot垂直线与日期轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当x轴是datetime( POSIXct
)对象时,我在添加垂直线时遇到麻烦。似乎总是想把这条线放在时代。这里有一个例子:
df< - data.frame(x = ymd('2011-01-01')+小时(0:24),y = runif(25))
ggplot(df,aes(x = x,y = y))+ geom_point()
现在我尝试在第三个观察时间添加一行:
ggplot(df, aes(x = x,y = y))+ geom_point()+ geom_vline(aes(x = df $ x [3]))
解决方案
尝试这样做:
geom_vline(xintercept = df $ x [3])
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?
解决方案
Try doing this instead:
geom_vline(xintercept = df$x[3])
这篇关于ggplot垂直线与日期轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文