【发布时间】:2022-01-15 01:26:59
【问题描述】:
我在受限研究环境(经过若干修改的 Windows 10 VM)中使用带有 {gganimate} 软件包的 RStudio 来创建 .gif 和 .mp4 格式的动画图 - 当它们在 RStudio 中呈现时,它们会显示很好,但是如果我使用 anim_save() 保存文件,我无法在提供的 Windows 图像查看器或 Web 浏览器(或.mp4 的情况下的媒体播放器)中打开生成的文件 - 它总是说文件已损坏或无法显示。
将这些文件从受限环境中导出需要一个漫长的过程,所以我想检查一下它们是否真的损坏了,或者由于某种原因无法在这个特定的操作系统中显示。 RStudio 可以打开/显示.gif 文件或视频吗?注意:我知道如何使用print()/plot() 方法显示动画 - 这是关于在导出后打开/显示外部动画文件。
生成动画情节并保存为.gif/.mp4的示例代码如下:
library(ggplot2)
library(gganimate) # package {av} also required to save as mp4
animated_plot <-
ggplot(mtcars, aes(x = wt, y = hp, colour = as.factor(cyl))) +
geom_point() +
transition_states(cyl, transition_length = 3, state_length = 1) +
enter_fade() +
exit_fade() +
labs(title = "Cyl: {closest_state}")
## save as gif
anim_save(
filename = "animation.gif",
animation = animate(animated_plot)
)
## save as mp4
anim_save(
filename = "animation.mp4",
animation = animate(animated_plot,
renderer = av_renderer())
)
(我的备用计划是使用file_renderer() 将单个帧导出为图像并稍后对其进行动画处理,例如Convert multiple png to gif as an animation in R)
【问题讨论】:
-
你能添加一个代码,生成一个简单的动画图(gif和mp4)吗?我想尝试一下。
-
@manro,我添加了一些示例代码!
-
谢谢,我现在正在努力;)
-
它有效,是的。我稍微更正了你的代码。我添加一个答案,请稍等。
标签: r rstudio gif mp4 gganimate