在 R 中,绘制随机过程的长时序时更改时间标签

huangapple go评论96阅读模式
英文:

In R, changing time labels when plotting a long realization of a stochastic process

问题

I am trying to plot a long realization (T=5000) of a certain Time Series. I want a unique plot with 5x1 subplots. So, I would like to set the time labels as 1:1000, 1001:2000,..., 4001:5000.

  1. T = 5000
  2. X = rnorm(T)
  3. # plot the time series
  4. par(mfrow = c(5,1), mar = c(2,2,1,1))
  5. for (i in 1:5) {
  6. start_idx <- (i-1) * 1000 + 1
  7. end_idx <- i * 1000
  8. plot(X[start_idx:end_idx], type="l", xlab="time", ylab="X_t^n")
  9. }

The plot is shown in the image. Note that in each subplot, the time labels are all the same.

英文:

I am trying to plot a long realization (T=5000) of a certain Time Series. I want a unique plot with 5x1 subplots. So, I would like to set the time labels as 1:1000, 1001:2000,..., 4001:5000.

  1. T= 5000
  2. X=rnorm(T)
  3. # plot the time series
  4. par(mfrow = c(5,1), mar = c(2,2,1,1))
  5. for (i in 1:5) {
  6. start_idx &lt;- (i-1) * 1000 + 1
  7. end_idx &lt;- i * 1000
  8. plot(X[start_idx:end_idx], type=&quot;l&quot;, xlab=&quot;time&quot;, ylab=&quot;X_t^n&quot;)
  9. }

The plot is
在 R 中,绘制随机过程的长时序时更改时间标签
Note that in each subplot, the time labes are all the same.

答案1

得分: 2

plot 函数接受一个可选的 y 参数,所以在这里,您可以分配相应的时间。请参阅下面我修改的示例,其中我创建了一个等长的时间对象 tt,并将其传递给 x 参数。

此外,请注意,在 R 中,T 是一个保留字,因此最好小心使用它作为变量名。因此,我将 T 重命名为 sample_size

  1. sample_size = 5000
  2. tt = 1:sample_size # 用您数据集中相应的时间/日期替换
  3. X = rnorm(sample_size)
  4. # 绘制时间序列
  5. par(mfrow = c(5,1), mar = c(2,2,1,1))
  6. for (i in 1:5) {
  7. start_idx <- (i-1) * 1000 + 1
  8. end_idx <- i * 1000
  9. plot(tt[start_idx:end_idx], X[start_idx:end_idx], type="l", xlab="time", ylab="X_t^n")
  10. }
英文:

plot takes an optional y argument, so here, you can assign the corresponding time. See my modified example below where I create a time object tt of equal length and pass it to the x argument.

As an aside, note that T is a reserved word in R, so it's best to be careful and avoid using it as a variable name. I have, therefore, renamed T to sample_size.

  1. sample_size = 5000
  2. tt = 1:sample_size #replace with your corresponding time/dates in your dataset
  3. X = rnorm(sample_size)
  4. # plot the time series
  5. par(mfrow = c(5,1), mar = c(2,2,1,1))
  6. for (i in 1:5) {
  7. start_idx &lt;- (i-1) * 1000 + 1
  8. end_idx &lt;- i * 1000
  9. plot(tt[start_idx:end_idx], X[start_idx:end_idx], type=&quot;l&quot;, xlab=&quot;time&quot;, ylab=&quot;X_t^n&quot;)
  10. }

答案2

得分: 1

Instead of plotting just the y-axis values in the plot, add the x= and the y= explicitly:

  1. ...
  2. plot(seq(start_idx, end_idx), X[start_idx:end_idx], type="l", xlab="time", ylab="X_t^n")
  3. ...
英文:

Instead of plotting just the y-axis values in the plot, add the x= and the y= explicitly:

  1. ...
  2. plot(seq(start_idx, end_idx), X[start_idx:end_idx], type=&quot;l&quot;, xlab=&quot;time&quot;, ylab=&quot;X_t^n&quot;)
  3. ...

huangapple
  • 本文由 发表于 2023年5月15日 07:16:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76250056.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定