我使用scatterplot3d
计算最佳聚类数 4 的地方进行了 kmeans 聚类 3D。现在我做了下面的代码来制作 3d kmeans 图。但我不知道如何在图中添加图例(集群 1、集群 2、...)和多边形或椭圆。
library(scatterplot3d)
df = read.csv(file.choose(), header = T)
str(df)
set.seed(137)
km = kmeans(df, 4, nstart=25)
df$cluster = factor(kmeans(df, 4)$cluster)
shapes = c(15, 17, 18, 19)
shapes = shapes[as.numeric(km$cluster)]
zz <- scatterplot3d(df[,-1], pch = shapes, grid = F, color = rainbow(4)[km$cluster])
zz.coords <- zz$xyz.convert(df[,-1])
text(zz.coords$x,
zz.coords$y,
labels = df[,1],
cex = .5,
pos = 4)
我用过的数据
df = structure(list(Station = c(91L, 92L, 94L, 95L, 96L, 97L, 98L,
99L, 103L, 105L, 106L, 107L, 112L, 113L, 114L, 115L, 116L, 117L,
119L, 122L, 123L), PC1 = c(-1.12015813, -0.162603353, -0.691170969,
1.290790838, -1.544758302, -0.027602401, -0.614168423, -0.495333696,
1.279949324, -0.63809094, -0.122668147, 0.744142235, -0.778789917,
-2.633418969, 0.758488847, -1.066042887, -0.721002477, 1.079359567,
-0.175695907, 5.318351482, 0.320422224), PC2 = c(0.415530088,
0.666545002, 0.695404632, -0.554688073, 0.222592134, 0.697855329,
1.242555625, 0.64505761, 0.702722431, 0.86363373, 0.827648227,
1.204083925, -4.232859175, -1.636251717, -0.685291914, 1.16637059,
-0.122979187, -1.485372609, 0.079916797, -0.402557579, -0.309915867
), PC3 = c(0.698238358, -0.071855245, -0.563762392, -0.382756976,
0.224635711, -0.786947583, -1.983627389, -0.032803567, -0.465769148,
-0.076804882, -1.775067301, -0.203906481, -0.419354014, -0.121035409,
-1.171532233, 3.578306494, 0.91365978, 0.951076125, 0.516367241,
0.770666924, 0.402271986), cluster = structure(c(2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 4L, 4L, 4L, 4L, 3L, 3L, 3L, 3L, 3L, 3L, 1L,
1L, 1L), .Label = c("1", "2", "3", "4"), class = "factor")), row.names = c(NA,
-21L), class = "data.frame")