spatial clustering in R (simple example)

Math picture Math · Feb 23, 2015 · Viewed 18.5k times · Source

I have this simple data.frame

 lat<-c(1,2,3,10,11,12,20,21,22,23)
 lon<-c(5,6,7,30,31,32,50,51,52,53)
 data=data.frame(lat,lon)

The idea is to find the spatial clusters based on the distance

First, I plot the map (lon,lat) :

plot(data$lon,data$lat)

enter image description here

so clearly I have three clusters based in the distance between the position of points.

For this aim, I've tried this code in R :

d= as.matrix(dist(cbind(data$lon,data$lat))) #Creat distance matrix
d=ifelse(d<5,d,0) #keep only distance < 5
d=as.dist(d)
hc<-hclust(d) # hierarchical clustering
plot(hc)
data$clust <- cutree(hc,k=3) # cut the dendrogram to generate 3 clusters

This gives :

enter image description here

Now I try to plot the same points but with colors from clusters

plot(data$x,data$y, col=c("red","blue","green")[data$clust],pch=19)

Here the results

enter image description here

Which is not what I'm looking for.

Actually, I want to find something like this plot

enter image description here

Thank you for help.

Answer

johannes picture johannes · Feb 23, 2015

What about something like this:

lat<-c(1,2,3,10,11,12,20,21,22,23)
lon<-c(5,6,7,30,31,32,50,51,52,53)

km <- kmeans(cbind(lat, lon), centers = 3)
plot(lon, lat, col = km$cluster, pch = 20)

enter image description here