简体   繁体   English

更改/添加空间边界/多边形形状文件R

[英]Changing/adding spatial boundaries/polygon shapefile R

I'm dealing with an Afghanistan shapefile which contains all of the provinces. 我正在处理包含所有省份的阿富汗shapefile。 I ultimately want to plot some data by regional command, which each contain several provinces (this is what I'm talking about: http://en.wikipedia.org/wiki/File:Afghanistan_ISAF_Sept2008.jpg ). 我最终想通过区域命令绘制一些数据,每个区域包含几个省(这就是我正在谈论的内容: http : //en.wikipedia.org/wiki/File : Afghanistan_ISAF_Sept2008.jpg )。 I'm not hugely familiar with handling shapefiles in R, but I haven't found much in the way of tutorials on this issue. 我对在R中处理shapefile并不是很熟悉,但是在有关此问题的教程方面却没有发现太多。 Is it possible to re-draw the polygons or overlay the RC boundaries and plot a choropleth that way? 是否可以重新绘制多边形或覆盖RC边界并以这种方式绘制Choropleth? Thanks. 谢谢。

Something like this? 像这样吗

Code: 码:

library(rgdal)     # for readOGR(...)
library(ggplot2)   # for fortify(...) and rendering the map
setwd("<directory with all your files>")

regional.commands <- read.csv("regional.commands.csv")

map  <- readOGR(dsn=".", layer="afghanistan_province_boundaries_-_34_provinces")
data <- data.frame(id=rownames(map@data),Province=map@data$Prov34Na)
data <- merge(data,regional.commands,by="Province")
map.df <- fortify(map)
map.df <- merge(map.df,data,by="id", all.x=T)
map.df <- map.df[order(map.df$order),]

ggplot(map.df, aes(x=long,y=lat, group=group))+ 
  geom_polygon(aes(fill=RC))+
  geom_path(colour="grey70")+
  coord_fixed()

In this example, regional.commands.csv was assembled (manually!!!!) from data on the ISAF website . 在此示例中, regional.commands.csv是由ISAF网站上的数据组装而成的(手动!!!!)。 This file has just two columns: Province and RC (regional command). 该文件只有两列: ProvinceRC (区域命令)。 Presumably, you have something like this already, but did not feel the need to share it. 大概您已经有了类似的东西,但是并不需要共享它。 The Afghanistan shapefile was obtained here . 阿富汗shapefile是在这里获得的。

Explanation 说明

The shapefile is read into R using readOGR(...) , creating a "SpatialPolygonsDataFrame" object ( map ). 使用readOGR(...)将shapefile读取到R中,创建一个“ SpatialPolygonsDataFrame”对象( map )。 This object has two main sections, a polygons section, which contains the coordinates of the polygon boundaries, and a data section, which contains information about each polygon (such as the province name). 该对象有两个主要部分,一个多边形部分(包含多边形边界的坐标)和一个数据部分,其中包含有关每个多边形的信息(例如省名)。 The latter can be referenced using map@data . 可以使用map@data引用后者。

Each polgyon has a unique id. 每个polgyon都有一个唯一的ID。 These are stored in the row names of map@data . 这些存储在map@data的行名称中。 So first we create a data frame data with column id from the row names of map@data , and a column Province from the relevant column of map@data (column Prov34Na ). 因此,首先,我们从map@data的行名称中创建具有列id的数据框data ,并从map@data的相关列(列Prov34Na )中创建一个Province列。

data <- data.frame(id=rownames(map@data),Province=map@data$Prov34Na)

If you have a different Afghanistan shapefile, the appropriate column in map@data might be different. 如果您使用的阿富汗shapefile不同,则map@data的相应列可能会不同。

Now we merge this with the regional.commands data frame based on the common Province column. 现在,我们将其与基于公共Province列的regional.commands数据框合并。

data <- merge(data,regional.commands,by="Province")

data now has 3 columns: Province , id , and RC (Regional Command). data现在有3列: ProvinceidRC (区域命令)。 One thing to watch out for is that, evidently, ISAF feels it knows best how to spell the names of Afghanistan's provinces. 需要注意的一件事是,显然,国际安全援助部队认为它最清楚如何拼写阿富汗各省的名称。 Some of these do not correspond to the spelling in the map attributes table. 其中一些与地图属性表中的拼写不符 So you may need to fix that, again, manually... 因此,您可能需要再次手动修复此问题...

Now we use fortify(...) to create, from map , a dataframe suitable for plotting ( map.df ). 现在,我们使用fortify(...)map创建适合绘图的数据map.dfmap.df )。 This data frame has an id column, so we merge that with data based on id . 该数据框具有一个id列,因此我们将其与基于id data合并。

map.df <- merge(map.df,data,by="id", all.x=T)

Finally, all this merging messed up the order of the rows, so we re-order using the order column in map.df . 最后,所有这些合并弄乱了行的顺序,因此我们使用map.df的order列重新排序。

map.df <- map.df[order(map.df$order),]

The rest is just generating the layers and rendering the plot. 剩下的只是生成图层并渲染图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM