合并后,spatialpolygonsdataframe多边形混乱加入数据

时间:2015-08-12 13:33:47

标签: r plot merge shapefile maptools

我试图使用R。

绘制一些土耳其数据

我遇到的问题是当我将数据与形状文件合并时(在spatialpolygonsdataframe格式中),数据不再与正确的pologons匹配。我做错了什么,

下面是一些可重现的代码。形状文件是一些before merge(所以公共领域),我把它放在我的谷歌驱动器上,用简单的数据excel文件压缩。 在合并之前和之后,它会绘制2个绘制省名的图。 你可以看到第二张图片混乱了#34;数据和Turkey.map@data不再匹配正确的多边形。

在使用正确的省名进行合并之前: after merge 合并后的情节: slice-notation

library(maptools)
library(readxl)

temp <- "TurkeyShapefile.zip"
URL      <- "https://docs.google.com/ucid=0B0TyKM0aACIONUxfNTJwWGhrR0k&export=download"
download.file(URL,temp, mode="wb")
unzip(temp)

trtr <- readShapeSpatial("Natural_earth_admin_RMS150518_TR")

#read excel file
fname <- "TR_data.xlsx"
TRdata <- read_excel(fname, sheet = "pcnt")

Turkey.map <- trtr       #create copy of trtr

#a plot of the map before the merge
plot(Turkey.map)
invisible(text(getSpPPolygonsLabptSlots(Turkey.map), labels=as.character(Turkey.map@data$Admin1Name), cex=0.5))


#merge (join data)
Turkey.map@data <- merge(Turkey.map@data,TRdata,by.x="Admin1Name",by.y="Province", all.x=TRUE)

#a plot of the map after the merge
plot(Turkey.map)
invisible(text(getSpPPolygonsLabptSlots(Turkey.map), labels=as.character(Turkey.map@data$Admin1Name), cex=0.5))

非常感谢!

1 个答案:

答案 0 :(得分:1)

如果您对空间对象上的@data插槽执行任何操作,以任何方式对其进行重新排序,您就会陷入痛苦的世界。一般情况下,您应该通过在匹配数据集之间的ID字段上手动调用which()来执行所有操作,或者在您的情况下,您可以在merge()对象本身上调用SpatialPolygonsDataFrame

Turkey.map <- merge(
    Turkey.map, TRdata, 
    by.x="Admin1Name", by.y="Province", 
    all.x=TRUE
)

很好奇为什么OP没有看到将Spatial*对象与data.frame合并的正确输出,这是一个完全可重现的示例,显示了正确的行为:

library(sp)

##  Reproducible 10x10 grid of polygons:
set.seed(2002)
grd <- GridTopology(c(1,1), c(1,1), c(10,10))
polys <- as(grd, "SpatialPolygons")
centroids <- coordinates(polys)
x <- centroids[,1]
y <- centroids[,2]
z <- 1.4 + 0.1*x + 0.2*y + 0.002*x*x
d <- SpatialPolygonsDataFrame(
  polys,
  data=data.frame(
    x=x, y=y, z=z, ID=1:length(x), 
    row.names=row.names(polys)
  )
)

df <- data.frame("ID"=1:10, color="black")

class(d)
class(df)

收率:

class(d)
[1] "SpatialPolygonsDataFrame"
attr(,"package")
[1] "sp"
class(df)
[1] "data.frame"

继续合并两者:

##  The merge of a SpatialPolygonsDataFrame and a data.frame:
dm <- merge(d, df, by.x="ID", by.y="ID", all.x=T)

##  Verify we still have a Spatial* object:
class(dm)
names(dm)

收率:

class(dm)
[1] "SpatialPolygonsDataFrame"
attr(,"package")
[1] "sp"
names(dm)
[1] "ID"    "x"     "y"     "z"     "color"

plot(dm, col=dm$color)

enter image description here

> sessionInfo()
R version 3.2.1 (2015-06-18)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 8 x64 (build 9200)

locale:
[1] LC_COLLATE=English_United States.1252 
[2] LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices
[4] utils     datasets  methods  
[7] base     

other attached packages:
[1] sp_1.1-1

loaded via a namespace (and not attached):
[1] tools_3.2.1     grid_3.2.1     
[3] lattice_0.20-31