3

The code below generates what appears almost as an animation in R studio because it renders 100 plots, each plot has slightly more data than then previous plot.

################################################################################
# Visualise probability of heads tending towards 0.5 as more tests performed

# Number of tests to run
tests <-  100

# duration to run for in seconds
durationSeconds <- 10

ht <- sample(c('heads', 'tails'), tests, replace=TRUE)
total <- vector()
for (i in 1:tests) {
  headsAtI <- length(which(ht[1:i] == 'heads'))
  total[i] <- headsAtI/i
  Sys.sleep(durationSeconds/tests)
  plot(total, type='l')
  abline(h = 0.5, col='blue')
}

This works but has some serious issues:

  1. 100 plots are created, I guess ideally one should be created and re-used
  2. If I change the value of 'tests' to be say 10,000, R Studio will hang or crash

What is the proper way to do this in R Studio?

I realise that I can just draw a single plot at the end, with all the "results", but I want to achieve the "animation" effect.

enter image description here

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
MattG
  • 5,589
  • 5
  • 36
  • 52

1 Answers1

4

Have you considered gganimate ?

library(dplyr)
library(gganimate)
library(ggplot2)

tests <-  100
ht <- sample(c('heads', 'tails'), tests, replace=TRUE)

p <- data.frame(ht) %>%
       mutate(headsAtI = cumsum(ht == 'heads'),
              index = row_number(),
              total = headsAtI/index) %>%
       ggplot() + aes(index, total) + geom_line() + 
       geom_hline(yintercept = 0.5, color = 'blue') + 
       transition_reveal(index)
p

enter image description here

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • This solution looks perfect, thanks, but I have had some trouble recreating. First I got a warning about no "animators" installed, so I installed and loaded "gifski" then I got warnings about "file_renderer failed to copy frames to the destination directory". So I set my working directory "setwd". Then I see 100 .png files created in the working directory, but no gif/avi file and no animation in RStudio Viewer. On each re-run I have to delete the .png files or I get the "file_renderer failed to copy frames to the destination directory" error again. – MattG Jul 22 '20 at 05:36
  • Seems like installation problem. Do you have `gganimate` installed? If you have it without any warnings/error, using the above code as it is should create the above plot in your Rstudio Viewer. To export it I used two more commands. `anim <- animate(p)` and `anim_save("filename.gif", anim)` . – Ronak Shah Jul 22 '20 at 05:40
  • this part ran without errors or warnings: library(dplyr) library(gganimate) library(ggplot2) library(gifski) tests <- 100 ht <- sample(c('heads', 'tails'), tests, replace=TRUE) p <- data.frame(ht) %>% mutate(headsAtI = cumsum(ht == 'heads'), index = row_number(), total = headsAtI/index) %>% ggplot() + aes(index, total) + geom_line() + geom_hline(yintercept = 0.5, color = 'blue') + transition_reveal(index) p And gend 100 files, but line "anim <- animate(p)" generates this warning "file_renderer failed to copy frames to the destination directory" – MattG Jul 22 '20 at 05:53
  • These are the two posts that I could find https://stackoverflow.com/questions/60259080/gganimate-fail-to-render-an-animation and https://community.rstudio.com/t/warning-message-file-renderer-failed-to-copy-frames-to-the-destination-directory/45261/5 Do you have permissions to write in the file? Strangely though, I do not get any warnings or error. – Ronak Shah Jul 22 '20 at 05:59
  • Yes, it looks like I am having those issues. I also get this same issue for other gganimate usage, so there is something wrong with my install/setup. I will work through it and create another question if necessary. The packages are installed, working directory is set, old files deleted and I have permissions to the directories. I think that covers all the possible answers in those posts and I still have the issue :( – MattG Jul 22 '20 at 06:12
  • 1
    Rebooting Mac OS fixed it. Works perfectly now, thanks for the beautiful solution. – MattG Jul 22 '20 at 06:34