[英]How to produce a pairwise plot of Kmeans clustering plots in R?
我對 R 中的 Iris 數據集執行 k 均值聚類分析。我試圖生成使用 kmeans 聚類的所有不同屬性組合(Sepal.Length、Sepal.Width、Petal.Length 和 Petal.Width)的成對圖3 的中心。我能夠為第一個組合(萼片長度 v 萼片寬度)生成如下圖:
attach(iris)
iris.scaled <- scale(iris[, -5])
k <- kmeans(iris.scaled,centers=3)
plot(iris.scaled[,1],iris.scaled[,2],col=KM$cluster,)
但是,我不確定如何對所有 6 種可能的屬性組合執行此操作,並具有 4 x 4 成對圖。 我想也許是pairs
功能,但沒有這樣的運氣
我提議:
library(GGally)
library(data.table)
attach(iris)
iris$Species <- NULL
iris.scaled <- data.table(scale(iris))
k <- kmeans(iris.scaled, centers=3)
iris.scaled[, cluster := as.factor(k$cluster)]
colnames(iris.scaled)
# ggplot(iris.scaled, aes(x = Sepal.Length, y = Sepal.Width)) +
# geom_point(aes(color = factor(cluster)))
ggpairs(iris.scaled, aes(colour = cluster, alpha = 0.4), columns = c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width"))
編輯:
您還可以刪除上面的圖表:
ggpairs(iris.scaled, aes(colour = cluster, alpha = 0.4),
columns = c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width"),
upper = "blank",
diag = NULL)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.