在Knitr Rmarkdown中绘制动画 [英] Plot animation in knitr rmarkdown

查看:114
本文介绍了在Knitr Rmarkdown中绘制动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:

您好,我尝试在rmarkdown文档中制作动画图.这是我的代码:

Hi, I try to make an animated plot in a rmarkdown document. 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)
```

循环工作正常,并传递给一个函数,我能够使用"saveLatex","saveHTML"或"saveVideo"将其保存为几种格式.但是,当编织" .Rmd文件以获取PDF时,动画不会出现,只是写了这一行:

The loop work fine, and passed into a function, I'am able to save it in several formats with 'saveLatex', 'saveHTML' or 'saveVideo'. However, when 'knit' the .Rmd file in order to obtain a PDF, the animation does not appear, there is just this line written:

video of chunk lmSim

如果将其编织为HTML,则仅显示视频的播放按钮.但是,如果我在浏览器(firefox)中打开HTML,则会正确显示.

If I knit it in HTML, only the play button of the video is displayed. However, If I open the HTML in my browser (firefox) it is displayed correctly.

没有显示错误信息.我在MacBook Pro Yosemite上使用的是R版本3.2.0,最新的R Studio版本,1.10.5编织器版本.我没有找到任何相关信息或文档来解决我的问题.

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. I didn't find any relevant information or documentation to solve my problem.

问题:

那么,是否可以简单地将动画嵌入到使用rmarkdown/knitr生成的PDF中?

So, is it simply possible to have an embeded animation into a PDF generated with rmarkdown/knitr ?

我是否需要安装另一个程序来处理PDF视频(我的计算机上有ffmpeg)?

Do I have to install another program to deal with videos in PDF (I have ffmpeg on my computer)?

非常感谢!

感谢亿辉!它在以下设置(使用Adobe阅读PDF)中效果很好:

Thanks Yihui! It works very well with the following settings (reading the PDF with Adobe):

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

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

推荐答案

您是否尝试过使用ggplot和gganimation?例如:

Have you tried with ggplot and 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.

您可以通过聊天或markdown中的html调用它(而不是R卡盘),如下所示:

After you can call it from the chat or html in markdown (not from the R chuck) like:

![your caption here](your_animation.gif)

这篇关于在Knitr Rmarkdown中绘制动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆