由于read.shp
在fastshp
包中以列表列表的形式返回多边形数据,因此只需将其减少为绘图所需的单个数据框ggplot2
。
library(fastshp)
library(ggplot2)
setwd(system.file("shapes", package="maptools"))
shp <- read.shp("columbus.shp", format="polygon")
shp.list <- sapply(shp, FUN = function(x) do.call(cbind, x[c("id","x","y")]))
shp.df <- as.data.frame(do.call(rbind, shp.list))
shp.gg <- ggplot(shp.df, aes(x = x, y=y, group = id))+geom_polygon()
编辑:基于@otsaw 关于多边形孔的评论,以下解决方案需要更多步骤,但确保最后绘制孔。它利用 shp.df$hole 是合乎逻辑的,并且带有 hole==TRUE 的多边形将最后绘制。
shp.list <- sapply(shp, FUN = function(x) Polygon(cbind(lon = x$x, lat = x$y)))
shp.poly <- Polygons(shp.list, "area")
shp.df <- fortify(shp.poly, region = "area")
shp.gg <- ggplot(shp.df, aes(x = long, y=lat, group = piece, order = hole))+geom_polygon()