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_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")
Sorry for being such not understanding, but I really need a help. Thanks in advance!
Here is data to be able to reproduce the code
New code, which I tried using following advice
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)