Parameter Issue from plotting

Found an interesting animated plot from here:
https://davetang.org/muse/2015/02/12/animated-plots-using-r/

When running following code, got error message "Invalid Parameter - -delay", can anyone help to fix the problem? Thank you.

#number of frames or plots
frames <- 50
 
# function for creating file name with leading zeros
# makes it easier to process them sequentially
rename <- function(x){
  if (x < 10) {
    return(name <- paste('000',i,'plot.png',sep=''))
  }
  if (x < 100 && i >= 10) {
    return(name <- paste('00',i,'plot.png', sep=''))
  }
  if (x >= 100) {
    return(name <- paste('0', i,'plot.png', sep=''))
  }
}
 
#loop through plots
for(i in 1:frames){
  name <- rename(i)
   
  #saves the plot as a .png file in the working directory
  png(name)
  sd <- 10
  n  <- 10000
  factor <- i * 2
  m  <- 50 + factor
  x  <- rnorm(n, m, sd)
  hist(x,
       xlim=c(0,200),
       ylim=c(0,2000),
       main = paste('Histogram of rnorm() n = ', n, ' mean = ', m, ' sd = ', sd),
       )
  dev.off()
}
 
#run ImageMagick
my_command <- 'convert *.png -delay 3 -loop 0 animation.gif'
system(my_command)

try using 'magick'
Animation Basics -- ImageMagick Examples

1 Like

Hi,

You didn't give detail of your environment, but from the error message I suppose that you are running on Windows. 'convert' is a built-in Windows command that has nothing to do with ImageMagick, so that is why you get the error message.

First, you can simplify the creation of png files.


#number of frames or plots
frames <- 50

png("%04dplot.png")

#loop through plots
for(i in 1:frames){
    
    #saves the plot as a .png file in the working directory
    sd <- 10
    n  <- 10000
    factor <- i * 2
    m  <- 50 + factor
    x  <- rnorm(n, m, sd)
    hist(x,
         xlim=c(0,200),
         ylim=c(0,2000),
         main = paste('Histogram of rnorm() n = ', n, ' mean = ', m, ' sd = ', sd),
    )
    
}

dev.off()

Then, install recent ImageMagick and run

#run ImageMagick ffmpeg
# no loop
my_command <- 'ffmpeg -i %4dplot.png -r 10 -loop -1 animation.gif'
# infinite loop
my_command <- 'ffmpeg -i %4dplot.png -r 10 -loop -0 animation.gif'
system(my_command)

It works for me.
Hope this helps.

1 Like

Thank you! It works.

Is it possible to generate a mp4 file? the vedio one.

my_command <- 'ffmpeg -framerate 5 -i %4dplot.png -r 10 -vf format=yuv420p animation.mp4'

1 Like

Thank you so much!
One last question: What do "-i" , "-r 10" mean (in 'ffmpeg -framerate 5 -i %4dplot.png -r 10 -vf format=yuv420p animation.mp4') ?

Hi,

-i designates input.
-r defines the frame rate (Hz value) for the output.
-framerate is an input per-file option. It is meant for input formats which don't have a framerate defined, image sequences being an example.
You can play with -framerate and -r to achieve the desired animation speed.

Hope this helps.
Zivan

1 Like

Got it, thank you!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.