r - 将预制的 gganimate gif 加载到 rmarkdown block 中作为标题

标签 r r-markdown gganimate

我正在尝试将 gif 标题作为 rmarkdown 文件中的图形标题 (fig.cap),以便我可以引用它在我的文字中。 gif 已预先制作,而不是在 rmd 文件中制作,并且不确定如何将其加载回 block 中。假设以下 gif 是通过 rscript 制作并保存的:

library(gganimate)
aim <- ggplot(iris, aes(x = Petal.Width, y = Petal.Length)) + 
  geom_point() +
  transition_states(Species,
                    transition_length = 2,
                    state_length = 1)

animate(aim)
#save animation
anim_save("testing.gif")

现在,如果我打开 rmd 文件并想要插入 gif,我可以执行以下操作:

---
title: "testing"
date: "2/26/2020"
output:
  bookdown::html_document2
---

![this is testing](C:/testing.gif)

它会运行,但只是将其放入文档的文本中。我想给它一个无花果标题,但我无法将 gif 加载到一个 block 中,我尝试了以下不同的变体:

```{r giftest, eval=knitr::is_html_output(), echo=F, fig.show = 'animate', fig.cap = "this is testing"}

![](C:/testing.gif)
```

#also tried:
```{r giftest, eval=knitr::is_html_output(), echo=F, fig.show = 'animate', fig.cap = "this is testing",
fig.process= "C:/testing.gif"}
```

#and
```{r, fig.width=4, fig.height=10, fig.cap = "this is testing"}
![](C:/testing.gif)
```

如果您在 rmd 中制作 gif,则不会出现此问题,但这不是我想要的:

```{r, fig.width=4, fig.height=10, fig.cap = "this is testing"}
library(gganimate)
ggplot(iris, aes(x = Petal.Width, y = Petal.Length)) + 
  geom_point() +
  transition_states(Species,
                    transition_length = 2,
                    state_length = 1)

```

有什么建议吗?谢谢

最佳答案

以下代码块对我有用:

```{r testing, fig.cap="example caption"}
knitr::include_graphics("testing.gif")
```

enter image description here

关于r - 将预制的 gganimate gif 加载到 rmarkdown block 中作为标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60420075/

相关文章:

r - 使用 gganimate() 沿日期对点和回归线进行动画处理

yaml - 编织成 PDF 时如何让 amsmath 在 RMarkdown 中工作?

html - RMarkdown 与 knitr 到 HTML : How to hide bullets in TOC (table of contents)?

在 R 中将数据文件渲染为格式化的 Word 文档

r - 如何生成堆积条形图?

r - 使用 ggplot 只为许多箱线图上色

regex - 在 R 中提取 URL 参数和值

R Plyr - 从 DDPLY 订购结果?

r - 更改ggplot中的线宽,而不是大小

r - 在 gganimate 中没有获得所需的标题 - 带有表情的动画标签