簡體   English   中英

ggplot geom_line到日期軸不起作用

[英]ggplot geom_line to date axis not working

我有幾個類似於https://www.dropbox.com/s/j9ihawgfqwxmkgc/pred.csv?dl=0的數據集

從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)與數據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

所以我認為如何處理來自ggplot2的“類unval”錯誤的提示? (見評論)是個好主意,有點情節不起作用。

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

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

類似的問題

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM