How to cluster points and plot

前端 未结 1 1900
陌清茗
陌清茗 2020-12-21 03:37

I am trying to use clustering in R. I am a rookie and havent worked much with R.

I have the geo location points as latitude and longitude values. What I am looking t

相关标签:
1条回答
  • 2020-12-21 04:25

    Hierarchical clustering is one approach.

    First you construct a dendrogram:

    dend <- hclust(dist(theData), method="complete")
    

    I am using "complete" linkage here, so that all that the groups are merged by the maximum-distance "rule". This should be useful later if we want to make sure that all of our points in one group are at most a certain distance apart.

    I choose the distance of "2" (Because I am not sure how to convert your latitudes and longitudes to feet. You should convert first and then choose 600 instead of 2). Here is the resulting dendrogram with the cutting at height of "2".

    plot(dend, hang=-1)
    points(c(-100,100), c(2,2), col="red", type="l", lty=2)
    

    dendrogram

    Now each subtree intersected by the red line will become one cluster.

    groups <- cutree(theData, h=2) # change "h" here to 600 after converting to feet.
    

    We can plot them as a scatter plot to see how they look:

    plot(theData, col=groups)
    

    cluster_scatter

    Promising. The points nearby form clusters which is what we wanted.

    Let's add centers and circles around those centers with the radius of 1 (so that the max distance within the circle is 2):

    G1 <- tapply(theData[,1], groups, mean)  # means of groups
    G2 <- tapply(theData[,2], groups, mean)  # ...
    
    library(plotrix)  # for drawing circles
    plot(theData, col=groups)
    points(G1, G2, col= 1:6, cex=2, pch=19)
    for(i in 1:length(G1)) {  # draw circles
        draw.circle(G1[i], G2[i], 1, border=i,lty=3,lwd=3)
    }
    

    radius

    Looks like drawing circles around the mean is not the best way to capture all of the points within the cluster. Nevertheless visually it can be verified that maximum distance between the points in one groups is 2. (just try shifting circles a bit to encapsulate all of the points).

    0 讨论(0)
提交回复
热议问题