【发布时间】:2016-05-29 18:26:23
【问题描述】:
我正在研究 K-Means 集群 ggplot(),几乎可以做我希望做的事情,但我只是不知道如何将我的中心着色为与它们各自集群相同的颜色。
到目前为止,我有这个:
data(mtcars)
library(ggplot2)
c1 <- kmeans(mtcars,9)
x <- tapply(mtcars$mpg,c1$cluster,mean)
y <- tapply(mtcars$hp,c1$cluster,mean)
kcenters <- data.frame(x,y)
ggplot(mtcars,aes(mpg,hp))+geom_point(col=c1$cluster,size=4) + geom_point(data=kcenters,aes(x,y),pch=8,size=10)
所以我有两个问题,如何将我的中心着色为与它们所代表的集群相同?另外,我觉得x 和y 代码是多余的,不需要存在,因为在我的c1 值中,我可以看到带有位置矩阵的中心以及它们所代表的颜色。我只是无法弄清楚如何编写代码来访问这部分,因为每次尝试时都会收到诸如...之类的错误。
Error: Aesthetics must be either length 1 or the same as the data (9): shape, colour, size
另一个不太重要的问题是关于为什么我有两个不同的黑色集群。 R 不是有超过 8 种可以单独调用的独特颜色吗?
【问题讨论】: