Plot spatial area defined by multiple polygons

不打扰是莪最后的温柔 提交于 2019-12-01 01:22:33
jlhoward

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))
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!