手把手:如何用R制作动态图

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

⼿把⼿:如何⽤R制作动态图
⼿把⼿:如何⽤R制作动态图
原创2015-11-02⼤数据⽂摘⼤数据⽂摘
⼤数据⽂摘作品,欢迎个⼈转发朋友圈,⾃媒体、媒体、机构转载务必申请授权,后台留⾔“机构名称+转载”,申请过授权的不必再次申请,只要按约定转载即可。

作者|顾运筠
如何⽤R制作动态的统计图呢?下⾯我们以“⼤数据⽂摘”国庆献礼的世界独⽴进程为例,⼿把⼿地教⼤家如何⽤R制作动态图。

安装制作GIF所需要的ImageMagic程序
从/script/binary-releases.php⽹址下载相关的ImageMagic.exe。


装ImageMagic.exe。

安装完成后启动Rstudio编写代码。

载⼊制作动画和地图的R程序包
library(animation)
library(maps)
输⼊相关数据
由于国家很多,这⾥只选择⼏个国家,⾜够说明问题就可以了。

mtitle <-c("660","1668","1776","1788","1840","1867","1880","1948","1949","1950","1990")
regList<-list(c("Japan"),c("UK"), c("USA"), c("Australia"),c("NewZealand"), c("Canada"),
c("France"),c("Korea","South Korea"),
c("China","Hungary"),c("Kuwait","India"),c("Russia","Germany","Yemen"))
对区域的列表采⽤了list形式,同⼀年成⽴的国家包含在⼀个regList[[k]]元素中,这样成⽴年份就和区域列表的元素⼀⼀对应了。

绘图函数绘制地图
采⽤maps包中的map函数绘制成⽴区域。

代码如下:
#绘制空⽩世界地图
map("world")
#添加独⽴区域,着⾊
map("world", region=reg,exact=FALSE, add=TRUE, fill=TRUE, col="pink")
制作动态图
同⼀种颜⾊填充的世界独⽴进程动态图
#设定动画的时间间隔和帧数
oopt = ani.options(interval = 0.2, nmax =11)
#⽣成动画
for (i in 1:ani.options("nmax")){
map("world")
reg=""
reg=regList[[1]]
if (i>1) {
for (k in 2:i) {reg =c(reg,regList[[k]])}
}
map("world", region=reg, exact=FALSE, add=TRUE, fill=TRUE,col="pink")
title(mtitle[i])
#等待interval设置的时间长度
ani.pause()
}
#重载动画options
ani.options(oopt)
1. 根据年份的不同,⽤不同颜⾊填充的世界独⽴进程动态图
oopt = ani.options(interval = 0.2, nmax =11)
amax=ani.options("nmax")
for (i in 1:amax) {
map("world",region=regList[[k]], exact=FALSE, add=TRUE, fill=TRUE,col=rgb(k+15,16,16,max=3*amax)) }
title(mtitle[i])
ani.pause()
}
ani.options(oopt)
⽣成HTML⽂件或GIF图
⽣成HTML⽂件
library(maps)
library(animation)
#设定⽹页⽂件所在⽬录
setwd("…./webpage")
mtitle <-c("660","1668","1776","1788","1840","1867","1880","1948","1949","1950","1990")
regList <- list(c("Japan"),c("UK"), c("USA"), c("Australia"),c("NewZealand"), c("Canada"), c("France"),
c("Korea","SouthKorea"),c("China","Hungary"),c("Kuwait","India"),c("Russia","Germany","Yemen"))
saveHTML(
{
oopt = ani.options(interval = 0.2, nmax =11)
amax=ani.options("nmax")
for (i in 1:amax) {
map("world")
for (k in 1:i) {
map("world",region=regList[[k]], exact=FALSE, add=TRUE, fill=TRUE,col=rgb(k+15,16,16,max=3*amax)) }
title(mtitle[i])
ani.pause()
}
ani.options(oopt)
}, = "map1", imgdir ="./img", htmlfile = "map.html",
autobrowse =FALSE, title = "Demo of Country Independents")
library(animation)
#设定GIF 图⽚所在⽬录
setwd("…./")
mtitle <-c("660","1668","1776","1788","1840","1867","1880","1948","1949","1950","1990")
regList <- list(c("Japan"),c("UK"), c("USA"), c("Australia"),c("NewZealand"), c("Canada"),
c("France"),c("Korea","SouthKorea"),c("China","Hungary"),c("Kuwait","India"),c("Russia","Germany","Yemen"))
saveGIF(
{
oopt = ani.options(interval = 0.2, nmax =11)
amax=ani.options("nmax")
for (i in 1:amax) {
map("world")
for (k in 1:i) {
map("world",region=regList[[k]], exact=FALSE, add=TRUE, fill=TRUE,col=rgb(k+15,16,16,max=3*amax))
}
title(mtitle[i])
ani.pause()
}
ani.options(oopt)
},="demo.gif",="map1")
粗略的就介绍到这⾥了。

还可以⽣成flash等其他的动画,不⼀⼀列出了。

祝⼤家⼯作、⽣活愉快!。

相关文档
最新文档