R中的时间/日期实时图 [英] Time/Date real time plot in R

查看:148
本文介绍了R中的时间/日期实时图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实时绘制一些时间数据.

I want to plot some data in real-time against time.

我使用的代码与此非常相似,取自我在此处阅读的另一个答案:

The code I am using is very similar to this, taken from another answer I read on here:

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

我想在图表的x轴上绘制时间.所以我这样做了:

I would like to plot the time on the x-axis of the graph. So I did this:

n=1000
df=data.frame(time=0,y=runif(n))
window=100
for(i in 1:(n-window)) 
{

    **df$time[i] <- format(Sys.time(),"%X")**

    flush.console()
    plot(df$time,df$y,type='l',xlim=c(i,i+window))
    Sys.sleep(0.1)
}

但是我遇到了错误,请问您有任何帮助或建议吗?

But I get errors, do any of you have any help/ advice

非常感谢,

编辑

我希望它以设定的频率运行900次.我可以在for循环之前计算时间值吗?例如

I want this to run 900 times, at a set frequency. Could I calculate the time values ahead of the for loop. e.g.

df $ DateTime [1] = Sys.time()

df$DateTime[1] = Sys.time()

FOR(;;) df $ DateTime [i] = df $ DateTime [i] + X秒
结束循环

FOR(;;) df$DateTime[i] = df$DateTime[i] + X seconds
END FOR LOOP

FOR(;;) ////模拟实时绘图? 结束循环

FOR(;;) //// simulate live plotting ? END FOR LOOP

推荐答案

要绘制时间,您需要一个时间对象.我对您的代码进行了一些修改,以实际具有时间向量.另外,我更改了索引向量的方式以摆脱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)
}

这篇关于R中的时间/日期实时图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆