我正在尝试创建一个动态的情节,有点像自动更新,增量,可能是实时。 我想完成这样的事情 http://www.youtube.com/watch?v=s7qMxpDUS3c
这是我到目前为止所做的。假设您在data.frame中有一个名为temp的时间序列。第一列是时间,第二列是值的位置。
for(i in 1: length(temp$Time[1:10000]))
{
flush.console()
plot(temp$Time[i:i+100],temp$Open[i:i+100],
xlim=c(as.numeric(temp$Time[i]),as.numeric(temp$Time[i+150])),
ylim=c(min(temp$Open[i:i+100]),max(tmep$Open[i:i+120])))
Sys.sleep(.09)
}
这确实是逐步绘制的,但我没有获得100个单位的长时间序列,而是只获得一个点更新。
答案 0 :(得分:24)
你想做这样的事吗?
n=1000
df=data.frame(time=1:n,y=runif(n))
window=100
for(i in 1:(n-window)) {
flush.console()
plot(df$time,df$y,type='l',xlim=c(i,i+window))
Sys.sleep(.09)
}
完成代码:
# for(i in 1: length(temp$Time[1:10000])) {
for (i in 1:10000) # The length of a vector of 10000 is always 10000
flush.console()
# plot(temp$Time[i:i+100],temp$Open[i:i+100],
# Don't bother subsetting the x and y variables of your plot.
# plot() will automatically just plot those in the window.
plot(temp$Time,temp$Open,
xlim=c(as.numeric(temp$Time[i]),as.numeric(temp$Time[i+150]))
# Why are you setting the y limits dynamically? Shouldn't they be constant?
# ,ylim=c(min(temp$Open[i:i+100]),max(tmep$Open[i:i+120])))
Sys.sleep(.09)
}