How to create a cluster plot in R?

前端 未结 2 1927
不知归路
不知归路 2020-12-23 08:47

How can I create a cluster plot in R without using clustplot?

I am trying to get to grips with some clustering (using R) and visualisation (using HTML5 Canvas).

2条回答
  •  心在旅途
    2020-12-23 09:10

    Did you mean something like this? Sorry but i know nothing about HTML5 Canvas, only R... But I hope it helps...

    First I cluster the data using kmeans (note that I did not cluster the distance matrix), than I compute the distance matix and plot it using cmdscale. Then I add colors to the MDS-plot that correspond to the groups identified by kmeans. Plus some nice additional graphical features.

    You can access the coordinates from the object created by cmdscale.

    ### some sample data
    require(vegan)
    data(dune)
    
    # kmeans
    kclus <- kmeans(dune,centers= 4, iter.max=1000, nstart=10000)
    
    # distance matrix
    dune_dist <- dist(dune)
    
    # Multidimensional scaling
    cmd <- cmdscale(dune_dist)
    
    # plot MDS, with colors by groups from kmeans
    groups <- levels(factor(kclus$cluster))
    ordiplot(cmd, type = "n")
    cols <- c("steelblue", "darkred", "darkgreen", "pink")
    for(i in seq_along(groups)){
      points(cmd[factor(kclus$cluster) == groups[i], ], col = cols[i], pch = 16)
    }
    
    # add spider and hull
    ordispider(cmd, factor(kclus$cluster), label = TRUE)
    ordihull(cmd, factor(kclus$cluster), lty = "dotted")
    

    enter image description here

提交回复
热议问题