【问题标题】:How to plot this cluster in R?如何在 R 中绘制这个集群?
【发布时间】:2014-09-28 01:38:43
【问题描述】:

我有一堆不同点的 x 和 y 坐标以及它所属的集群。如何绘制集群?这是我正在使用的示例:

x-values    y-values    cluster
3           5           0
2           3           1
1           4           0
8           3           0
2           2           2
7           7           2

如何将点的散点图绘制为“*”或“+”并对集群进行着色,使其看起来像:

请注意,我没有进行 PCA 分析。

【问题讨论】:

标签: r cluster-analysis


【解决方案1】:

以下可能有用:

library(ggplot2)
ggplot(ddf, aes(x.values, y.values, color=factor(cluster)))+geom_point()

可以使用 stat_ellipse() 看到集群区域。由于以下错误,这些数据看不到它们:

ggplot(ddf, aes(x.values, y.values, color=factor(cluster)))+geom_point()+stat_ellipse()
Too few points to calculate an ellipse
Too few points to calculate an ellipse
Too few points to calculate an ellipse
geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?

如果点在使用虹膜数据的类似图中很好地聚类,效果会更好:

ggplot(iris, aes(Sepal.Length, Petal.Length, color=Species))+geom_point()+stat_ellipse()

【讨论】:

  • 如果我有两个簇列:预测簇和正确簇怎么办?我将如何用 ggplot 表示它,以便一个被圈出而另一个被遮蔽?另外,如果我有两列:x、y 坐标对应于簇的质心怎么办?如何将它作为“+”或“*”添加到图表中?
  • 预测的和正确的集群可以用不同的颜色显示。第二个问题我不清楚。最好是用可重复的例子开始另一个问题。
【解决方案2】:

您可以使用来自cluster 包的clusplot

clusplot(dat[,1:2], dat$cluster, color=TRUE, shade=TRUE, labels=2, lines=0)

dat 是你的矩阵。

【讨论】:

    猜你喜欢
    • 2017-06-24
    • 2016-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-28
    • 2014-06-24
    • 1970-01-01
    相关资源
    最近更新 更多