在R中绘制时间序列

itc*_*lpl 2 plot r time-series

我正在处理数据,前两列是日期,第三列是符号,第四和第五列是价格.所以,我创建了一个数据子集如下:

test.sub<-subset(test,V3=="GOOG",select=c(V1,V4)
Run Code Online (Sandbox Code Playgroud)

然后我尝试使用以下方法绘制时间序列图

as.ts(test.sub)
plot(test.sub)
Run Code Online (Sandbox Code Playgroud)

好吧,它给了我一个散点图 - 不是我想要的.所以,我试过plot(test.sub[1],test.sub[2]) ,现在我收到以下错误:

Error in xy.coords(x, y, xlabel, ylabel, log) : 
  'x' and 'y' lengths differ
Run Code Online (Sandbox Code Playgroud)

确保没有.行是相同的,我跑了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 
Run Code Online (Sandbox Code Playgroud)

现在这个工作正常,我想添加一个第三个变量来绘制一个三维图表 - 任何建议我如何做到这一点.谢谢!

mwe*_*ndt 14

所以我认为这里有一些值得一谈的事情:

首先,一些示例数据:

test <- data.frame(End = Sys.Date()+1:5, 
               Start = Sys.Date()+0:4, 
               tck = rep("GOOG",5), 
               EndP= 1:5, 
               StartP= 0:4)

test.sub = subset(test, tck=="GOOG",select = c(End, EndP))
Run Code Online (Sandbox Code Playgroud)

首先,请注意test和test.sub都是数据框,因此调用test.sub[1]并不像R 一样"意味着"任何东西.**test.sub[,1]由于与其他R结构的一致性,写入更多R-ish .如果你比较的结果str(test.sub[1])str(test.sub[,1])你会看到的是,R对待他们略有不同.

你说你输入了:

as.ts(test.sub)
plot(test.sub)
Run Code Online (Sandbox Code Playgroud)

我猜你在某种OO语言方面有丰富的经验; 虽然R确实有一些OO味道,但它不适用于此.不是将test.sub转换为类ts的东西,而是仅进行转换并将其抛弃,然后继续绘制您开始使用的数据框.这是一个简单的解决方案:

test.sub.ts <- as.ts(test.sub)
plot(test.sub.ts)
Run Code Online (Sandbox Code Playgroud)

但是,这可能不是你想要的.相反,R创建一个时间序列,其中包含两个名为"End"的变量(现在是强制转换为整数的日期)和"EndP".像这样的有趣的业务是像动物园和xts这样的时间序列包已经流行的部分原因,所以我会详细介绍它们.

(不幸的是,据我所知,R不会使用其默认的ts类保留日期戳,而是选择保留开始和结束日期以及频率.对于更一般的时间序列工作,这很少是足够灵活的)

你也许可以通过打字得到你想要的东西

plot(test.sub[,1], test.sub[,2]) 
Run Code Online (Sandbox Code Playgroud)

代替

plot(test.sub[1], test.sub[2])
Run Code Online (Sandbox Code Playgroud)

因为前者遇到麻烦,因为你传递了两个子数据帧而不是两个向量(即使它看起来像你会这样).*

无论如何,使用xts(同样适用于动物园):

library(xts) # You may need to install this
xtemp <- xts(test.sub[,2], test.sub[,1]) # Create the xts object
plot(xtemp) 
# Dispatches a xts plot method which does all sorts of nice time series things
Run Code Online (Sandbox Code Playgroud)

希望其中一些有用并且对于未被识别的内联代码感到抱歉:仍然习惯于堆栈溢出.

迈克尔

**实际上,他们访问用于在内部构建数据框的列表,但这比代码值得依赖的代码更具细微差别.

***实质上是当你传递plot(test.sub[1], test.sub[2])给R时,它会调度plot.data.frame采用单个数据帧的方法并尝试将第二个数据帧解释为另外的一个绘图参数,这个参数在某个地方被误解,给出了错误.