mjl*_*cey 5 animation r ggplot2
我试图从大型数据集(来自循环科学实验)在R中制作动画图,以便可视化两个变量随时间的变化.我只是使用animation图书馆:
saveGIF(
for(i in 1:100){
mygraph(i)
}, interval = 0.1, ani.width = 640, ani.height = 480)
Run Code Online (Sandbox Code Playgroud)
其中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
Run Code Online (Sandbox Code Playgroud)
我对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)
Run Code Online (Sandbox Code Playgroud)
这有效,但如果功能mygraph是:
mygraph <- function(i) {
ggplot() +
geom_point(aes(x=data$x[data$z == i], y=data$x[data$z == i]))
}
Run Code Online (Sandbox Code Playgroud)
...然后它给了我如上所述的错误.
如果你包装ggplot一个print()语句,这似乎是有效的,例如
mygraph <- function(i) {
g <- ggplot() +
geom_point(aes(x=data$x[data$z == i], y=data$x[data$z == i]))
print(g)
}
Run Code Online (Sandbox Code Playgroud)
这是R-FAQ 7.22的变体,为什么格子/格子图形不起作用?