我正在研究R中动画创作的可能性.{Animation}包似乎主要是ffmpeg和imagemagick的R平台.我发现创建单个图像帧的唯一参考是嵌套plot()在循环内.但是,鉴于png()渲染器的速度性能较差,这对于生成更复杂的绘图似乎是一个不可行的缓慢过程,特别是对于包括地图对象的绘图 - 例如:
library(maptools)
data(wrld_simpl)
starttime = Sys.time()
for(i in 1:10){
png(paste('frames/', i, '.png', sep=''))
plot(wrld_simpl, col='grey85', bg = 'white', border='white')
points(sample(-180:180, 50), sample(-90:90, 50), col='red', pch=16, cex=2)
title('poxy map')
dev.off()
}
print(Sys.time() - starttime)
Run Code Online (Sandbox Code Playgroud)
产生10帧并且:
Time difference of 9.763794 secs
Run Code Online (Sandbox Code Playgroud)
我不明白为什么R渲染速度如此之慢 - 按照这个速度,以25fps渲染2分钟视频需要45分钟左右,这对于这个相对简单的地图示例来说似乎很慢.包装apply并不快.有没有人知道plot更有效地包装的方法,或者可能是在渲染了不变的元素之后中途保存了一个情节?
将地图绘制为具有足够分辨率的图像应该比SpatialPolygonsDataFrame的绘图方法更有效.
require(maps) # save the world
png("world.png", width=500, height=200)
map("world", col="grey90", fill=TRUE, border="grey90", mar=c(0,0,0,0))
dev.off()
library(png); library(grid)
img = readPNG("world.png")
animation::saveGIF( {
for( ii in 1:100) {
grid.newpage()
grid.raster(img)
grid.points(default.units="npc")
}
}, ani.height=200, ani.width=500)
Run Code Online (Sandbox Code Playgroud)
