简体   繁体   English

在 knitr rmarkdown 中绘制动画

[英]Plot animation in knitr rmarkdown

Problem:问题:

Hi, I try to make an animated plot in a rmarkdown document.嗨,我尝试在 rmarkdown 文档中制作动画图。 Here is my code:这是我的代码:

```{r lmSim, fig.show='animate'}
library(animation)
library(plyr)

oopt = ani.options(interval = 0.3, nmax = 101)

a <- sort(rnorm(100, 2))
b <- sort(rnorm(100, 7))
out <- vector("list", 101)
for (i in 1:ani.options("nmax")) {
  ji <- seq(from = 0, to = 5, by = .05)
  a <- jitter(a, factor = 1, amount = ji[i])
  fab1 <- lm(a ~ b)
  coe <- summary(fab1)$coefficients
  r2 <- summary(fab1)$r.squared
  if (coe[2, 4] < .0001) p <- " < .0001"
  if (coe[2, 4] < .001 & coe[2, 4] > .0001) p <- " < .001"
  if (coe[2, 4] > .01) p <- round(coe[2, 4], 3)
  plot(a ~ b, main = "Linear model")
  abline(fab1, col = "red", lw = 2)
  text(x = min(b) + 2, y = max(a) - 1, 
       labels = paste("t = ", round(coe[2, 3], 3), ", p = ", p, ", R2 = ", round(r2, 3)))
  out[[i]] <- c(coe[2, 3], coe[2, 4], r2)
  ani.pause()
  }

ani.options(oopt)
```

The loop work fine, and passed into a function, I'am able to save it in several formats with 'saveLatex', 'saveHTML' or 'saveVideo'.循环工作正常,并传递给一个函数,我可以使用“saveLatex”、“saveHTML”或“saveVideo”以多种格式保存它。 However, when 'knit' the .Rmd file in order to obtain a PDF, the animation does not appear, there is just this line written:但是,当“编织” .Rmd 文件以获取 PDF 时,动画不会出现,只写了这一行:

video of chunk lmSim

If I knit it in HTML, only the play button of the video is displayed.如果我用 HTML 编织它,则只显示视频的播放按钮。 However, If I open the HTML in my browser (firefox) it is displayed correctly.但是,如果我在浏览器 (firefox) 中打开 HTML,它会正确显示。

There is no error message displayed.没有显示错误消息。 I'm using R version 3.2.0, the latest R Studio version, 1.10.5 knitr version on a MacBook Pro Yosemite.我在 MacBook Pro Yosemite 上使用 R 版本 3.2.0、最新的 R Studio 版本、1.10.5 knitr 版本。 I didn't find any relevant information or documentation to solve my problem.我没有找到任何相关信息或文档来解决我的问题。

Questions:问题:

So, is it simply possible to have an embeded animation into a PDF generated with rmarkdown/knitr ?那么,是否可以将嵌入动画嵌入到使用 rmarkdown/knitr 生成的 PDF 中?

Do I have to install another program to deal with videos in PDF (I have ffmpeg on my computer)?我是否必须安装另一个程序来处理 PDF 格式的视频(我的计算机上有 ffmpeg)?

Many thanks!非常感谢!

Thanks Yihui!谢谢奕辉! It works very well with the following settings (reading the PDF with Adobe):它适用于以下设置(使用 Adob​​e 阅读 PDF):

---
title: "Sim"
author: ""
header-includes:
   - \usepackage{animate}
...
---

```{r lmSim, fig.show='animate', out.width = '6in'}

Have you tried with ggplot and gganimation?你试过 ggplot 和 gganimation 吗? For example:例如:

library(gapminder)
library(gganimate)
library(ggplot)

head(gapminder)
theme_set(theme_bw())

p <- ggplot(gapminder, aes(gdpPercap, lifeExp, size = 1, color = continent, frame = year)) +
  geom_point() +
  scale_x_log10()


animation<-gganimate(p, "your_animation.gif")#Or to your path

See that you need to save first your animation.看到您需要先保存动画。 You need to have establish your work directory.您需要建立您的工作目录。

After you can call it from the chat or html in markdown (not from the R chuck) like:在你可以在 markdown 中从聊天或 html 调用它之后(而不是从 R 卡盘),例如:

![your caption here](your_animation.gif)

May be it's too late for this answer, but hopefully should help others.这个答案可能为时已晚,但希望可以帮助其他人。 I recommend to save and run the R code as a separate file in the same directory and only import the animation as a gif to reduce lag in the presentation.我建议将 R 代码作为单独文件保存并运行在同一目录中,并且仅将动画导入为 gif 以减少演示中的延迟。 This works for me in ioslides:这在 ioslides 中对我有用:

## Time series visualization

![time series](myanimation.gif){width=80%}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM