R没有原生的GIF图形驱动程序,大部分(完全?)由于GIF格式的专利保留:见
http://tolstoy.newcastle.edu.au/R/help/05/02/12809.html.
caTools包中有一个函数(write.gif()),但它专门用于编写图像.如果你想使用它,你必须先做一些hacky将你的绘图转换为图像(例如保存为PNG,然后将其作为图像读回R).例如:
png("myPlot.png")
plot(rnorm(1000),rnorm(1000))
dev.off()
library(png)
P1
library(caTools)
write.gif(P1,"myPlot.gif")
showGIF
showGIF("myPlot.gif")
unlink("myPlot.gif") ## clean up
?write.gif()有很多关于颜色索引的东西,我没有阅读,但这对于更复杂的图形可能很重要……
动画包有一个saveGIF()函数来保存GIF,但(1)它用于保存多帧动画(不是通用图形),(2)它通过调用ImageMagick来实现.
只是自己构建这个功能更容易.
>安装ImageMagick(http://imagemagick.org)
>另存为PNG,然后使用ImageMagick进行转换.
例如:
png("myPlot.png")
plot(rnorm(1000),rnorm(1000))
dev.off()
system("convert myPlot.png myPlot.gif")
unlink("myPlot.png") ## clean up
showGIF("myPlot.gif")
unlink("myPlot.gif") ## clean up
当然,如果你想定期使用它们,你可以在函数中使用其中任何一个.
更新:我花了一些时间来尝试获得纯R解决方案,但还没有一个有效的解决方案.建议或编辑欢迎……
## needs ImageMagick: just for testing ...
showGIF
主要功能:
saveGIF
require(png)
require(caTools)
tmpfn
on.exit(unlink(tmpfn))
savePlot(tmpfn,type="png")
P1
dd
P1
dim(P1)
P1
if (verbose) cat("finding unique colours ...\n")
P1u
rgbMat
rgb(x[,1],x[,2],3])
}
if (verbose) cat("creating colour index ...\n")
pp
## make sure factor is correctly ordered
ind
if (verbose) cat("finding colour palette ...\n")
if (nrow(P1u)>256) {
if (verbose) cat("kmeans clustering ...\n")
kk
ind
pal
} else {
pal
}
## test:
if (debug) {
dev.new()
par(mar=rep(0,4))
image(t(matrix(ind-1,nrow=dd[1])),col=pal,axes=FALSE,ann=FALSE)
}
if (verbose) cat("writing GIF ...\n")
indmat
storage.mode(indmat)
write.gif(indmat,fn,col=as.list(pal),scale="never")
}
X11.options(antialias="none")
image(matrix(1:64,nrow=8),col=rainbow(10))
saveGIF("tmp.gif",verbose=TRUE,debug=TRUE)
showGIF("tmp.gif")