本文介绍了R中的动画gadm地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 每个人都很抱歉打扰,但是我正在面临着一个非常重要的难题,我正在面临新的挑战:我想用不同年份的失业创造一个动画地图。首先,我在这里阅读了许多主题,其中包括 新代码,我尝试使用以下建议 库(sp)库(rgdal)库(空时)库(动画) rus< ; - url(http://www.filefactory.com/file/4h1hb5c1cw7r/n/RUS_adm1_RData) load(rus) proj4.str gadm.prj< - spTransform(gadm,proj4.str) N< - nrow(gadm.prj) pols< - 几何.prj) nms< -gadm $ NAME_1 vals1< - read.csv2(C:\\unempl11.txt) ord1< - match(nms,vals1 $ region vals1 vals2 vals2 nDays< - 2 tt< - seq(as.Date '2011-01-01'),by ='year',le ngth = nDays) vals< - data.frame(rbind(vals1,vals2)) gadmST< - STFDF(pols,time = tt,data = vals) stplot(gadmST,animate = 1,do.repeat = FALSE) 新更正的数据 解决方案 spacetime 包定义了 stplot 方法与几个图形替代。使用 animate 参数来构建动画。首先你必须定义一个 STFDF 对象(阅读包文档和 这篇文章) 首先导入你的 SpatialPolygonsDataFrame ...: 库(sp)库(rgdal)库(spacetime) rus< - url(http://www.filefactory.com/file/4h1hb5c1cw7r/n/RUS_adm1_RData) load(rus) proj4.str< ; - CRS(+ init = epsg:3413 + lon_0 = 105) gadm.prj< - spTransform(gadm,proj4.str) N< - nrow(gadm.prj) pols< - 几何(gadm.prj) ...然后添加您的数据(两天)。您必须使用 SpatialPolygon 的代码重新排序 data.frame 。 vals1 ord1< - match(nms,vals1 $ region) vals1 vals2< - read.csv2('/ tmp / unempldata / unempl12.txt') ord2< - match(nms,vals2 $ region) vals2< - vals2 [ord2,] 不幸的是,您的数据的区域名称与与区域名称不匹配。因此,以前的代码将提供 data.frame ,其行数小于多边形,下一个代码将失败。在使用此代码之前,您可能需要清理数据(请阅读 STFDF 帮助页面了解如何定义 gadmST ): nDays< - 2 tt< - seq(as.Date -01-01'),by ='day',length = nDays) vals< - data.frame(unempl = rbind(vals1,vals2)[, - 1])$ ​​b $ b gadmST< - STFDF(pols,time = tt,data = vals) 现在你准备好了为动画。阅读 stplot 帮助页面以使用其参数改进图形输出: png('gadm%02d.png') stplot(gadmST,animate = 1,do.repeat = FALSE) dev.off() png 文件是可以生产的电影的帧与 ffmpeg 。 system('ffmpeg -r 1 -i gadm%02d.png gadm.mp4') everybody, sorry for disturb, but I am being quite new with r faced a crucial difficuty: I want to create an animated map of Russin with changes in unemployment with differnt years, like. To start with I read a number of themes here, including Creating a Movie from a Series of Plots in R, although I am stil couldn't do it rightly. What I want to have as a result is animated map like here , but with unemployment, like I have made for one year!Here is the code :require(sp)require(maptools)require(RColorBrewer)require(rgdal) rus<-url("http://www.filefactory.com/file/4h1hb5c1cw7r/n/RUS_adm1_RData")print(load(rus)) unempl1 <- read.delim2(file="C:\\unempl11.txt", header = TRUE, sep = ";",quote = "", dec=",", stringsAsFactors=F)unempl2<- read.delim2(file="C:\\unempl12.txt", header = TRUE, sep = ";",quote = "", dec=",", stringsAsFactors=F)gadm_names <-gadm.prj$NAME_1total <- length(gadm_names)pb <- txtProgressBar(min = 0, max = total, style = 3) order <- vector()for (i in 1:total){ order[i] <- agrep(gadm_names[i], unempl1$region, max.distance = 0.2)[1] setTxtProgressBar(pb, i) # update progress bar}for (l in 1:total){ order[l] <- agrep(gadm_names[l], unempl2$region, max.distance = 0.2)[1] setTxtProgressBar(pb, i) # update progress bar}col_no_1 <- as.factor(as.numeric(cut(unempl1$data[order], c(0,2.5,5,7.5,10,15,100))))col_no_2<- as.factor(as.numeric(cut(unempl2$data[order], c(0,2.5,5,7.5,10,15,100))))levels(col_no_1) <- c("<2,5%", "2,5-5%", "5-7,5%", "7,5-10%", "10-15%", ">15%")gadm.prj$col_no_1 <- col_no_1myPalette1<-brewer.pal(6,"Purples")levels(col_no_2) <- c("<2,5%", "2,5-5%", "5-7,5%", "7,5-10%", "10-15%", ">15%")gadm.prj$col_no_2 <- col_no_2myPalette2<-brewer.pal(6,"Purples")proj4.str <- CRS("+init=epsg:3413 +lon_0=105")gadm.prj <- spTransform(gadm, proj4.str)spplot(gadm.prj, "col_no", col=grey(.9), col.regions=myPalette,main="Unemployment in Russia by region")Sorry for being such not understanding, but I really need a help.Thanks in advance!Here is data to be able to reproduce the codeNew code, which I tried using following advicelibrary(sp)library(rgdal)library(spacetime)library(animation)rus <- url("http://www.filefactory.com/file/4h1hb5c1cw7r/n/RUS_adm1_RData")load(rus)proj4.str <- CRS("+init=epsg:3413 +lon_0=105")gadm.prj <- spTransform(gadm, proj4.str)N <- nrow(gadm.prj)pols <- geometry(gadm.prj)nms<-gadm$NAME_1vals1 <- read.csv2("C:\\unempl11.txt")ord1 <- match(nms, vals1$region)vals1 <- vals1[ord1,]vals2 <- read.csv2("C:\\unempl12.txt")ord2 <- match(nms, vals2$region)vals2 <- vals2[ord2,]nDays <- 2tt <- seq(as.Date('2011-01-01'), by='year', length=nDays)vals <- data.frame(rbind(vals1, vals2))gadmST <- STFDF(pols, time=tt, data=vals)stplot(gadmST, animate=1, do.repeat=FALSE)New corrected data 解决方案 The spacetime package defines the stplot method with severalgraphical alternatives. Use its animate argument to build ananimation. First you have to define a STFDF object (read thepackage documentation andthis paper for details)First import your SpatialPolygonsDataFrame...:library(sp)library(rgdal)library(spacetime)rus <- url("http://www.filefactory.com/file/4h1hb5c1cw7r/n/RUS_adm1_RData")load(rus)proj4.str <- CRS("+init=epsg:3413 +lon_0=105")gadm.prj <- spTransform(gadm, proj4.str)N <- nrow(gadm.prj)pols <- geometry(gadm.prj)... and then add your data (two days). You have to reorder thedata.frame with the codes of the SpatialPolygon. vals1 <- read.csv2('/tmp/unempldata/unempl11.txt')ord1 <- match(nms, vals1$region)vals1 <- vals1[ord1,]vals2 <- read.csv2('/tmp/unempldata/unempl12.txt')ord2 <- match(nms, vals2$region)vals2 <- vals2[ord2,]Unfortunately, the region names of your data does not match exactlywith the region names of the polygons. Therefore, the previous codewill provide data.frame with less rows than polygons, and the nextcode will fail. You may want to clean your data before using this code(read the STFDF help page to understand how to define gadmST):nDays <- 2tt <- seq(as.Date('2013-01-01'), by='day', length=nDays)vals <- data.frame(unempl=rbind(vals1, vals2)[,-1])gadmST <- STFDF(pols, time=tt, data=vals)Now you are ready for the animation. Read the stplot help pageto improve the graphical output using its arguments:png('gadm%02d.png')stplot(gadmST, animate=1, do.repeat=FALSE)dev.off()The png files are the frames of a movie that can be producedwith ffmpeg.system('ffmpeg -r 1 -i gadm%02d.png gadm.mp4') 这篇关于R中的动画gadm地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-12 18:56