我正在使用R中的factoextra库来处理K-means聚类。我能够创建我的PCA图,显示数据点的聚类成员资格,但我希望使用时间变量来塑造我的数据点。我已经在下面粘贴了我的虚拟代码,似乎fviz_cluster无法识别‘时间’变量。
我会感谢所有的帮助和意见。
k2 <- kmeans(Scaled_data, centers = 2, nstart = 25)
k2$Time <- as.factor(time)
print(names(k2))
print(length(k2$Time))
print(length(k2$cluster))
plot_Obj <- fviz_cluster(k2, data = Scaled_data,
stand = FALSE,
ellipse.type = "norm",
geom = "point",
alpha=0.5,
ggtheme = theme_minimal(),
repel = FALSE,
shape=Time)
print(plot_Obj)
Output:
[1] "cluster" "centers" "totss" "withinss"
"tot.withinss" [6] "betweenss" "size" "iter" "ifault" "Time"
[1] 783
[1] 783
Error:
Error in fviz_cluster(k2, data = Scaled_data, stand = FALSE,
ellipse.type = "norm", : object 'Time' not found
Execution halted发布于 2019-05-19 09:04:40
和scale_shape_manual()在一起。
library(factoextra)
set.seed(123)
data("iris")
iris.scaled <- scale(iris[, -5])
km.res <- kmeans(iris.scaled, 3, nstart = 10)
km.res$cluster
shapex <- data.frame(clust = km.res$cluster) %>%
dplyr::mutate(shape = ifelse(clust == 1, 21,
ifelse(clust == 2, 22,
ifelse(clust == 3, 23, "ERROR"))))
p <- fviz_cluster(km.res, iris[, -5], ellipse.type = "norm")
p

p + scale_shape_manual(values = 10:12)

请注意,点的数量等于簇的数量。可用的形状包括:

https://stackoverflow.com/questions/55173789
复制相似问题