【发布时间】:2011-12-09 16:16:01
【问题描述】:
我正在处理数据,第一列两列是日期,第三列是符号,第四列和第五列是价格。 因此,我创建了一个数据子集,如下所示:
test.sub<-subset(test,V3=="GOOG",select=c(V1,V4)
然后我尝试使用以下方法绘制时间序列图
as.ts(test.sub)
plot(test.sub)
好吧,它给了我一个散点图——不是我想要的。
所以,我尝试了plot(test.sub[1],test.sub[2])
现在我收到以下错误:
Error in xy.coords(x, y, xlabel, ylabel, log) :
'x' and 'y' lengths differ
确保没有。行数相同,我运行了nrow(test.sub[1]) 和nrow(test.sub[2]),它们都返回相同的行,所以作为 R 的新手,我不确定修复方法是什么。
我还运行了plot.ts(test.sub),它有效,但它没有显示 x 轴上的日期,它与 plot(test.sub) 一起运行,这是我希望看到的。
test.sub[1]
V1
1107 2011-Aug-24
1206 2011-Aug-25
1307 2011-Aug-26
1408 2011-Aug-29
1510 2011-Aug-30
1613 2011-Aug-31
1718 2011-Sep-01
1823 2011-Sep-02
1929 2011-Sep-06
2035 2011-Sep-07
2143 2011-Sep-08
2251 2011-Sep-09
2359 2011-Sep-13
2470 2011-Sep-14
2581 2011-Sep-15
2692 2011-Sep-16
2785 2011-Sep-19
2869 2011-Sep-20
2965 2011-Sep-21
3062 2011-Sep-22
3160 2011-Sep-23
3258 2011-Sep-26
3356 2011-Sep-27
3455 2011-Sep-28
3555 2011-Sep-29
3655 2011-Sep-30
3755 2011-Oct-03
3856 2011-Oct-04
3957 2011-Oct-05
4059 2011-Oct-06
4164 2011-Oct-07
4269 2011-Oct-10
4374 2011-Oct-11
4479 2011-Oct-12
4584 2011-Oct-13
4689 2011-Oct-14
str(test.sub)
'data.frame': 35 obs. of 2 variables:
$ V1:Class 'Date' num [1:35] NA NA NA NA NA NA NA NA NA NA ...
$ V4: num 0.475 0.452 0.423 0.418 0.403 ...
head(test.sub) V1 V4
1212 <NA> 0.474697
1313 <NA> 0.451907
1414 <NA> 0.423184
1516 <NA> 0.417709
1620 <NA> 0.402966
1725 <NA> 0.414264
现在这正在工作,我想添加第三个变量来绘制 3d 图表 - 任何建议我可以如何做到这一点。谢谢!
【问题讨论】:
-
你到底在寻找什么样的情节?
-
只是一个折线图,x 轴显示第一列中的日期,y 轴显示第四列中的价格
-
那么就做
plot(test.sub,type="l")。 -
它仍然像
plot(test.sub)一样给我散点。 -
另外,作为初学者,我很想知道为什么当两列中的行数相同时我得到
Error in xy.coords
标签: r plot time-series