1

我想像本教程中那样绘制一种 isarhytmoc 点,但要使用人口数据。然而,不幸的是,我没有足够的技能,只有这样的代码:

require(sp)
require(rgdal)
require(RColorBrewer)
require(ggplot2)
library(plyr)
library(maptools)
library(rgeos)
require(gpclib)
gpclibPermitStatus() 
gpclibPermit()


rus<-url("http://www.filefactory.com/file/4h1hb5c1cw7r/n/RUS_adm1_RData")
print(load(rus))
proj4.str <- CRS("+init=epsg:3413 +lon_0=105")
gadm.prj <- spTransform(gadm, proj4.str)


popul <- read.csv2(file="C:\\unempl11.txt", header = TRUE, 
        sep = ";",quote = "", dec=",", stringsAsFactors=F)

df <- fortify(gadm.prj, region = "ID_1")
df2 <- merge(df, gadm.prj, by.x="id", by.y="ID_1")






p <- ggplot(df2, aes(x = long, y = lat, group=group)) +
       geom_point(data    = popul,  
                  mapping = aes(x=lon, y=lat, colour=abs),
                  size    = 3,
                  alpha   = 0.8) +
       scale_colour_gradient2(name  = "Population",
                              low   = "darkred",
                              mid   = "white",
                              high  = "blue",
                              guide = "colorbar") +
       ggtitle("Population in Russia)")
p + geom_path(data    = df2,
               mapping = aes(x=long, y=lat, group=group),
              size    = 0.125)

这是我使用的数据,如果有人可以帮助我提出一些想法,我将不胜感激。

4

1 回答 1

1

您正在尝试显示点,但您正在使用 SpatialPolygonsDataFrame源。您可以提取质心的坐标coordinates并将它们用作数据的坐标,但我不确定这是否是您需要的:

library(sp)
library(rgdal)

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)
## IDs needed to match polygons and data
nms <- gadm.prj$NAME_1

ll <- coordinates(gadm.prj)

另一方面,我无法正确检索data您文件的列。我用一些噪音填充它:

popul <- read.csv2('/tmp/popul.txt')
popul$data <- runif(nrow(popul))

现在是时候匹配坐标和数据了(类似于我们在 上一个问题中所做的):

ord1 <- match(nms, popul$region)
popul <- popul[ord1,]
row.names(popul) <- nms
row.names(ll) <- nms

popSP <- SpatialPointsDataFrame(ll, popul["data"], proj4string=proj4.str)

SpatialPointsDataFrame可以使用 spplot下面的边界直接显示sp.layout

spplot(popSP, sp.layout=list('sp.polygons', gadm.prj))

结果

于 2013-06-24T17:20:12.910 回答