Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple gganimate plots side by side

I would like to display animations made by the package gganimate side by side or at least in the same document.

Plots to work with:

library(ggplot2)
library(gganimate)

anime_one <- 
  ggplot(mtcars, aes(factor(cyl), mpg)) + 
  geom_boxplot() + 
  # Here comes the gganimate code
  transition_states(
    gear,
    transition_length = 2,
    state_length = 1
  ) +
  enter_fade() + 
  exit_shrink() +
  ease_aes('sine-in-out')

anime_two <- 
  ggplot(mtcars, aes(factor(cyl), mpg, fill = factor(cyl))) + 
  geom_boxplot() + 
  # Here comes the gganimate code
  transition_states(
    gear,
    transition_length = 2,
    state_length = 1
  ) +
  enter_fade() + 
  exit_shrink() +
  ease_aes('sine-in-out')

First attempt:

library(patchwork)
anime_one + anime_two  

Error in UseMethod("ggplot_gtable") : 
no applicable method for 'ggplot_gtable' applied to an object of class 
"gganim_built"

Second attempt:

library(patchwork)
animate(anime_one+anime_two)

This actually renders and doesn't give an error, but the result is an animation of only the last "gganim_built" object (here anime_two)

Third attempt (Works, but not really what I am looking for)
The only halfway solution I have found is to include everything in a Rmarkdown document, and then make codecunks with animation calls (only one animation call pr codechunk otherwise it doesn't work), and finally knit the document to HTML:

```{r}
animate(anime_one)
```

```{r}
animate(anime_two)
```

Has anyone managed to make gganimate objects appear side by side?

Thanks in advance.

like image 707
Steen Harsted Avatar asked Jan 09 '19 11:01

Steen Harsted


1 Answers

Seems that gganimate help docs are being rewritten and maybe you can find a solution about "animation composition" here:

https://github.com/thomasp85/gganimate/wiki/Animation-Composition

Under the title "Placing Animations side-by-side with magick" there's a method to do such thing. Hope this help

like image 78
Forge Avatar answered Sep 17 '22 21:09

Forge