Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
Load the shapefile using readShapePoly()
from the maptools package, convert it to a data frame with fortify(), then
plot it (Figure 13-39):
library(maptools)# Load the shapefile and convert to a data frametaiwan_shp<-readShapePoly("TWN_adm/TWN_adm2.shp")taiwan_map<-fortify(taiwan_shp)ggplot(taiwan_map,aes(x=long,y=lat,group=group))+geom_path()
Esri shapefiles are a common format for map data. The readShapePoly() function reads a shape file
and returns a SpatialPolygonsDataFrame object:
taiwan_shp<-readShapePoly("TWN_adm/TWN_adm2.shp")# Look at the structure of the objectstr(taiwan_shp)Formal class'SpatialPolygonsDataFrame'[package"sp"]with5slots..@data :'data.frame':22obs. of11variables:....$ID_0 : int[1:22]223223223223223223223223223223.......$ISO : Factor w/1level"TWN":1111111111.......$NAME_0 : Factor w/1level"Taiwan":1111111111.......$ID_1 : int[1:22]1234444444......[lots more stuff]..@proj4string:Formal class'CRS'[package"sp"]with1slots......@projargs: chrNA