-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathplot.cascadeKM.R
114 lines (114 loc) · 4.19 KB
/
plot.cascadeKM.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
`plot.cascadeKM` <-
function (x, min.g, max.g, grpmts.plot = TRUE, sortg = FALSE,
gridcol = NA, ...)
{
wrapres <- x
number <- (as.numeric(gsub(" groups", "", colnames(wrapres$results))))
if (missing(min.g))
min.g <- min(number)
if (missing(max.g))
max.g <- max(number)
if (min.g < 2)
min.g = 2
c.min <- which(number == min.g)
c.max <- which(number == max.g)
if (length(c.min) == 0) {
stop("min.g value given has not been calculated by 'cascadeKM'\n")
}
if (length(c.max) == 0) {
stop("max.g value given has not been calculated by 'cascadeKM'\n")
}
x <- wrapres$partition[, c.min:c.max]
w <- wrapres$results[2, c.min:c.max]
criterion <- wrapres$criterion
x <- pregraphKM(x)
if (sortg) {
x <- orderingKM(x)
}
main = (paste("K-means partitions comparison"))
xlab = ("Number of groups in each partition")
ylab = ("Objects")
nc = ncol(x)
colo <- (rainbow(max.g + 1))
if (grpmts.plot) {
def.par <- par(no.readonly = TRUE)
nf <- layout(matrix(c(1, 2), 1, 2), widths = c(3, 1),
TRUE)
par(mar = c(5, 5, 5, 1), bg = "white", col = "black")
image(1:nrow(x), 1:nc, x, col = colo, yaxt = "n", frame.plot = TRUE,
main = main, xlab = ylab, ylab = xlab, bg = NA)
grid(nx = nrow(x), ny = max.g - min.g + 1, col = gridcol)
box()
axis(2, seq(min.g - min.g + 1, max.g - min.g + 1, by = 1),
labels = seq(min.g, max.g, by = 1))
axis(1)
par(mar = c(5, 2, 5, 1))
par(bg = "white", fg = "black", col = "black")
plot(y = min.g:max.g, x = w[1:nc], type = "b", main = paste(criterion,
"\ncriterion", sep = ""), ylab = "", ylim = c(min.g -
0.5, max.g + 0.5), yaxs = "i", yaxt = "n", xlab = "Values")
grid(nx = NULL, ny = max.g - min.g + 1, col = gridcol)
box()
axis(2, seq(min.g, max.g, by = 1), labels = seq(min.g, max.g,
by = 1), col.axis = "black")
axis(1)
maxx = which.max(w[])
minx = which.min(w[])
tops <- which(w[c(2:nc)] - w[c(1:(nc - 1))] > 0) + 1
maxx.o <- NA
if (length(tops) != 0) {
if (length(which(tops > maxx)) != 0)
maxx.o <- tops[which(tops > maxx)]
}
tops <- which(w[c(2:nc)] - w[c(1:(nc - 1))] < 0) + 1
minx.o <- NA
if (length(tops) != 0) {
if (length(which(tops > minx)) != 0)
minx.o <- tops[which(tops > minx)]
}
if (tolower(criterion) == "calinski") {
if (!is.na(maxx.o[1]))
points(y = maxx.o + min.g - 1, x = w[maxx.o],
col = "orange", pch = 19)
points(y = maxx + min.g - 1, x = w[maxx], col = "red",
pch = 19)
}
else if (tolower(criterion) == "likelihood") {
if (!is.na(maxx.o[1])) {
points(y = maxx.o + min.g - 1, x = w[maxx.o],
col = "orange", pch = 19)
}
points(y = maxx + min.g - 1, x = w[maxx], col = "red",
pch = 19)
}
else if (tolower(criterion) == "ssi") {
if (!is.na(maxx.o[1])) {
points(y = maxx.o + min.g - 1, x = w[maxx.o],
col = "orange", pch = 19)
}
points(y = maxx + min.g - 1, x = w[maxx], col = "red",
pch = 19)
}
else {
cat("When using the", criterion, "criterion, no red marker is",
"used to indicate the best value.\n")
}
par(def.par)
}
else {
tops <- which(w[c(2:nc)] - w[c(1:(nc - 1))] > 0) + 1
if (length(tops) != 0) {
maxx <- which.max(w[c(2:nc)] - w[c(1:nc - 1)]) +
1
}
else {
maxx <- which.max(w[])
tops = 1
}
}
out <- list(x = x, best.grps = maxx)
if (grpmts.plot)
invisible(out)
else
out
}