如何在R中创建聚类图? [英] How to create a cluster plot in R?
问题描述
如何在不使用 clustplot ?
我正在设法处理一些群集(使用R)和可视化(使用HTML5 Canvas).
I am trying to get to grips with some clustering (using R) and visualisation (using HTML5 Canvas).
Basically, I want to create a cluster plot but instead of plotting the data, I want to get a set of 2D points or coordinates that I can pull into canvas and do something might pretty with (but I am unsure of how to do this). I would imagine that I:
- 为整个数据集创建一个相似度矩阵(使用dist)
- 使用kmeans或类似的东西(使用kmeans)聚集相似性矩阵
- 使用MDS或PCA绘制结果-但是我不确定第2步和第3步的关系(cmdscale).
我已经在此处,(最后一个是最常用的).
I've checked out questions here, here and here (with the last one being of most use).
推荐答案
您的意思是这样的吗? 抱歉,但是我对HTML5 Canvas一无所知,只有R ...但是我希望对您有所帮助...
Did you mean something like this? Sorry but i know nothing about HTML5 Canvas, only R... But I hope it helps...
首先,我使用kmeans对数据进行聚类(请注意,我没有对距离矩阵进行聚类),然后我计算距离矩阵并使用cmdscale对其进行绘制.然后,我向MDS绘图添加与kmeans标识的组相对应的颜色.加上一些不错的附加图形功能.
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.
您可以从cmdscale创建的对象访问坐标.
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")
这篇关于如何在R中创建聚类图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!