英文:
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 <- (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
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 <- (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")
}
答案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="l", xlab="time", ylab="X_t^n")
...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论