install.packages("animation") library(animation)
ffmpeg = "C:/Program Files (x86)/Path/to/ffmpeg/file.exe"
brew install imagemagick
# Makes an image for every value of i sine_func <- function(i){
sineplot <- ggplot(data.frame(x=c(0,i)), aes(x)) +
stat_function(fun=sin) +
theme(axis.title = element_text(size=16),
axis.text = element_text(size=12))
return(sineplot)
} # Loops through all n images and turns them into a GIF n <- 25 saveGIF({
ani.options(interval = 0.15, nmax = n)
for (i in 1:n){
myplot <- sine_func(i)
print(myplot)
ani.pause()}
This produces the following animation: In general, GIF format is quite large. Video formats tend to have more customizability in terms of video quality, so they are more space efficient (you can also scroll through them). To make the same animation but in video format, you would use the following.}, movie.name = "./whatsyoursine.gif", ani.width = 500, ani.height = 400)
saveVideo({
ani.options(interval = 0.15, nmax = n)
for (i in 1:n){
myplot <- sine_func(i)print(myplot)ani.pause()}
}, video.name = "./whatsyoursine.mp4", other.opts = "-pix_fmt yuv420p -b:v 500K", #ffmpeg = "C:/Program Files (x86)/Path/to/ffmpeg/executable/file.exe",
ani.width = 500, ani.height = 400)If you are on Windows and you wanted to produce a video, you would have to uncomment the "ffmpeg =" line above and make the path refer to your ffmpeg installation.
Barrick Lab > ComputationList > RAnimation