4

大家,很抱歉打扰,但我对 r 很陌生,面临一个关键的困难:我想创建一个 Russin 的动画地图,其中包含不同年份的失业率变化,比如。首先,我在这里阅读了许多主题,包括从 R 中的一系列情节创建电影,尽管我仍然无法正确地做到这一点。结果我想要的是像这里这样的动画地图,但是有失业,就像我做了一年一样!在此处输入图像描述 这是代码:

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_1


total <- 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_1

myPalette1<-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_2

myPalette2<-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")

很抱歉这么不理解,但我真的需要帮助。提前致谢!

这是能够重现代码的数据

新代码,我尝试使用以下建议

library(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_1
vals1  <- 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 <- 2
tt <- 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)

新的更正数据

4

1 回答 1

1

The spacetime package defines the stplot method with several graphical alternatives. Use its animate argument to build an animation. First you have to define a STFDF object (read the package documentation and this 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 the data.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 exactly with the region names of the polygons. Therefore, the previous code will provide data.frame with less rows than polygons, and the next code 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 <- 2
tt <- 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 page to 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 produced with ffmpeg.

system('ffmpeg -r 1 -i gadm%02d.png gadm.mp4')
于 2013-06-18T22:36:53.040 回答