0

英国のシェープファイルがありますが、イングランドとウェールズのみが必要です。これまでに使用したコードは次のとおりです。

RG <- readOGR("filepath", "filename")
UK <- grep("England", "Wales", RG$NAME_1)
RG_EW <- RG[UK]
plot(RG_EW)

しかし、私はまだ英国全体に行き着きます。http://www.gadm.org/からダウンロードしたシェープファイルを使用しています

ありがとう

4

2 に答える 2

2

名前が明確で、2 つだけを選択する必要がある場合は、次のように使用するのではなく、ワンライナーを使用しますgrep

e.w.shp <- uk.shp[uk.shp$NAME_1 == "England" | uk.shp$NAME_1 == "Wales", ]

結果は次のようになります。

> str(e.w.shp)
Formal class 'SpatialPolygonsDataFrame' [package "sp"] with 5 slots
  ..@ data       :'data.frame': 134 obs. of  11 variables:
  .. ..$ ID_0     : int [1:134] 239 239 239 239 239 239 239 239 239 239 ...
  .. ..$ ISO      : chr [1:134] "GBR" "GBR" "GBR" "GBR" ...
  .. ..$ NAME_0   : chr [1:134] "United Kingdom" "United Kingdom" "United Kingdom" "United Kingdom" ...
  .. ..$ ID_1     : int [1:134] 1 1 1 1 1 1 1 1 1 1 ...
  .. ..$ NAME_1   : chr [1:134] "England" "England" "England" "England" ...
  .. ..$ ID_2     : int [1:134] 1 2 3 4 5 6 7 8 9 10 ...
  .. ..$ NAME_2   : chr [1:134] "Barking and Dagenham" "Bath and North East Somerset" "Bedfordshire" "Berkshire" ...
  .. ..$ NL_NAME_2: chr [1:134] NA NA NA NA ...
  .. ..$ VARNAME_2: chr [1:134] NA NA NA NA ...
  .. ..$ TYPE_2   : chr [1:134] "London Borough" "Unitary Authority" "Administrative County" "County" ...
  .. ..$ ENGTYPE_2: chr [1:134] "London Borough" "Unitary Authority" "Administrative County" "County" ...
  ..@ polygons   :List of 134

したがって、ggplot2ベース グラフィックスではなく、完全に機能する例は次のようになります。

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

shape.dir <- "your_directory_name" # use your directory name here
uk.shp <- readOGR(shape.dir, layer = "GBR_adm2")
e.w.shp <- uk.shp[uk.shp$NAME_1 == "England" | uk.shp$NAME_1 == "Wales", ]
e.w.df <- fortify(e.w.shp, region = "ID_2") # convert to data frame for ggplot

ggplot(e.w.df, aes(x = long, y = lat, group = group)) +
    geom_polygon(colour = "black", fill = "grey80", size = 0.5) +
    theme()

スクリーンショット

于 2013-01-29T16:27:43.010 に答える
1

まず、あなたのgrep電話は間違っています。「イングランド」または「ウェールズ」を含む文字列を探している場合は、次のようにする必要があります。

UK <- grep("(England|Wales)", RG$NAME_1)

そして、次のようにデータをサブセット化できます。

RG_EW <- RG[UK,]

そして、あなたは最終的に得ます:

plot(RG_EW)

ここに画像の説明を入力

于 2013-01-29T14:33:25.487 に答える