大家,很抱歉打扰,但我对 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)