r - 使用 rmarkdown 渲染时更改 R ggplot gganimate 的速度

标签 r ggplot2 r-markdown gganimate

我有一些通过 gganimate 进行动画处理的 ggplot 对象,我想更改动画的速度。

这是来自 github 存储库的可重现示例:https://github.com/dgrtwo/gganimate

library(gapminder)
library(ggplot2)
theme_set(theme_bw())

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

library(gganimate)
gganimate(p)

该动画从头到尾循环大约需要 12 秒。我怎样才能让它在 6 秒内循环?

我尝试将时间设置为不同的值,但无济于事,并且从帮助页面上看不清楚。

gganimate(p, time = 0.1)

更新

interval 似乎通常可以工作,但我仍然不清楚如何在 Rmarkdown 报告中使其工作。举个例子,如果我将下面的代码放在名为 test.R 的文件中,然后运行 ​​rmarkdown::render('test.R') 我的动画运行在默认速度而不是预期的增加速度。我如何在 rmarkdown::render 的上下文中完成这项工作?我一直在尝试各种方法:https://github.com/dgrtwo/gganimate/commit/3aa2064cdfff30feb2b00724ad757fd5a5a62621 ,但无济于事。

knitr::opts_chunk$set(message = FALSE, warning = FALSE, fig.show = 'animate')

library(gapminder)
library(ggplot2)
theme_set(theme_bw())

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

library(gganimate)
capture.output(gganimate(p, interval = 0.2))

最佳答案

我们需要设置interval选项:

gganimate(p, interval = 0.2)

来自animation package手册,请参阅ani.options:

interval a positive number to set the time interval of the animation (unit in seconds); default to be 1.

关于r - 使用 rmarkdown 渲染时更改 R ggplot gganimate 的速度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49023515/

相关文章:

r - 如何使用 R 中的 if 条件语句将矩阵列表的所有行设置为零

r - 使用移动窗口修正数据框

r - ggplot2;当颜色由变量编码时的单回归线?

html - 在R markdown html文档的右上角插入一个标志

r - 如何在 R 中获取数据框/矩阵的整数行名称?

r - 对由两个变量分组的变量进行计数

r - ggplot 图层首先放置时会破坏顺序

r - ggplot2:数字 x 轴上的自定义分类标签

html - Rmarkdown : embed html file to ioslides

r - 注释掉 Rmd 文件的一些 block /部分