我偶然发现了扣件库,根据描述(以及我的快速粗略测试),与其他三种方法相比,它确实提供了阅读大型shapefiles的时间上的改进。
我使用read.shp函数从maptools包加载示例性数据集:
library("maptools")
setwd(system.file("shapes", package="maptools"))
shp <- read.shp("columbus.shp", format="polygon")我选择了‘多边形’格式,因为根据文档:
这通常是绘图的首选格式。
我的问题是如何使用ggplot2包绘制这些多边形?
发布于 2012-04-25 00:27:55
由于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()https://stackoverflow.com/questions/10306831
复制相似问题