Menampilkan hasil clustering dari kmeans() menggunakan ggplot2.
library(ggplot2)
Data yang akan digunakan
iris_cluster <- iris[, -5]
head(iris_cluster)
cls <- kmeans(x = iris_cluster, centers = 3)
iris_cluster$cluster <- as.character(cls$cluster)
head(iris_cluster)
ggplot() +
geom_point(data = iris_cluster,
mapping = aes(x = Sepal.Length,
y = Petal.Length,
colour = cluster))
ggplot() +
geom_point(data = iris_cluster,
mapping = aes(x = Sepal.Length,
y = Petal.Length,
colour = cluster)) +
geom_point(mapping = aes_string(x = cls$centers[, "Sepal.Length"],
y = cls$centers[, "Petal.Length"]),
color = "red", size = 4)
ggplot() +
geom_point(data = iris_cluster,
mapping = aes(x = Sepal.Length,
y = Petal.Length,
colour = cluster)) +
geom_point(mapping = aes_string(x = cls$centers[, "Sepal.Length"],
y = cls$centers[, "Petal.Length"]),
color = "red", size = 4) +
geom_text(mapping = aes_string(x = cls$centers[, "Sepal.Length"],
y = cls$centers[, "Petal.Length"],
label = 1:3),
color = "black", size = 4) +
theme_light()
ggplot() +
geom_point(data = iris_cluster,
mapping = aes(x = Petal.Width,
y = Petal.Length,
colour = cluster)) +
geom_point(mapping = aes_string(x = cls$centers[, "Petal.Width"],
y = cls$centers[, "Petal.Length"]),
color = "red", size = 4) +
geom_text(mapping = aes_string(x = cls$centers[, "Petal.Width"],
y = cls$centers[, "Petal.Length"],
label = 1:3),
color = "black", size = 4) +
theme_light()