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

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

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.

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

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.

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

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

sample_size = 5000 

tt = 1:sample_size # 用您数据集中相应的时间/日期替换

X = rnorm(sample_size)

# 绘制时间序列

par(mfrow = c(5,1), mar = c(2,2,1,1))

for (i in 1:5) {
  start_idx <- (i-1) * 1000 + 1
  end_idx <- i * 1000
  plot(tt[start_idx:end_idx], X[start_idx:end_idx], type="l", xlab="time", ylab="X_t^n")
}
英文:

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.

sample_size = 5000 

tt = 1:sample_size #replace with your corresponding time/dates in your dataset

X = rnorm(sample_size)

# plot the time series

par(mfrow = c(5,1), mar = c(2,2,1,1))

for (i in 1:5) {
  start_idx &lt;- (i-1) * 1000 + 1
  end_idx &lt;- i * 1000
  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;)
}

答案2

得分: 1

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

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

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

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

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:

确定