2012/10/26

R tips: read and visualize shapefile with R

Below sample code shows how to read shapefiles with R and plot them.
library(maptools)

# downloaded via http://www.globalmap.org/download/data/shp/1_1/shape_jp_1_1.zip
boundp <- readShapePoly("shape_jp_1_1/bnda_1_1.shp")
transl <- readShapeLines("shape_jp_1_1/transl_1_1.shp")

# Plot shapefile
plot(boundp)
plot(transl)

# plot shapefile within specified boundary
plot(boundp, xlim=c(135, 136), ylim=c(34,35))
lines(transl, xlim=c(135, 136), ylim=c(34,35), col='red')

# plot polygons which name equals 'OSAKA'
boundp_hyogo <- shp[(shp$nam == 'HYOGO'),]
x_range <- bbox(shp_tokyo)[1,]
y_range <- bbox(shp_tokyo)[2,]
plot(boundp_hyogo, xlim=x_range, ylim=y_range)
lines(transl, xlim=x_range, ylim=y_range, col='green')

No comments:

Post a Comment

100