2012-02-23 109 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