Plot spatial area defined by multiple polygons

前端 未结 1 1336
無奈伤痛
無奈伤痛 2021-01-07 03:51

I have a SpatialPolygonsDataFrame with 11589 spatial objects of class \"polygons\". 10699 of those objects consists of exactly 1 polygon. However, the rest of those spatial

相关标签:
1条回答
  • 2021-01-07 04:37

    I may be misinterpreting your question, but it's possible that you are making this much harder than necessary.

    (Note: I had trouble dealing with the .zip file in R, so I just downloaded and unzipped it in the OS).

    library(rgdal)
    library(ggplot2)
    
    setwd("< directory with shapefiles >")
    map <- readOGR(dsn=".", layer="vg250_gem", p4s="+init=epsg:25832")
    map <- spTransform(map, CRS("+proj=longlat +datum=WGS84"))
    
    nPolys <- sapply(map@polygons, function(x)length(x@Polygons))
    region <- map[which(nPolys==max(nPolys)),]
    plot(region, col="lightgreen")
    

    # using ggplot...
    region.df <- fortify(region)
    ggplot(region.df, aes(x=long,y=lat,group=group))+
      geom_polygon(fill="lightgreen")+
      geom_path(colour="grey50")+
      coord_fixed()
    

    Note that ggplot does not deal with the holes properly: geom_path(...) works fine, but geom_polygon(...) fills the holes. I've had this problem before (see this question), and based on the lack of response it may be a bug in ggplot. Since you are not using geom_polygon(...), this does not affect you...

    In your code above, you would replace the line:

    ggmap(al1) + geom_path(data=as.data.frame(Poly.coords), aes(x=X1, y=X2))
    

    with:

    ggmap(al1) + geom_path(data=region.df, aes(x=long,y=lat,group=group))
    
    0 讨论(0)
提交回复
热议问题