Hello: I want to present a sequence of plots as an animation. As a toy example consider the code function(n){for (i in 1:n){ plot(1:100,sin(i*(1:100)),type="l") title(paste("n=",i)) segments(0,0,100,0,col=2) }} This sort-of works on a MacOS platform, but the rendering of the plots is a bit choppy. Inserting a sleep function allows the plots to evolve smoothly. function(n){for (i in 1:n){ plot(1:100,sin(i*(1:100)),type="l") title(paste("n=",i)) segments(0,0,100,0,col=2) Sys.sleep(.2) }} However, on a Windows platform, only the last plot is rendered without the Sys.sleep, so the dynamic element is lost. Inserting the Sys.sleep does allow all the plots to be rendered, but they seem to be erased before they are drawn again, so there is substantial flicker in the appearance. Is there some kind of double-buffering available within R, so that plots are rendered only after they are fully drawn, leaving the previous plot visible until it is replaced? I just used the default graphics driver on Windows ? is there perhaps a different driver that will the graphics smoother? Mik Bickis Professor Emeritus Department of Mathematics and Statistics University of Saskatchewan
A search on "make animated plots in R" brought up many hits and the gganimate package (and maybe others, as I didn't scroll through). Bert On Fri, Mar 22, 2024, 18:45 Bickis, Mikelis <bickis at math.usask.ca> wrote:> Hello: > > I want to present a sequence of plots as an animation. As a toy example > consider the code > > function(n){for (i in 1:n){ > plot(1:100,sin(i*(1:100)),type="l") > title(paste("n=",i)) > segments(0,0,100,0,col=2) > }} > > This sort-of works on a MacOS platform, but the rendering of the plots is > a bit choppy. Inserting a sleep function allows the plots to evolve > smoothly. > > function(n){for (i in 1:n){ > plot(1:100,sin(i*(1:100)),type="l") > title(paste("n=",i)) > segments(0,0,100,0,col=2) > Sys.sleep(.2) > }} > > However, on a Windows platform, only the last plot is rendered without the > Sys.sleep, so the dynamic element is lost. Inserting the Sys.sleep does > allow all the plots to be rendered, but they seem to be erased before they > are drawn again, so there is substantial flicker in the appearance. > > Is there some kind of double-buffering available within R, so that plots > are rendered only after they are fully drawn, leaving the previous plot > visible until it is replaced? I just used the default graphics driver on > Windows ? is there perhaps a different driver that will the graphics > smoother? > > Mik Bickis > Professor Emeritus > Department of Mathematics and Statistics > University of Saskatchewan > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
Hi Take a look at dev.hold() and dev.flush() Paul On 23/03/24 06:43, Bickis, Mikelis wrote:> Hello: > > I want to present a sequence of plots as an animation. As a toy example > consider the code > > function(n){for (i in 1:n){ > plot(1:100,sin(i*(1:100)),type="l") > title(paste("n=",i)) > segments(0,0,100,0,col=2) > }} > > This sort-of works on a MacOS platform, but the rendering of the plots > is a bit choppy. Inserting a sleep function allows the plots to evolve > smoothly. > > function(n){for (i in 1:n){ > plot(1:100,sin(i*(1:100)),type="l") > title(paste("n=",i)) > segments(0,0,100,0,col=2) > Sys.sleep(.2) > }} > > However, on a Windows platform, only the last plot is rendered without > the Sys.sleep, so the dynamic element is lost. Inserting the Sys.sleep > does allow all the plots to be rendered, but they seem to be erased > before they are drawn again, so there is substantial flicker in the > appearance. > > Is there some kind of double-buffering available within R, so that plots > are rendered only after they are fully drawn, leaving the previous plot > visible until it is replaced? I just used the default graphics driver on > Windows ? is there perhaps a different driver that will the graphics > smoother? > > Mik Bickis > Professor Emeritus > Department of Mathematics and Statistics > University of Saskatchewan > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > <https://stat.ethz.ch/mailman/listinfo/r-help> > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > <http://www.R-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code.-- Dr Paul Murrell (he/him) Te Kura Tatauranga | Department of Statistics Waipapa Taumata Rau | The University of Auckland Private Bag 92019, Auckland 1142, New Zealand 64 9 3737599 x85392 paul at stat.auckland.ac.nz www.stat.auckland.ac.nz/~paul/