问题描述
目标
使用开始/停止/暂停按钮控制动画。
问题
animation
包可以使用saveHTML()
函数(example here)将创建的动画另存为HTML页面。但这要求“ expr
=要计算以创建图像序列的R表达式”作为第一个参数。
但是,gganimate
创建了一个gganim
对象,我似乎找不到能够将动画另存为带有按钮的HTML页面的函数。可用的anim_save()
函数不会另存为HTML。有解决办法吗?
动画示例代码
如果您有任何想法,请分享。供您参考,我将其网站上的gganimate
代码放在下面。
library(ggplot2)
library(gganimate)
ggplot(mtcars,aes(factor(cyl),mpg)) +
geom_Boxplot() +
# Here comes the gganimate code
transition_states(
gear,transition_length = 2,state_length = 1
) +
enter_fade() +
exit_shrink() +
ease_aes('sine-in-out')
解决方法
以下内容在RStudio中对我有用,尽管我没有在gganimate::animate
中看到“当前”作为设备选项列出:
library(gganimate)
library(animation)
# name the above gganimate example as p
p <- ggplot(mtcars,aes(factor(cyl),mpg)) +
geom_boxplot() +
transition_states(
gear,transition_length = 2,state_length = 1
) +
enter_fade() +
exit_shrink() +
ease_aes('sine-in-out')
# pass the animate command to saveHTML
saveHTML(animate(p,nframes = 10,# fewer frames for simplification
device = "current"),img.name = "gganimate_plot",htmlfile = "gg.html")
我有一个带有动画控制按钮的html文件和一个包含10个png文件的图像文件夹。