2012-02-23 194 views
9

我在x轴是日期时间(POSIXct)对象时向垂直线添加垂直线时遇到了一些问题。它似乎总是想把这条线放在大纪元上。这里有一个例子:ggplot垂直线与日期轴

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

without vertical line

现在,我尝试在第三观测时间添加一行:

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

with vertical line

什么我做错了吗?

回答

3

尝试这样做,而不是:

geom_vline(xintercept = df$x[3]) 
+0

谢谢。看起来ggplot文档中的美学列表中缺少'xintercept',而那里的说法却用'x'来代替。 =/ – 2012-02-23 23:11:40

+0

是的,那也把我扔了。 http://had.co.nz/ggplot2/geom_vline.html中的文档声称使用'aes(x = whatever)',但以下所有示例都使用'xintercept' – Andrew 2012-02-23 23:27:59

+1

准确使用@KenWilliams示例我得到> ggplot( df,aes(x = x,y = y))+ geom_point()+ geom_vline(xintercept = df $ x [3]) 错误:无效的截取类型:应该是数字向量,函数或功能。有任何想法吗? – 2014-02-26 22:31:04

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

你想xintercept而不是xgeom_vlineaes

+0

也是第二个和第三个[google serarch](https://www.google.com/search?sourceid=chrome&client=ubuntu&channel=cs&ie=UTF-8&q=geom_vline+and+date&safe=on)结果似乎可以回答你的问题。 .. – Justin 2012-02-23 22:41:06

+0

感谢您的帮助。在提交此问题之前,我确实看到了这些页面,但没有注意到美学名称的变化。文档中缺少'xintercept'美学,我会看看是否可以提交文档补丁。 – 2012-02-23 23:18:03

+0

但我无法弄清楚在文档中产生审美列表的内容,它似乎不是在https://github.com/hadley/ggplot2/blob/master/R/geom-vline.r中的任何内容。 – 2012-02-23 23:22:19