dimanche 30 novembre 2014

Spatial Clustering of Point Data in shapfiles with R


Helle,


i want to divide a number of point observation into k clusters, based on their spatial attributes only.


stations.txt


Code :



cur=setwd("C:/Users")

Algerie <- shapefile("DZA_adm/DZA_adm0.shp")
# read in ascii file, and assign column names
x <- read.table('E:/cluster/stations.txt', sep=";")
names(x) <- c('long', 'lat')

# subset original object, return only x,y cols
y <- data.frame(x[,1:2])
row.names(y) <- x$cat

# simple plot of x,y data
plot(y, pch=3)


# load cluster package
library(cluster)
library(flexclust)

# figure out a good number of clusters use a range of 2 to 10 clusters, with 20 reps each
s <- stepFlexclust(y, k=2:10, nrep=20)
#plot(s)

# 5 clusters in a good number
y.pam <- pam(y, 5, stand=TRUE)

# add the clustering vector back to the original dataframe
y$cluster <- y.pam$clustering

# plot the clusters by color

plot(y$long, y$lat, col=y$cluster, main="Bugsites Spatial Clustering, 5 classes", cex=0.5, pch=16, xlab="long", ylab="lat",add=TRUE)

# add the cluster number to the original dataframe
y$cluster <- y.pam$clustering
y$orig_cat <- as.numeric(row.names(y))

# save as a text file and quit
write.table(y, file='bugsites.clust', row.names=FALSE)


enter image description here I started in R, looking for a way ploter the colorful dots Each one with its cluster on a shapefile (Algeria) and then unscrew the board (shapfiles) as cluster exists.


thank you





Aucun commentaire:

Enregistrer un commentaire