1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 手把手:如何用R制作动态图

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

时间:2023-01-26 13:01:25

相关推荐

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

如何用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)

根据年份的不同,用不同颜色填充的世界独立进程动态图

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)

生成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)

},img.name = "map1", imgdir ="./img", htmlfile = "map.html",

autobrowse =FALSE, title = "Demo of Country Independents")

生成GIF图

library(maps)

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)

},movie.name="demo.gif",img.name="map1")

粗略的就介绍到这里了。还可以生成flash等其他的动画,不一一列出了。祝大家工作、生活愉快!

原文发布时间为:-11-02

本文来自云栖社区合作伙伴“大数据文摘”,了解相关信息可以关注“BigDataDigest”微信公众号

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。