每个人,都为造成的困扰感到抱歉,但是我刚接触R时遇到了一个非常困难的问题:我想创建一张动画的Russin地图,例如失业率随不同年份的变化。在图像上,您可以看到一年的数据
require(sp)
require(maptools)
require(RColorBrewer)
require(rgdal)
rus<-url("http://www.filefactory.com/file/4h1hb5c1cw7r/n/RUS_adm1_RData")
print(load(rus))
unempl <- read.delim2(file="C:\\unempl1.txt", header = TRUE,
sep = ";",quote = "", dec=",", stringsAsFactors=F)
gadm_names <-gadm$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], unempl$region,
max.distance = 0.2)[1]
setTxtProgressBar(pb, i) # update progress bar
}
col_no <- as.factor(as.numeric(cut(unempl$data[order],
c(0,2.5,5,7.5,10,15,100))))
levels(col_no) <- c("<2,5%", "2,5-5%", "5-7,5%",
"7,5-10%", "10-15%", ">15%")
gadm$col_no <- col_no
myPalette<-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")
我愿意得到的结果类似于此处的动画:http : //spatial.ly/2011/02/mapping-londons-population-change-2011-2030/ 但是,我在Google上搜索了很多,阅读了许多主题在http://stackoverflow.com中,包括以下内容:从R中的一系列绘图创建影片,但仍无法正确执行操作。
先感谢您!
我想出了类似的方法,有人可以告诉我错误在哪里:
require(animation)
require(sp)
require(RColorBrewer)
require(classInt)
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$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))))
saveHTML(
for(k in 1:2) {
try<-get(paste("col_no_", k, sep = ""))
levels(try) <- c("<2,5%", "2,5-5%", "5-7,5%",
"7,5-10%", "10-15%", ">15%")
gadm$col_no <- try
myPalette<-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")
},img.name = "map", htmlfile = "unrus2.html")