title: spatial clustering in R (simple example)
body: <p>I have this simple <code>data.frame</code></p>
  
  <pre><code> lat&lt;-c(1,2,3,10,11,12,20,21,22,23)
   lon&lt;-c(5,6,7,30,31,32,50,51,52,53)
   data=data.frame(lat,lon)
  </code></pre>
  
  <p>The idea is to find the spatial clusters based on the distance</p>
  
  <p>First, I plot the map (lon,lat) :</p>
  
  <pre><code>plot(data$lon,data$lat)
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/nmzQ8.jpg" alt="enter image description here"></p>
  
  <p>so clearly I have three clusters based in the distance between the position of points.</p>
  
  <p>For this aim, I've tried this code in R :</p>
  
  <pre><code>d= as.matrix(dist(cbind(data$lon,data$lat))) #Creat distance matrix
  d=ifelse(d&lt;5,d,0) #keep only distance &lt; 5
  d=as.dist(d)
  hc&lt;-hclust(d) # hierarchical clustering
  plot(hc)
  data$clust &lt;- cutree(hc,k=3) # cut the dendrogram to generate 3 clusters
  </code></pre>
  
  <p>This gives : </p>
  
  <p><img src="https://i.stack.imgur.com/P9glW.jpg" alt="enter image description here"></p>
  
  <p>Now I try to plot the same points but with colors from clusters </p>
  
  <pre><code>plot(data$x,data$y, col=c("red","blue","green")[data$clust],pch=19)
  </code></pre>
  
  <p>Here the results </p>
  
  <p><img src="https://i.stack.imgur.com/HCZSk.jpg" alt="enter image description here"></p>
  
  <p>Which is not what I'm looking for.</p>
  
  <p>Actually, I want to find something like this plot </p>
  
  <p><img src="https://i.stack.imgur.com/SaXzI.jpg" alt="enter image description here"></p>
  
  <p>Thank you for help.</p>