且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

R中的时间/日期实时图

更新时间:2023-02-26 18:17:24

要绘制时间,您需要一个时间对象.我对您的代码进行了一些修改,以实际具有时间向量.另外,我更改了索引向量的方式以摆脱xlim.

To plot the time, you need a time object. I modified your code a bit to actually have a time vector. Also, I change the way you indexed the vector to get rid of the xlim.

n=1000
df=data.frame(time=Sys.time()+1:n,y=runif(n))
window=100
for(i in 1:(n-window))
{
    flush.console()
    df1 <-df[i:(i+window),]
    plot(df1$time,df1$y,type='l')
    Sys.sleep(0.1)
}

EDIT 解决方案,以增加时间.您可以增加几个小时,但似乎会减慢该过程.如您所见,我添加了axis.POSIXct调用,但是我不得不将Sys.Sleep放慢到0.5,并且您几乎看不到x轴:

EDIT Solution to add hours. You can add hours but it seems to slow down the process. As you can see, I added the axis.POSIXct call, but I had to slow down the Sys.Sleep to 0.5 and you can barely see the x-axis:

n=1000
df=data.frame(time=Sys.time()+1:n,y=runif(n))
window=100
for(i in 1:(n-window))
{
    flush.console()
    df1 <-df[i:(i+window),]
    x_at <-pretty(df1$time)
    x_labels <-format(pretty(df1$time),"%H:%M:%S")
    plot(df1$time,df1$y,type='l',xaxt='n')
    axis.POSIXct(side=1,at=x_at,labels=x_labels)
    Sys.sleep(0.5)
}