DATA MINING
Desktop Survival Guide by Graham Williams |
|||||
Spatial Data |
R provides a set of tools for reading geographic (map) data, particularly ESRI shapefiles, and plotting and manipulating such data. Maps for many countries are available, particularly the US, Europe, and New Zealand. Only limited Australian map data is also freely available.
> if (!file.exists('australia.shp')) { download.file(paste("http://www.vdstech.com/mapdata/", "australia.zip", sep=""), "australia.zip") system("unzip australia.zip; rm australia.zip") } |
The data can be loaded into R with readShapePoly and displayed with plot. Australia has a few outlying islands which we crop from the main focus of the map here using Roption[]xlim and Roption[]ylim.
> library(maptools) |
Note: polygon geometry computations in maptools depend on the package gpclib, which has a restricted licence. It is disabled by default; to enable gpclib, type gpclibPermit() Checking rgeos availability as gpclib substitute: FALSE |
> aus <- readShapePoly("australia.shp") > plot(aus, lwd=2, border="grey", xlim=c(115,155), ylim=c(-35,-20)) |
The class of the resulting object (aus) is SpatialPolygonsDataFrame. Such an object has a collection of slots. For example, the data slot includes meta information about the region recorded in the data frame.
> aus@data |
FIPS_ADMIN GMI_ADMIN ADMIN_NAME 0 AS01 AUS-ACT Australian Capital Territory 1 AS02 AUS-NSW New South Wales 2 AS03 AUS-NTR Northern Territory 3 AS04 AUS-QNS Queensland 4 AS05 AUS-SAS South Australia 5 AS06 AUS-TSM Tasmania 6 AS07 AUS-VCT Victoria 7 AS08 AUS-WAS Western Australia FIPS_CNTRY CNTRY_NAME REGION CONTINENT 0 AS Australia Australia/New Zealand Australia 1 AS Australia Australia/New Zealand Australia 2 AS Australia Australia/New Zealand Australia 3 AS Australia Australia/New Zealand Australia 4 AS Australia Australia/New Zealand Australia 5 AS Australia Australia/New Zealand Australia 6 AS Australia Australia/New Zealand Australia 7 AS Australia Australia/New Zealand Australia POP_ADMIN SQKM_ADMIN SQMI_ADMIN TYPE_ENG TYPE_LOC 0 292475 2342.295 904.36 Territory Territory 1 6338919 803110.812 310081.09 State State 2 161294 1352365.000 522148.09 Territory Territory 3 3107362 1733475.000 669294.69 State State 4 1445153 985308.500 380427.59 State State 5 472122 68131.477 26305.56 State State 6 4354611 227781.406 87946.40 State State 7 1655588 2533628.000 978233.81 State State |
The bounding box of the plot is available using bbox.
> bbox(aus) |
min max x 112.90721 159.10190 y -54.75389 -10.05139 |