### Voronoi tesselation
voronoi <- suppressMessages(suppressWarnings(require(deldir)))
cols <- c(
"#00000019",
"#DF536B19",
"#61D04F19",
"#2297E619",
"#28E2E519",
"#CD0BBC19",
"#F5C71019",
"#9E9E9E19"
)
### Helper function
test <- function(db, k) {
print(cl <- clustlearn::kmeans(db, k, 100))
plot(db, col = cl$cluster, asp = 1, pch = 20)
points(cl$centers, col = seq_len(k), pch = 13, cex = 2, lwd = 2)
if (voronoi) {
x <- c(min(db[, 1]), max(db[, 1]))
dx <- c(x[1] - x[2], x[2] - x[1])
y <- c(min(db[, 2]), max(db[, 2]))
dy <- c(y[1] - y[2], y[2] - y[1])
tesselation <- deldir(
cl$centers[, 1],
cl$centers[, 2],
rw = c(x + dx, y + dy)
)
tiles <- tile.list(tesselation)
plot(
tiles,
asp = 1,
add = TRUE,
showpoints = FALSE,
border = "#00000000",
fillcol = cols
)
}
}
### Example 1
test(clustlearn::db1, 2)
### Example 2
test(clustlearn::db2, 2)
### Example 3
test(clustlearn::db3, 3)
### Example 4
test(clustlearn::db4, 3)
### Example 5
test(clustlearn::db5, 3)
### Example 6
test(clustlearn::db6, 3)
### Example 7 (with explanations, no plots)
cl <- clustlearn::kmeans(
clustlearn::db5[1:20, ],
3,
details = TRUE,
waiting = FALSE
)
Run the code above in your browser using DataLab