【发布时间】:2016-09-25 21:53:50
【问题描述】:
我有一个包含两个变量的数据框。其中一个是日期(Col1),另一个是数字人口数据(sk)。我可以像这样绘制数据:
plot(D$sk)
我的问题是(显然)该图不包括 x 轴中的日期。我试图通过这段代码添加它:
plot(D$sk, D$Col1, ylim=c(2013-01,2015-05))
但我收到一个空白图和以下错误消息:
Warning message:
In xy.coords(x, y, xlabel, ylabel, log) : NAs introduced by coercion
基于this 对类似问题的回答,我尝试使用xaxt=n 参数,如下所示:
with(D, plot(Col1, sk, xaxt="n"))
但它也不起作用。我收到以下错误:
Error in plot.window(...) : need finite 'xlim' values
In addition: Warning messages:
1: In xy.coords(x, y, xlabel, ylabel, log) : NAs introduced by coercion
2: In min(x) : no non-missing arguments to min; returning Inf
3: In max(x) : no non-missing arguments to max; returning -Inf
你能帮我怎么做吗?
编辑:Col1 由以下代码定义:
Col1=seq(as.Date("2013/1/1"), by="1 month", length.out=41).
sk 变量是介于 100.000 和 200.000 之间的某个随机数。对不起,但我是 R 新手,不知道如何重新处理。我从 41 张 excel 表中提取了它,并对其进行了几次转换。但基本上这些是数据框中的两个变量。我希望这会有所帮助。
EDIT2:抱歉,刚刚注意到还有另一个代码减少了日期变量:
D$Col1=format(D$Col1, format="%Y-%m")
【问题讨论】:
-
你能举一个你的数据的例子吗?或者先看这个帖子:stackoverflow.com/questions/4843969/…
-
确保日期列是模式日期。如果没有尝试
as.Date(D$Col1, format='%Y-%m')。格式可能不同 -
2013-01将评估为 2012 年!需要用引号括起来,可能需要是带有as.Date的日期对象 -
引号给出了这个错误:
plot(D$sk, D$Col1, ylim=c("2013-01","2015-05")) Error in D$sk : $ operator is invalid for atomic vectors