### loading data
bh <- readShapePoly(system.file("etc/shapes/bhicv.shp",
package="spdep")[1])
### data padronized
dpad <- data.frame(scale(bh@data[,5:8]))
### neighboorhod list
bh.nb <- poly2nb(bh)
### calculing costs
lcosts <- nbcosts(bh.nb, dpad)
### making listw
nb.w <- nb2listw(bh.nb, lcosts, style="B")
### find a minimum spanning tree
mst.bh <- mstree(nb.w,5)
### the mstree plot
par(mar=c(0,0,0,0))
plot(mst.bh, coordinates(bh), col=2,
cex.lab=.7, cex.circles=0.035, fg="blue")
plot(bh, border=gray(.5), add=TRUE)
### three groups with no restriction
res1 <- skater(mst.bh[,1:2], dpad, 2)
### thee groups with minimum population
res2 <- skater(mst.bh[,1:2], dpad, 2, 200000, bh@data$Pop)
### thee groups with minimun number of areas
res3 <- skater(mst.bh[,1:2], dpad, 2, 3, rep(1,nrow(bh@data)))
### groups frequency
table(res1$groups)
table(res2$groups)
table(res3$groups)
### the skater plot
par(mar=c(0,0,0,0))
plot(res1, coordinates(bh), cex.circles=0.035, cex.lab=.7)
### more one partition
res1b <- skater(res1, dpad, 1)
### length groups frequency
table(res1$groups)
table(res1b$groups)
### the skater plot, using other colors
plot(res1b, coordinates(bh), cex.circles=0.035, cex.lab=.7,
groups.colors=colors()[(1:length(res1b$ed))*10])
### the Spatial Polygons plot
plot(bh, col=heat.colors(4)[res1b$groups])
Run the code above in your browser using DataLab