2015-06-21 59 views
1

日期軸線我有幾個數據集類似於https://www.dropbox.com/s/j9ihawgfqwxmkgc/pred.csv?dl=0ggplot geom_line不工作

從CSV加載它們,然後繪製工細

predictions$date <- as.Date(predictions$date) 
plot(predictions$date, predictions$pct50) 

但是,當我想用​​GGPLOT得出這些數據預測點到情節將它們與原來的點狀比較:

p = ggplot(theRealPastDataValues,aes(x=date,y=cumsum(amount)))+geom_line() 

此命令

p + geom_line(predictions, aes(x=as.numeric(date), y=pct50)) 

生成以下錯誤:

ggplot2 doesn't know how to deal with data of class uneval 

但作爲第一plot(predictions$date, predictions$pct50)作品與數據我不明白什麼是錯的。

編輯

dput(predictions[1:10, c("date", "pct50")]) 
structure(list(date = c("2009-07-01", "2009-07-02", "2009-07-03", 
"2009-07-04", "2009-07-05", "2009-07-06", "2009-07-07", "2009-07-08", 
"2009-07-09", "2009-07-10"), pct50 = c(4276, 4076, 4699.93, 4699.93, 
4699.93, 4699.93, 4664.76, 4627.37, 4627.37, 4627.37)), .Names = c("date", 
"pct50"), row.names = c(NA, 10L), class = "data.frame") 

編輯2

我改變這個

p + geom_line(data = predictions, aes(x=as.numeric(date), y=pct50)) 

,並改爲錯誤:

Invalid input: date_trans works with objects of class Date only 
Zusätzlich: Warning message: 
In eval(expr, envir, enclos) : NAs created 

所以我覺得提示How to deal with "data of class uneval" error from ggplot2?(見評論)是一個好主意,還有點情節不起作用。

+0

'?geom_line'顯示第一個參數是'mapping = NULL'。嘗試明確指出'數據=預測' – tospig

+0

[這個答案](http://stackoverflow.com/a/16486873/4002530)是類似的 – tospig

+1

確定 - 請參閱編輯 –

回答

3

你的第一個問題(編輯2)是因爲?geom_line使用mapping=NULL作爲第一個參數,所以你需要明確地說出第一個參數是data

p + geom_line(data = predictions, aes(x=as.numeric(date), y=pct50)) 

similar question

你的第二個問題是因爲你predictions$date是一個字符矢量,當使用as.numeric時它引入了NA s。如果你需要數字,你需要首先將其格式化爲日期,然後將其轉換爲數字

as.numeric(as.Date(predictions$date), format="%Y%m%d")