简体   繁体   English

ggplot geom_line到日期轴不起作用

[英]ggplot geom_line to date axis not working

I have several data-sets similar to https://www.dropbox.com/s/j9ihawgfqwxmkgc/pred.csv?dl=0 我有几个类似于https://www.dropbox.com/s/j9ihawgfqwxmkgc/pred.csv?dl=0的数据集

Loading them from CSV and then plotting works fine 从CSV加载它们然后绘图工作正常

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

But when I want to use GGPLOT to draw these data predicted points into a plot to compare them with the original points like: 但是,当我想使用GGPLOT将这些数据预测点绘制到一个图中时,将它们与原始点进行比较,如:

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

This command 这个命令

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

generates the following error: 生成以下错误:

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

But as the first plot(predictions$date, predictions$pct50) works with the data I do not understand what is wrong. 但是,作为第一个plot(predictions$date, predictions$pct50)与数据plot(predictions$date, predictions$pct50)工作,我不明白有什么不对。

Edit 编辑

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")

Edit 2 编辑2

I change this 我改变了

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

and the error changed to: 并将错误更改为:

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

so I think the hint to How to deal with "data of class uneval" error from ggplot2? 所以我认为如何处理来自ggplot2的“类unval”错误的提示? (see comments) was a good Idea, bit still the plot does not work. (见评论)是个好主意,有点情节不起作用。

Your first issue (Edit 2) is because ?geom_line uses mapping=NULL as the first argument, so you need to explicitely state the first argument is data 你的第一个问题(编辑2)是因为?geom_line使用mapping=NULL作为第一个参数,所以你需要明确说明第一个参数是data

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

similar question 类似的问题

Your second issue is because your predictions$date is a character vector, and when using as.numeric it introduces NA s. 你的第二个问题是因为你的predictions$date是一个字符向量,当使用as.numeric时它引入了NA If you want numerics you need to format it as a date first, then convert it to numeric 如果你想要数字,你需要先将它格式化为日期,然后将其转换为数字

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM