Animation/GIF's in R

Suppose I have the three following plots:

p1 = hist(rnorm(1000,1,1), 10000)
p2 = hist(rnorm(1000,1,1), 10000)
p3 = hist(rnorm(1000,1,1), 10000)

Is there any way that these can be made into a "gif"?

For example:

library(gifski)
gif_file <- ( "C:/Users/OneDrive/Documents/ref.gif")
save_gif((p1, p2, p3), gif_file, 1280, 720, res = 144)

I am not sure if this statement is intended to be used this way. Does anyone know if this is possible?

(Note: I know that you can do this with a loop (i.e. create p1, p2, p3 with a loop) , but I am specifically interested in knowing if this can be done when p1, p2, p3 have already been created. I also know that this is relatively straightforward to do if the plots were made in ggplot, but I am trying to do this with plots made in base R.).

Thanks!

try this

library(gifski)
library(purrr)

make_a_hist <- function() {
  plot.new()
  hist(rnorm(1000, 1, 1), 10000)
  recordPlot()
}

list_of_plots <- map(1:3,~make_a_hist())

gif_file <- file.path(getwd(), "three.gif")
save_gif(expr = walk(list_of_plots,
                            replayPlot),
         gif_file, 1280, 720, res = 144)
1 Like

This topic was automatically closed 21 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.