animation of subplots

Hello all,

I have read the documentation of gganimate to animate plots. But how can i animate if there are subplots (scatter plots & histograms, merged together using grid.arrange) in the loop?

Can someone please modify the sample code to illustrate how to animate and create a video of plot p (with multiple subplots).

The original purpose is to save weights of logistic regression off all epochs while training 2D data. Later create a animation video illustrating -
a. hyperplane
b. loss function
c. histogram of probabilities

in different subplots at each epoch during the training process

library(tidyverse)
library(gridExtra)
library(grid)

# loop
for (m in 1:100)
{
  # line
  x <- seq(1, 10000, 1)
  y <- m*x
  df1 <- tibble(x, y)
  
  p_line <- ggplot(data = df1,
                   aes(x = x,
                       y = y)) +
    geom_point() + 
    theme_bw()
  
  
  # histogram
  x <- rnorm(10000, mean = m, sd = 2*m)
  df2 <- tibble(x)
  
  p_hist <- ggplot(data = df2,
                   aes(x = x)) +
    geom_histogram(bins = 100) +
    theme_bw()
    
  p <- grid.arrange(p_line, p_hist, nrow = 2)
}

If you have a function that can produce the plots for each iteration that you want to animate, you can use the av::av_capture_graphics() function to produce a nice mp4 file with transitions.

2 Likes

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.

Thank you, that was very helpful