如何使用 R 更改簇的颜色?

How to change the color of clusters using R?

我将 R 用于 Kmeans 聚类,因此我加载了库 (fpc)​​,并使用 plotcluster 方法绘制数据。

clus <- kmeans(data, centers = 5)
plotcluster(data, clus$cluster, pch = clus$cluster)

因此绘制了 5 组粒子,但不同组具有默认颜色。但是我怎样才能改变粒子的颜色呢?

例如,第 1 组对应某种特定颜色,第 2 组对应某种特定颜色。

plotcluster函数中有个参数叫“col”,不知道怎么改

这是我在 Mac OS X 10.10.2 上使用最新安装的 fpc 包在 RStudio 运行 上测试的代码。

我绝不建议您使用这些颜色,但如果您添加以下两个参数,我认为您将能够 select 您想要的颜色:

vector of class numbers which can be coerced into integers; length must equal nrow(xd)

clvecd = c(1,2,3,4,5)

vector of colors that correspond to your clusters

col=c("blue","green","purple","red","yellow")

plotcluster(  data
            , clus$cluster
            , pch = clus$cluster
            , clvecd=c(1,2,3,4,5)
            , col=c("blue","green","purple","red","yellow"))

有点晚了,嘿。我今天遇到了这个问题并找到了这个解决方案。

clus <- kmeans(x, centers=5)
vcol <- c("blue","green","purple","red","yellow")
plotcluster(x, clus$cluster, col=vcol[clus$cluster])

注意:
如果 length(vcol) - centers = 1,最后的颜色将不会被使用。
如果 centers - length(vcol) = 1,最后一个簇中的点将不会显示。