使用保存 GIF 在 R 中创建动画



我试图在包"动画"中使用函数saveGIF。我已经安装了所需的外部软件ImageMagick。试用代码如下:

library(animation)
ani.options(convert = 'C:\Program Files\ImageMagick-7.0.6-Q16\convert.exe')
saveGIF({
for (i in 1:10) plot(runif(10), ylim = 0:1)
})

但它给出了以下错误:

Executing: 
"C:Program FilesImageMagick-7.0.6-Q16convert.exe -loop 0 -delay 100
Rplot1.png Rplot2.png Rplot3.png Rplot4.png Rplot5.png Rplot6.png Rplot7.png
Rplot8.png Rplot9.png Rplot10.png "animation.gif""
'C:Program' is not recognized as an internal or external command,
operable program or batch file.
an error occurred in the conversion... see Notes in ?im.convert
Warning messages:
1: running command 'C:WINDOWSsystem32cmd.exe /c "C:Program FilesImageMagick-7.0.6-Q16convert.exe -loop 0  -delay 100 Rplot1.png Rplot2.png Rplot3.png Rplot4.png Rplot5.png Rplot6.png Rplot7.png Rplot8.png Rplot9.png Rplot10.png "animation.gif""' had status 1 
2: In cmd.fun(convert) :
'"C:Program FilesImageMagick-7.0.6-Q16convert.exe -loop 0  -delay 100 Rplot1.png Rplot2.png Rplot3.png Rplot4.png Rplot5.png Rplot6.png Rplot7.png Rplot8.png Rplot9.png Rplot10.png "animation.gif""' execution failed with error code 1
3: running command '"C:Program FilesImageMagick-7.0.6-Q16convert.exe -loop 0  -delay 100 Rplot1.png Rplot2.png Rplot3.png Rplot4.png Rplot5.png Rplot6.png Rplot7.png Rplot8.png Rplot9.png Rplot10.png "animation.gif""' had status 127 

从 ?im.convert 中的注释中注明

ImageMagick

用户 请从 http://www.imagemagick.org 安装ImageMagick,并确保转换路径.exe 在你的 'PATH' 变量中,在这种情况下,命令转换可以是 在没有完整路径的情况下调用。Windows用户经常非常困惑 关于ImageMagick和"PATH"设置,所以我会尝试搜索 ImageMagick in the Registry Hive by readRegistry('SOFTWARE\ImageMagick\Current'($BinPath,因此你可以 实际上不需要修改您的"PATH"变量。对于以下 安装了 LyX,我也会尝试在 LyX安装目录,所以他们实际上不必安装 ImageMagick,如果LyX存在于他们的系统中(当然,LyX应该 与图像魔术一起安装(。找到转换实用程序后, 将设置动画选项"转换"(ani.options(convert = "路径/到/转换.exe"((;这可以节省搜索转换的时间 下次在操作系统中。

我可以将这些理解为,如果要转换的路径.exe不在我的 PATH 变量中,我需要指定要转换的完整目录.exe,这是错误的吗?

设置或调用转换.exe是否有问题?

saveGIF({
for (i in dflaser10sec1$seg) {
title <- as.character(i)
g1 <- ggplot(dflaser10sec1, aes(x = dflaser10sec1$value, y = dflaser10sec1$pos, fill = dflaser10sec1$pos, width = 1)) +
coord_fixed() +
coord_flip() +
annotate('text', x = 98, y = -800000, 
label = 'Laser PROFILE', size = 3) +
geom_bar(data = subset(dflaser10sec1, pos == "9"), stat = "identity") +
geom_bar(data = subset(dflaser10sec1, pos == "10"),  stat = "identity") +
theme_economist(base_size = 14) + 
scale_fill_manual(values = c('#ff9896', '#d62728')) + 
ggtitle(paste0('profile of the laser setup, ', title)) + 
ylab('Value') + 
xlab('Laser Position') + 
theme(legend.position = "bottom", legend.title = element_blank()) + 
guides(fill = guide_legend(reverse = TRUE))
print(g1)
}
}, movie.name = 'laser.gif', interval = 0.1, ani.width = 700, ani.height = 600)

最新更新