如何使用fortify()从shapefile创建过滤的R数据帧?


11

我正在建立ggplot威尔士行政区人口的总人口图。我已经从军械测量局下载了边界线数据,并提取了似乎正确的shapefile(community_ward_region.shp)。使用R,我已经读完shapefile。

require(maptools)
shape <- readShapePoly(wards)
str(shape)

这给了我这个有希望的输出:

Formal class 'SpatialPolygonsDataFrame' [package "sp"] with 5 slots
  ..@ data       :'data.frame': 1690 obs. of  4 variables:
  .. ..$ NAME      : Factor w/ 1507 levels "Abbey Cwmhir",..: 969 90 111 200 441 477 1455 249 255 305 ...
  .. ..$ DESCRIPTIO: Factor w/ 4 levels "COMMUNITY","COMMUNITY WARD",..: 2 2 1 1 2 2 2 2 1 1 ...
  .. ..$ COMMUNITY : Factor w/ 858 levels "Abbey Cwmhir",..: 67 67 81 128 152 152 152 152 157 190 ...
  .. ..$ FILE_NAME : Factor w/ 23 levels "ABERTAWE_-_SWANSEA",..: 1 1 1 1 1 1 1 1 1 1 ...
  .. ..- attr(*, "data_types")= chr [1:4] "C" "C" "C" "C"
  ..@ polygons   :List of 1690
  .. ..$ :Formal class 'Polygons' [package "sp"] with 5 slots
  .. .. .. ..@ Polygons :List of 1
  .. .. .. .. ..$ :Formal class 'Polygon' [package "sp"] with 5 slots
  .. .. .. .. .. .. ..@ labpt  : num [1:2] 259009 188524
  .. .. .. .. .. .. ..@ area   : num 1923892
  .. .. .. .. .. .. ..@ hole   : logi FALSE
  .. .. .. .. .. .. ..@ ringDir: int 1
  .. .. .. .. .. .. ..@ coords : num [1:1629, 1:2] 259413 259420 259427 259427 259432 ...
  .. .. .. ..@ plotOrder: int 1
  .. .. .. ..@ labpt    : num [1:2] 259009 188524
  .. .. .. ..@ ID       : chr "0"
  .. .. .. ..@ area     : num 1923892

现在,如果我这样做:

bar <- fortify(shape, region = "NAME")

我得到了一个很好的数据框bar,看起来与我预期的差不多:

> str(bar)
'data.frame':   4744053 obs. of  7 variables:
 $ long : num  302962 302970 302974 303013 303015 ...
 $ lat  : num  280066 280076 280078 280097 280105 ...
 $ order: int  1 2 3 4 5 6 7 8 9 10 ...
 $ hole : logi  FALSE FALSE FALSE FALSE FALSE FALSE ...
 $ piece: Factor w/ 29 levels "1","2","3","4",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ group: Factor w/ 1762 levels "Abbey Cwmhir.1",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ id   : chr  "Abbey Cwmhir" "Abbey Cwmhir" "Abbey Cwmhir" "Abbey Cwmhir" ..

但是,这是一个很大的数据帧,ggplot在尝试显示时会用完粉扑。实际上,我一次只想看一个区域。看起来FILE_NAME形状对象中的因素正是我想要的,因为它主要对应于县和主要城市。

> unique(shape@data$FILE_NAME)
 [1] ABERTAWE_-_SWANSEA
 [2] BLAENAU_GWENT_-_BLAENAU_GWENT
 [3] BRO_MORGANNWG_-_THE_VALE_OF_GLAMORGAN
 [4] CAERDYDD_-_CARDIFF
 [5] CAERFFILI_-_CAERPHILLY
 [6] CASNEWYDD_-_NEWPORT
 [7] CASTELL-NEDD_PORT_TALBOT_-_NEATH_PORT_TALBOT
 [8] CONWY_-_CONWY
 [9] GWYNEDD_-_GWYNEDD
[10] MERTHYR_TUDFUL_-_MERTHYR_TYDFIL
[11] PEN-Y-BONT_AR_OGWR_-_BRIDGEND
[12] POWYS_-_POWYS
[13] RHONDDA_CYNON_TAF_-_RHONDDA_CYNON_TAFF
[14] SIR BENFRO - PEMBROKESHIRE
[15] SIR_BENFRO_-_PEMBROKESHIRE
[16] SIR_CEREDIGION_-_CEREDIGION
[17] SIR_DDINBYCH_-_DENBIGHSHIRE
[18] SIR_FYNWY_-_MONMOUTHSHIRE
[19] SIR_GAERFYRDDIN_-_CARMARTHENSHIRE
[20] SIR_YNYS_MON_-_ISLE_OF_ANGLESEY
[21] SIR_Y_FFLINT_-_FLINTSHIRE
[22] TOR-FAEN_-_TORFAEN
[23] WRECSAM_-_WREXHAM
23 Levels: ABERTAWE_-_SWANSEA ... WRECSAM_-_WREXHAM

问:如何仅从shape从shapefile中提取的对象中选择数据的子集?例如,仅POWYS_-_POWYS零件?如果我可以以某种方式将包含FILE_NAME在创建的数据框中,则fortify可以轻松地对bar数据框进行子集设置,但我不知道该怎么做。还是有一种方法可以fortify用来仅提取对象的一部分?

Answers:


5

使用索引选择shapefile数据的子集:

sub.shape <- shape[shape$FILE_NAME == "POWYS_-_POWYS",]

fortify(sub.shape) 然后会为您减少数据帧。


谢谢,现在检查,看起来很有希望。那末应该是“,]”吗?
SlowLearner 2012年

1
@slowlearner-糟糕,是的,缺少逗号,是我永远的错误……
Simbamangu 2012年
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.