I would like to generate an animated graphic in PDF using the LaTeX animate package.
---
title: "test_animations"
author: "Colours"
date: "27/10/2017"
output:
pdf_document:
includes:
in_header: header_ani.tex
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
pacman::p_load(gganimate, gapminder, ggplot2)
```
## Test animations
```{r sample_ani, fig.show='animate', message=FALSE, warning=FALSE}
p2 <- ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop)) +
geom_point() +
geom_point(aes(frame = year), color = "red") +
scale_x_log10()
gganimate(p2, saver = "gif")
```
header_ani.tex
\usepackage{animate}
Warning: Ignoring unknown aesthetics: frame
Quitting from lines 20-25 (second_animation.Rmd) Error: Could not find ffmpeg command. You should either change the animation.fun hook option or install ffmpeg with libvpx enabled. Execution halted
Why the reference to ffmpeg. According to the knitr documentation:
When the chunk option fig.show='animate' and there are multiple plots produced from a code chunk, all plots will be combined to an animation. For LaTeX output, the LaTeX package animate is used to create animations in PDF. For HTML/Markdown output, by default FFmpeg is used to create a WebM video. Note you have to enable the libvpx support when installing FFmpeg. Linux and Windows users can just follow the download links on the FFmpeg website (libvpx has been enabled in the binaries). For OS X users, you can install FFmpeg via Homebrew
ffmpeg should be used in conversion to html. Is it because of the RMarkdown's pipeline?
(RStudio: RMarkdown)
that forces use of ffmpeg somewhere along the line?
Is it possible to make use of the animate package in a RMarkdown document and avoid ffmpeg so the obtained PDF has the following component with conrtols offred by the animate package.
(Not the chart I want to generate but shows how the animated graphic should be embedded in the PDF, taken from the animate package documentation referenced above.)
Drawing on a similar answer (Plot animation in knitr rmarkdown) and a LaTeX-related discussion I have came up with the following solution:
---
title: "test_animations"
author: "Colours"
date: "27/10/2017"
classoption: landscape
output:
pdf_document:
keep_tex: true
includes:
in_header: header_ani.tex
---
```{r setup, include=FALSE}
Vectorize(require)(package = c("knitr"),
character.only = TRUE)
opts_chunk$set(echo = FALSE,
cache = TRUE)
pacman::p_load(gganimate, gapminder, ggplot2)
```
## Test animations
```{r sample_ani, message=TRUE, warning=TRUE, echo=TRUE, }
p2 <- ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop)) +
geom_point() +
geom_point(aes(frame = year), color = "red") +
scale_x_log10()
gganimate(p2, filename = "animation2.gif", title_frame = TRUE) -> amimate
```
<!-- Notes on the answer: https://tex.stackexchange.com/a/240387/123504 -->
```{r convert_shele, echo=TRUE, message=TRUE, warning=TRUE, paged.print=FALSE}
# Extra options for resize can be added
system(command = "convert -coalesce animation2.gif something.png")
```
\animategraphics[loop,controls,width=\linewidth]{12}{something-}{0}{12}
# Latex code generating animation
Figures 0 - 12 should reflect frames
~~~
\animategraphics[loop,controls,width=\linewidth]{12}{something-}{0}{12}
~~~
The header file is as in question. The code generates and animated graph with the required controls.
Snapshot of generated animation with the controls available.
convert
command. I reckon that this is not optimal as it involves converting the graph back and forth. {12}{something-}{0}{12}
have to be set manually with the first figure reflecting frame rate (decreasing will slow down the animation) something-
corresponds to files generated via convert
that are named something-0.png ... something-n.png
and {0}{12}
corresponds to the file numbers. If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With