ClustDistSpat <-function(pts, X, Y, lon, lat)
{
require("maptools")
require("ggmap")
require("ggplot2")
require("plyr")
require("fossil")
pts1_a<-readShapePoints("pts")
pts1<-as.data.frame(pts1_a)
#Obtain the layers from Google Maps
map <- get_map(location = c(lon=lon, lat=lat),zoom = "auto")
# map zoom, an integer from 3 (continent) to 21 (building)
ggmap(map)
mapPoints <- ggmap(map) + geom_point(aes(X =X, Y =Y), data=pts1, alpha = .5)
##Cluster analysis
dist.g <- earth.dist(pts1)
kmean <- kmeans(dist.g,centers=5)
#centers is the number of clusters k
hcluster <- hclust(dist.g,method="complete")
clust <- cutree(hcluster, k=5 )
set.seed(123)
pts1a$clust <- cutree(hcluster,k=5)
results<- ggmap(map)+geom_point(data=pts1, aes(x=X, y=Y, color=factor(clust)), size=4)+
scale_color_discrete("Cluster") + coord_fixed()
return (results)
}
funcaoclustdistspatfinal.r
scripthelpclustdistspat.r