ggplot geom_line到日期轴不工作 [英] ggplot geom_line to date axis not working
问题描述
我有几个类似于 https:// www的数据集.dropbox.com / s / j9ihawgfqwxmkgc / pred.csv?dl = 0
从CSV中加载它们,然后绘图工作正常
预测$ date< - as.Date(预测$日期)
图(预测$日期,预测$ pct50)
但是,当我想使用GGPLOT将这些数据预测点绘制成图时,将它们与原始点进行比较,如:
p = ggplot(theRealPastDataValues,aes(x = date,y = cumsum(amount)))+ geom_line()
这个命令
p + geom_line(预测,aes(x = as.numeric(日期),y = pct50))
生成以下错误:
ggplot2不知道如何处理类不等的数据
但是,作为第一个图(预测
编辑
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 ))
并将错误更改为:
无效的输入:date_trans对类Date的对象有效
Zusätzlich:警告消息:
在eval(expr,envir,enclos)中:创建了
so I th墨迹提示如何处理"不同类别的数据错误来自ggplot2?(请参阅评论)是一个好主意,有点还是情节不起作用。
你的第一个问题(编辑2)是因为?geom_line
使用 mapping = NULL
作为第一个参数,所以你需要显式声明第一个参数是 data
p + geom_line(data =预测,aes(x = as.numeric(日期),y = pct50))
你的第二个问题是因为你的预测$ date
是一个字符向量,当使用as.numeric时,它引入了 NA
s。如果你需要数字,你需要首先格式化它作为日期,然后将其转换为数字
as.numeric(as.Date (预测$ date),format =%Y%m%d)
I have several data-sets similar to https://www.dropbox.com/s/j9ihawgfqwxmkgc/pred.csv?dl=0
Loading them from CSV and then plotting works fine
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:
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.
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
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? (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
p + geom_line(data = predictions, aes(x=as.numeric(date), y=pct50))
Your second issue is because your predictions$date
is a character vector, and when using as.numeric it introduces NA
s. 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")
这篇关于ggplot geom_line到日期轴不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!