4

私はfastshpライブラリに遭遇し、説明 (および私の簡単な大まかなテスト) によると、他の 3 つの方法と比較して、大きなシェープファイルの読み取り時間が実際に改善されています。

関数を使用してread.shp、maptools パッケージから例示的なデータセットをロードしています。

library("maptools")

setwd(system.file("shapes", package="maptools"))

shp <- read.shp("columbus.shp", format="polygon")

ドキュメントに従って「ポリゴン」形式を選択しました:

これは通常、プロットに適した形式です。

私の質問は、ggplot2 パッケージを使用してこれらのポリゴンをプロットするにはどうすればよいですか?

4

1 に答える 1

4

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()
于 2012-04-25T00:27:55.473 に答える