【问题标题】:plotting time series in R在R中绘制时间序列
【发布时间】: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


【解决方案1】:

所以我认为这里发生了一些值得讨论的事情:

首先,一些示例数据:

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

首先,请注意 test 和 test.sub 都是数据帧,所以像 test.sub[1] 这样的调用对 R 来说并没有真正的“意义”。** 由于一致性,写 test.sub[,1] 更符合 R-ish与其他 R 结构。如果您比较 str(test.sub[1])str(test.sub[,1]) 的结果,您会发现 R 对它们的处理略有不同。

你说你输入了:

as.ts(test.sub)
plot(test.sub)

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

test.sub.ts <- as.ts(test.sub)
plot(test.sub.ts)

但是,这可能也不是您想要的。相反,R 创建了一个时间序列,其中包含两个变量,分别称为“End”(现在强制转换为整数的日期)和“EndP”。像这样有趣的事情是像 zoo 和 xts 这样的时间序列包流行起来的部分原因,所以我将在下面详细介绍它们。

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

你也许可以通过输入得到你想要的

plot(test.sub[,1], test.sub[,2]) 

而不是

plot(test.sub[1], test.sub[2])

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

无论如何,使用 xts(对于 zoo 也是如此):

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

希望其中的一些内容有所帮助,并对未识别为内联代码的内联代码表示抱歉:仍然习惯于堆栈溢出。

迈克尔

**实际上,他们访问用于在内部构建数据框的列表,但这更多是代码的细微差别,而不是值得依赖的东西。

***本质是,当您将 plot(test.sub[1], test.sub[2]) 传递给 R 时,它会调度 plot.data.frame 方法,该方法采用单个数据帧并尝试将第二个数据帧解释为额外的绘图参数,但该参数会被误解某处下线,给你的错误。

【讨论】:

  • 感谢您的解释 - 确实很有帮助。 xts 出现故障。我运行了以下xtemp&lt;-xts(test.sub[,2],test.sub[,1]) Error in xts(test.sub[, 2], test.sub[, 1]) : order.by requires an appropriate time-based object 我检查了test.sub[1] 它以'yyyy-mmm-dd' 格式显示日期,所以它是一个基于时间的对象......我错过了什么
  • 如果将日期显示为“yyyy-mm-dd”,则不一定是基于时间的对象:根据您的数据源,它可能只是一个字符,对您来说显然是一个日期,但 R不知道。 Date 是 R 的一种特殊数据类型...尝试使用 as.Date() 包装 test.sub[,1] 如果您不遵循标准,则它采用可选的 format= 参数。对你来说,听起来 as.Date(test.sub[,1], format = "YYYY-mm-dd") 会起作用。
  • 试过了,但没有运气,这是它返回的结果 - as.Date(test.sub[,1],format="YYYY-mm-dd") [1] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA [26] NA NA NA NA NA NA NA NA NA NA NA。这是我在 test.sub 4689 2011-Oct-14 0.2460010 7.18000 1.000000 中的数据样本,日期为 V1
  • @itcplpl 我相信 mweylandt 只是给了你格式参数的错误格式(讽刺!)。请改用format = '%Y-%m-%d'
  • 也返回了 NA :-( 这就是我得到的 as.Date(test.sub[,1],format='%Y-%m-%d') [1] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA [26] NA NA NA NA NA NA NA NA NA NA NA
【解决方案2】:

如果您在引发错误后立即进行回溯,那么您收到关于不同 xy 长度的错误的原因立即显而易见:

> plot(test.sub[1],test.sub[2])
Error in xy.coords(x, y, xlabel, ylabel, log) : 
  'x' and 'y' lengths differ
> traceback()
6: stop("'x' and 'y' lengths differ")
5: xy.coords(x, y, xlabel, ylabel, log)
4: plot.default(x1, ...)
3: plot(x1, ...)
2: plot.data.frame(test.sub[1], test.sub[2])
1: plot(test.sub[1], test.sub[2])

您的电话中的问题是多方面的。首先,正如@mweylandt 所提到的,test.sub[1] 是具有单个组件的数据框,而不是由 test.sub 的第一个组件的内容组成的向量。

从回溯中,我们看到调用了plot.data.frame 方法。 R 很乐意绘制一个数据框,只要它至少有两列。 R 相信你的话并将test.sub[1](作为data.frame)传递给plot() - test.sub[2] 永远不会看到。test.sub[1] 最终传递给xy.coords(),它正确地通知你你x 有很多行,但 y 有 0 行,因为 test.sub[1] 只包含一个组件。

如果您已完成 plot(test.sub[,1], test.sub[,2], type = "l") 或使用公式界面将变量命名为 plot(V4 ~ V1, data = test.sub, type = "l"),就像我在另一个答案中显示的那样,它会起作用。

【讨论】:

    【解决方案3】:

    使用公式界面肯定更方便:

    > 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))
    > head(test.sub)
             End EndP
    1 2011-10-19    1
    2 2011-10-20    2
    3 2011-10-21    3
    4 2011-10-22    4
    5 2011-10-23    5
    > plot(EndP ~ End, data = test.sub, type = "l")
    

    我广泛使用时间序列类型的数据,很少(如果有的话)需要"ts" 对象类。 zooxts 包非常有用,但如果您只想绘制数据,i) 将日期/时间信息正确格式化/设置为"Date""POSIXt" 类对象,然后 ii) 只需使用标准图形和 type = "l" (或 type = "b"type = "o" 如果您想查看观察时间)绘制它。

    【讨论】:

    • 感谢您的示例,这很有帮助。我现在的问题是让 Date 类对象正常工作。我在处理历史数据时不能使用Sys.Date()。我发布了我在 Date 中遇到的错误。关于修复的建议会非常有帮助
    • @itcplpl 您如何向我们展示您的日期数据是什么样的?如果您向我展示格式,我将向您展示如何将其转换为 R 可以读取的内容。
    • 听起来不错。我刚刚用日期数据更新了原始帖子
    • @itcplpl test.sub &lt;- within(test.sub, V1 &lt;- as.Date(V1, format = "Y%-%b-%d")) 应该这样做。有关格式代码的详细信息,请参阅?strftime
    • 感谢 strftime 上的指针。那一点有效,但是当我运行情节时,它给了我一个错误...这是我运行的test.sub&lt;-within(test.sub, V1&lt;-as.Date(V1, format = "%Y-%b-%d")) &gt; xtemp&lt;-xts(test.sub[,2],test.sub[,1]) &gt; plot(xtemp) Error in if (on == "years") { : missing value where TRUE/FALSE needed
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-11
    • 2013-01-30
    • 1970-01-01
    • 1970-01-01
    • 2013-02-20
    相关资源
    最近更新 更多