你的意思是这样吗? 很抱歉,我不懂HTML5画布,只了解R语言...但希望对你有所帮助...
首先,我使用kmeans对数据进行聚类(请注意,我没有对距离矩阵进行聚类),然后计算距离矩阵并使用cmdscale绘制它。然后,我添加颜色到MDS图,这些颜色对应于kmeans识别出来的组。再加上一些漂亮的额外图形特征。
你可以从由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")
install.packages("devtools") ## To be able to download packages from github
library(devtools)
install_github("pablo14/clusplus")
library(clusplus)
## Create k-means model with 3 clusters
fit_mtcars=kmeans(mtcars,3)
## Call the function
plot_clus_coord(fit_mtcars, mtcars)
这篇文章解释了如何使用它。