10
$\begingroup$

THis is a data visualization question. I have a database that contains some data that is constantly revised (online update). What is the best way in R to update a graph every let say 5 or 10 seconds. (without plotting again the all thing is possible)?

fRed

$\endgroup$

2 Answers 2

7
$\begingroup$

Assuming you want to update R windows() or x11() graph, you can use functions like points() and lines() to add new points or extend lines on a graph without redraw; yet note that this won't change the axes range to accommodate points that may go out of view. In general it is usually a good idea to make the plotting itself instantaneous -- for instance by moving computation effort into making some reduced middle representation which can be plotted rapidly, like density map instead of huge number of points or reducing resolution of line plots (this may be complex though).

For holding R session for a certain time without busy wait, use Sys.sleep().

$\endgroup$
4
  • $\begingroup$ (+1) I forgot the Sys.sleep() function. I think the $x$- and $y$-axis range should bet set up in advance, no? $\endgroup$
    – chl
    Jan 6, 2011 at 12:26
  • $\begingroup$ @chl, yes the $x$ and $y$ axis ranges should be set up in advance. $\endgroup$
    – mpiktas
    Jan 6, 2011 at 13:30
  • 1
    $\begingroup$ @mpiktas Or just init the plot with a single call to plot(), possibly with 0,0,type="n" if there is nothing to plot yet... Indeed this is much easier than ding plot.new() and adding all the stuff like axes or labels by hand. $\endgroup$
    – user88
    Jan 6, 2011 at 13:49
  • $\begingroup$ Indeed this seems the simplest... but too bad that the axes are not updated. As I want to update online a time series, and I know at which speed the x axis moves, I guess that I can replot everything every n updates. $\endgroup$ Jan 7, 2011 at 3:06
8
$\begingroup$

For offline visualization, you can generate PNG files and convert them to an animated GIF using ImageMagick. I used it for demonstration (this redraw all data, though):

source(url("http://aliquote.org/pub/spin_plot.R"))
dd <- replicate(3, rnorm(100))
spin.plot(dd)

This generates several PNG files, prefixed with fig. Then, on an un*x shell,

convert -delay 20 -loop 0 fig*.png sequence.gif

gives this animation (which is inspired from Modern Applied Biostatistical Methods using S-Plus, S. Selvin, 1998):

alt text

Another option which looks much more promising is to rely on the animation package. There is an example with a Moving Window Auto-Regression that should let you go started with.

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.