【问题标题】:Please interpret this r code for me请为我解释这个 r 代码
【发布时间】:2017-10-30 01:35:00
【问题描述】:
library(tidyverse) # Includes the packages ggplot2 and tidyr, which we         use below

# Get the time values for the time series
Time = attributes(co2)[[1]]
Time = seq(Time[1],Time[2], length.out=(Time[2]-Time[1])*Time[3])

# Convert td to data frame
dat = cbind(Time, with(td, data.frame(Observed=x, Trend=trend,          Seasonal=seasonal, Random=random)))

ggplot(gather(dat, component, value, -Time), aes(Time, value)) +
facet_grid(component ~ ., scales="free_y") +
  geom_line() +
  theme_bw() +
  labs(y=expression(CO[2]~(ppm)), x="Year") +
  ggtitle(expression(Decomposed~CO[2]~Time~Series)) +
  theme(plot.title=element_text(hjust=0.5))

我知道这是关于 ggplot 的时间序列分解。我主要需要第 3 行和第 4 行的解释。我想在代码上应用每月时间序列。

【问题讨论】:

    标签: r time-series


    【解决方案1】:

    您似乎有一个长度至少为 3 的向量 Time

    seq(Time[1],Time[2], length.out=(Time[2]-Time[1])*Time[3])
    

    创建一个从Time[1]Time[2] 的序列,结果向量的长度在Time[2]-Time[1])*Time[3] 中计算。

    以下行只是将一些对象绑定到工作 data.frame 中。 Data.frames 是能够使用 ggplot2 绘图所必需的。 ggplot2 代码或多或少是基本的样板文件,您应该能够自己辨别(参见 superb documentation)。

    你可以自己试试,即使你没有数据。

    Time <- c(1, 10, 5)
    seq(from = Time[1], to = Time[2], length.out = (Time[2] - Time[1]) * Time[3])
    
    [1]  1.000000  1.204545  1.409091  1.613636  1.818182  2.022727  2.227273
    [8]  2.431818  2.636364  2.840909  3.045455  3.250000  3.454545  3.659091
    [15]  3.863636  4.068182  4.272727  4.477273  4.681818  4.886364  5.090909
    [22]  5.295455  5.500000  5.704545  5.909091  6.113636  6.318182  6.522727
    [29]  6.727273  6.931818  7.136364  7.340909  7.545455  7.750000  7.954545
    [36]  8.159091  8.363636  8.568182  8.772727  8.977273  9.181818  9.386364
    [43]  9.590909  9.795455 10.000000
    

    我会要求你用你的数据进行重置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-06
      • 2015-08-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多