如何附加一个shp文件来修改地图边界?

时间:2013-04-30 15:01:03

标签: r maps ggplot2 shapefile

感谢@AriBFriedman和@PaulHiemstra的建议,并随后想出如何合并.shp文件,我设法使用以下代码和数据生成以下地图:

数据:

MoroccoWestern Sahara

代码:

    # Loading administrative coordinates for Morocco maps
    library(sp)
    library(maptools)
    library(mapdata)

    # Loading shape files
    Mor <- readShapeSpatial("F:/Purdue University/RA_Position/PhD_ResearchandDissert/PhD_Draft/Country-CGE/MAR_adm1.shp")
    Sah <- readShapeSpatial("F:/Purdue University/RA_Position/PhD_ResearchandDissert/PhD_Draft/Country-CGE/ESH_adm1.shp")

    # Ploting the maps individually
    png("Morocco.png")
    Morocco <- readShapePoly("F:/Purdue University/RA_Position/PhD_ResearchandDissert/PhD_Draft/Country-CGE/MAR_adm1.shp")
    plot(Morocco)
    dev.off()

    png("WesternSahara.png")
    WesternSahara <- readShapePoly("F:/Purdue University/RA_Position/PhD_ResearchandDissert/PhD_Draft/Country-CGE/ESH_adm1.shp")
    plot(WesternSahara)
    dev.off()

    # Merged map of Morocco and Western Sahara

    MoroccoData <- rbind(Mor@data,Sah@data) # First, 'stack' the attribute list rows using rbind() 
    MoroccoPolys <- c(Mor@polygons,Sah@polygons) # Next, combine the two polygon lists into a single list using c()

    # summary(MoroccoData)
    # summary(MoroccoPolys)

    offset <- length(MoroccoPolys) # Next, generate a new polygon ID for the new SpatialPolygonDataFrame object

    browser()
    for (i in 1: offset)
    {
    sNew =  as.character(i)
    MoroccoPolys[[i]]@ID = sNew
    }

    ID <- c(as.character(1:length(MoroccoPolys))) # Create an identical ID field and append it to the merged Data component
    MoroccoDataWithID <- cbind(ID,MoroccoData)

    MoroccoPolysSP <- SpatialPolygons(MoroccoPolys,proj4string=CRS(proj4string(Sah))) #  Promote the merged list to a SpatialPolygons data object

    Morocco <- SpatialPolygonsDataFrame(MoroccoPolysSP,data = MoroccoDataWithID,match.ID = FALSE) #  Combine the merged Data and Polygon components into a new SpatialPolygonsDataFrame.

    Morocco@data$id <- rownames(Morocco@data)
    Morocco.fort <- fortify(Morocco, region='id') 
    Morocco.fort <- Morocco.fort[order(Morocco.fort$order), ] 

    MoroccoMap <- ggplot(data=Morocco.fort, aes(long, lat, group=group)) + 
    geom_polygon(colour='black',fill='white') + 
    theme_bw()

结果:

个人地图

Morocco Western Sahara

合并地图:

Morocco-Merged

问题:

我想消除切入地图中间的边界?有任何建议和/或帮助的人吗?

全部

1 个答案:

答案 0 :(得分:0)

我确实消除了使用QGIS软件合并后将地图切成两半的额外多边形图层,可以在此链接免费下载QGIS