Title: map with shapefile

一些簡單的地圖操作 這邊使用的是台灣政府公開資料平台下載的shapefile http://data.gov.tw/node/7441

library(spdep)
## Warning: package 'spdep' was built under R version 3.0.3
## Loading required package: sp
## Warning: package 'sp' was built under R version 3.0.3
## Loading required package: Matrix
## Warning: package 'Matrix' was built under R version 3.0.3
## 
## Attaching package: 'Matrix'
## 
## The following objects are masked from 'package:base':
## 
##     crossprod, tcrossprod
library(maptools)
## Warning: package 'maptools' was built under R version 3.0.3
## Checking rgeos availability: TRUE
library(sp)
library(rgeos)
## Warning: package 'rgeos' was built under R version 3.0.3
## rgeos version: 0.3-8, (SVN revision 460)
##  GEOS runtime version: 3.4.2-CAPI-1.8.2 r3921 
##  Polygon checking: TRUE
#read shapefile
tw.map <- readShapeSpatial("C:\\Users\\taich_000\\Documents\\鄉鎮界_97TM2.shp")
plot(tw.map)

#data structure
head(slot(tw.map,"data"))
##     townid towncode countyname townname
## 0 10009006  1000912     雲林縣   崙背鄉
## 1 10009007  1000904     雲林縣   西螺鎮
## 2 10003010  1000304     桃園縣   楊梅市
## 3 10009020  1000918     雲林縣   四湖鄉
## 4 10009021  1000917     雲林縣   元長鄉
## 5 10009027  1000920     雲林縣   水林鄉
head(tw.map@data)
##     townid towncode countyname townname
## 0 10009006  1000912     雲林縣   崙背鄉
## 1 10009007  1000904     雲林縣   西螺鎮
## 2 10003010  1000304     桃園縣   楊梅市
## 3 10009020  1000918     雲林縣   四湖鄉
## 4 10009021  1000917     雲林縣   元長鄉
## 5 10009027  1000920     雲林縣   水林鄉
#select part of map
tw.map@data$countyID=as.numeric(substr(tw.map@data$towncode,1,5))
taipei=tw.map[tw.map@data$countyID==63000,]
plot(taipei)

#output shapefile
writePolyShape(taipei, "taipeishape")

#union polygon
countytaiwan<- unionSpatialPolygons(tw.map, tw.map$countyname)
plot(countytaiwan)

#compute polygon center
c1 = gCentroid(countytaiwan,byid=TRUE) 
points(c1)
text(c1@coords, levels(tw.map@data$countyname))