R: "InvalidArgument `-delay' with animation and ggplot
R: "InvalidArgument `-delay' with animation and ggplot
我正在尝试根据大型数据集(来自循环科学实验)在 R 中制作动画图,以可视化两个变量随时间的变化。我只是简单地使用 animation
库:
saveGIF(
for(i in 1:100){
mygraph(i)
}, interval = 0.1, ani.width = 640, ani.height = 480)
其中 mygraph(i)
只是绘制周期 i 的图形。如果我使用 plot()
来制作图表,那么它工作得很好,但是如果我改为使用 ggplot(我想这样做,因为我最终想用它来制作更复杂的图),那么它就不会无法正常工作,我得到以下输出:
Executing:
'convert' -loop 0 -delay 'animation.gif'
convert: InvalidArgument `-delay': animation.gif @ error/convert.c/ConvertImageCommand/1161.
an error occurred in the conversion... see Notes in ?im.convert
[1] FALSE
我是 R 的新手,所以有点卡住了,我还没有通过查看 ?im.convert
或四处搜索找到解决方案。任何建议将不胜感激...
根据要求使用虚拟数据的示例:
library(animation)
library(ggplot2)
x <- 1:20
y <- 21:40
z <- c(1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,4,4,4,4,4)
data <- data.frame(x,y,z)
mygraph <- function(i) {
plot(data$x[data$z == i],
data$y[data$z == i],
title(title))
}
saveGIF(
for(i in 1:4){
title <- paste("Cycle", i, sep=" ")
mygraph(i)
}, interval = 0.5, ani.width = 640, ani.height = 480)
这有效,但如果函数 mygraph
改为:
mygraph <- function(i) {
ggplot() +
geom_point(aes(x=data$x[data$z == i], y=data$x[data$z == i]))
}
...然后它给了我如上所述的错误。
如果将 ggplot
包装在 print()
语句中,这似乎有效,例如
mygraph <- function(i) {
g <- ggplot() +
geom_point(aes(x=data$x[data$z == i], y=data$x[data$z == i]))
print(g)
}
这是 R-FAQ 7.22 的变体,Why do lattice/trellis graphics not work?
我正在尝试根据大型数据集(来自循环科学实验)在 R 中制作动画图,以可视化两个变量随时间的变化。我只是简单地使用 animation
库:
saveGIF(
for(i in 1:100){
mygraph(i)
}, interval = 0.1, ani.width = 640, ani.height = 480)
其中 mygraph(i)
只是绘制周期 i 的图形。如果我使用 plot()
来制作图表,那么它工作得很好,但是如果我改为使用 ggplot(我想这样做,因为我最终想用它来制作更复杂的图),那么它就不会无法正常工作,我得到以下输出:
Executing:
'convert' -loop 0 -delay 'animation.gif'
convert: InvalidArgument `-delay': animation.gif @ error/convert.c/ConvertImageCommand/1161.
an error occurred in the conversion... see Notes in ?im.convert
[1] FALSE
我是 R 的新手,所以有点卡住了,我还没有通过查看 ?im.convert
或四处搜索找到解决方案。任何建议将不胜感激...
根据要求使用虚拟数据的示例:
library(animation)
library(ggplot2)
x <- 1:20
y <- 21:40
z <- c(1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,4,4,4,4,4)
data <- data.frame(x,y,z)
mygraph <- function(i) {
plot(data$x[data$z == i],
data$y[data$z == i],
title(title))
}
saveGIF(
for(i in 1:4){
title <- paste("Cycle", i, sep=" ")
mygraph(i)
}, interval = 0.5, ani.width = 640, ani.height = 480)
这有效,但如果函数 mygraph
改为:
mygraph <- function(i) {
ggplot() +
geom_point(aes(x=data$x[data$z == i], y=data$x[data$z == i]))
}
...然后它给了我如上所述的错误。
如果将 ggplot
包装在 print()
语句中,这似乎有效,例如
mygraph <- function(i) {
g <- ggplot() +
geom_point(aes(x=data$x[data$z == i], y=data$x[data$z == i]))
print(g)
}
这是 R-FAQ 7.22 的变体,Why do lattice/trellis graphics not work?