0

enter image description hereI did clustering by using function hclust, now I want to draw polygons around each cluster!

How I can do it? I only have a group of point with the cluster id!

for example this image has two classes and 4 distinct areas! How I can get the number of these areas?

Community
  • 1
  • 1

1 Answers1

2

You can do this using the convex hull function chull.

## First part sets up problem
library(cluster)        ## For Ruspinin data
Rusp_HC = hclust(dist(ruspini))
Cluster4 = cutree(Rusp_HC, 4)
plot(ruspini, pch=20, col=rainbow(4)[Cluster4])

##  Now get the polygons
for(i in 1:4) {
    ConvexHull = chull(ruspini[Cluster4 == i, ])
    polygon(ruspini[Cluster4 == i, ][ConvexHull,], 
        border=rainbow(4)[i], col=rainbow(4, alpha=0.1)[i])
}

Convex hulls

G5W
  • 36,531
  • 10
  • 47
  • 80
  • thanks for your answer! I tried this but i get an error for my data! I should mention that for example I have two data points for cluster 1, 3 forr cluster 2 and so on! I get this error; Error in ruspini[Cluster4 == i, ][ConvexHull, ] : incorrect number of dimensions – Oumnia Asadian Jul 05 '17 at 14:40
  • @OumniaAsadian You will need to substitute your data for the Ruspini data. I just used that as an example. – G5W Jul 05 '17 at 14:45
  • yep I replaced my data of course! but I get this error on my data! :/ – Oumnia Asadian Jul 05 '17 at 14:52
  • ok I just solved the problem, but now it doesnt draw around each datapoints!! I only get few polygons by this! by what i wanna ask how i can easily find out the number of polygons when I plot the original area with its clusters! – Oumnia Asadian Jul 05 '17 at 15:59
  • it doesnt work it only gives me the number of classes! – Oumnia Asadian Jul 05 '17 at 19:04
  • Maybe I was confused. I thought that was what you wanted to know. Don't you have one polygon per cluster? – G5W Jul 05 '17 at 19:08
  • no each cluster has more than one polygons! I dont know if you can see in the image, I uploaded an image some minutes ago! – Oumnia Asadian Jul 05 '17 at 19:14
  • Your picture does not look like what I expect to see coming out of cluster analysis and I am not sure what you are asking for. For example, the green points. Do you want one polygon that contains all green points? If not, how should they be divided? – G5W Jul 05 '17 at 19:25
  • yes I clusters the raster image by hclust and plot it in this way :clustersimage <- plot(raster, col=col_vector[clusters]) I already know for example I have two classes but I also want to get this infos by code that how many polygons( distinct areas) I get( like in the image which is four polygons, one green and 3 grey) – Oumnia Asadian Jul 05 '17 at 19:33