library(ggpubr)
library(factoextra)
data("iris")
df <- iris
head(df, 3)
set.seed(123)
res.km <- kmeans(scale(df[, -5]), 3, nstart = 25)
res.km$cluster
fviz_cluster(res.km, data = df[, -5],
palette = c("#2E9FDF", "#00AFBB", "#E7B800"),
geom = "point",
ellipse.type = "convex",
ggtheme = theme_bw()
)
library(ggpubr)
library(factoextra)
data("iris")
df <- iris
head(df, 3)
set.seed(123)
res.pca <- prcomp(df[, -5], scale = TRUE)
ind.coord <- as.data.frame(get_pca_ind(res.pca)$coord)
ind.coord$cluster <- factor(res.km$cluster)
ind.coord$Species <- df$Species
head(ind.coord)
eigenvalue <- round(get_eigenvalue(res.pca), 1)
variance.percent <- eigenvalue$variance.percent
head(eigenvalue)
ggscatter(
ind.coord, x = "Dim.1", y = "Dim.2",
color = "cluster", palette = "npg", ellipse = TRUE, ellipse.type = "convex",
shape = "Species", size = 1.5, legend = "right", ggtheme = theme_bw(),
xlab = paste0("Dim 1 (", variance.percent[1], "% )" ),
ylab = paste0("Dim 2 (", variance.percent[2], "% )" )
) +
stat_mean(aes(color = cluster), size = 4)
原文链接