Cross Validated is a question and answer site for people interested in statistics, machine learning, data analysis, data mining, and data visualization. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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


share|improve this question
up vote 7 down vote accepted

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().

share|improve this answer
(+1) I forgot the Sys.sleep() function. I think the $x$- and $y$-axis range should bet set up in advance, no? – chl Jan 6 '11 at 12:26
@chl, yes the $x$ and $y$ axis ranges should be set up in advance. – mpiktas Jan 6 '11 at 13:30
@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 and adding all the stuff like axes or labels by hand. – mbq Jan 6 '11 at 13:49
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. – RockScience Jan 7 '11 at 3:06

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

dd <- replicate(3, rnorm(100))

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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