【问题标题】:Real time, auto updating, incremental plot in RR中的实时,自动更新,增量绘图
【发布时间】:2012-07-07 03:04:39
【问题描述】:

我正在尝试制作动态图,有点像自动更新、增量,可能是实时的。 我想完成这样的事情 http://www.youtube.com/watch?v=s7qMxpDUS3c

这是我到目前为止所做的。假设您在名为 temp 的 data.frame 中有一个时间序列。第一列是时间,第二列是值所在的位置。

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 个单位的长时间序列,而是只更新了一个点。

【问题讨论】:

  • is max(tmep$打开一个错字?应该是 'temp' 不是吗?你是从会话中粘贴的吗?它应该给你一个警告......
  • length(temp$Time[1:10000]) 始终为 10000,即使向量不超过 10000(它被 NA 填充)。 for(i in 1:10000) 应该做得很好。
  • Spacedman,感谢您的回复。第一个是错别字。第二不应该对实际结果做出任何改变。事实上我试过了,它什么也没做。
  • 当数据进入时 temp 如何更新,例如。来自文件?

标签: r plot real-time


【解决方案1】:

你想做这样的事情吗?

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)
}

【讨论】:

  • 天哪,你是我的最爱!我有男子气概地爱你!这正是我想要完成的。谢谢大师。回答你的最后一个问题。它不是恒定的,因为它们是第二个数据的开盘价?
  • @nograpes,这非常有用,谢谢!我怀疑这不适用于ggplot。你知道类似的ggplot2 解决方案吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-03-04
  • 1970-01-01
  • 2012-09-22
  • 2016-12-24
  • 2019-02-13
  • 1970-01-01
  • 2021-08-24
相关资源
最近更新 更多