ggplot2通过坐标填充shapefile中的多边形

ggplot2 fill polygons in shapefile by coords

所以我有 shapefile(在页面底部命名为 POWIATY)。我想用我的坐标填充特定的多边形。

比方说,我想填充整个区域: lat:52.599427 lon:20.7572137

我知道如何在地图上设置点坐标,但如何在其中填充整个多边形?

该 shapefile 中有重复的区域名称,因此您必须使用多边形数字 ID 进行填充:

library(rgdal)
library(rgeos)
library(ggplot2)

pow <- readOGR("POWIATY.shp", "POWIATY")
plot(pow)

where <- over(SpatialPoints(cbind(20.7572137, 52.599427)), pow, TRUE)
reg <- data.frame(id=rownames(where[[1]]))

map <- fortify(pow)

gg <- ggplot()
gg <- gg + geom_map(map=map, data=map, 
                    aes(x=long, y=lat, map_id=id),
                    fill="white", color="black", size=0.25)
gg <- gg + geom_map(data=reg, map=map,
                    aes(fill=id, map_id=id), color="steelblue")
gg <- gg + coord_map()
gg <- gg + theme_bw()
gg