I am trying to arrange two gganimate plots side-by-side. To do so I:
- Create the animations using gganimate
- Convert the animations to magick-images (using
image_read()) - Create a combined plot for each frame using
image_append()
The problem i am having is that using image_read() in step 2 creates two objects of different sizes, despite the nframes value in the animate() call being the same. This results in me being unable to stick the two images together.
I won't add all my code as the dataframes are large, but this is essentially what is happening:
library('ggplot2')
library('gganimate')
library('magick')
#Set the number of frames for the animation
nframes = 513
#Create the first plot
a1 <- ggplot(...) + transition_manual()
#Animate the first plot
anim1 <- animate(a1, nframes = nframes, fps = 9, height = 600, width = 600)
#Create the second plot
a2 <- ggplot(...) + transition_reveal()
#Animate the second plot
anim2 <- animate(a2, nframes = nframes, fps = 9, height = 200, width = 600)
#Convert to magick-images
anim1_magic <- image_read(anim1)
anim2_magic <- image_read(anim2)
#Inspect the results
length(anim1_magic)
> 513
length(anim2_magic)
> 466
I can't work out what is going on: the number of frames is the same, but image_read() treats them differently. This means that I cannot join the two frames using the following code:
anim1_2_magic <- image_append(c(anim1_magic[1], anim2_magic[1]), stack = T)
for(i in 2:nframes) {
combined <- image_append(c(anim1_magic[i], anim2_magic[i]), stack = T)
anim1_2_magic <- c(anim1_2_magic, combined)
}
The other strange thing is that this seems to work sometimes, but not others. I can't work out what image_read() is doing from the docs. Perhaps there is a way to count the number of frames in the gganimate gif (to see if the problem is there), but I can't find a tool for that either.