-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathggplotcasestudy.R
332 lines (248 loc) · 10.5 KB
/
ggplotcasestudy.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
cols <- c (N = "#008000", `A°II` = "#0000FF", `A°III+` = "#FF0000", all = "black")
dimcols <- c (N = "#00800060", `A°II` = "#0000A060", `A°III+` = "#FF000060")
library (ggplot2)
library (hyperSpec)
load ("astrocytomas.RData")
astro
###### class membership values
## function to tabulate values against fraction of objects
fraction <- function (max.membership){
max.membership <- round (max.membership, digits = 3)
y <- sort (unique (max.membership), decreasing = TRUE)
x <- tabulate (factor (max.membership, levels = y))
x <- cumsum (x)
x <- x / tail (x, 1)
x <- c (0, x)
y <- c (y [1], y)
data.frame (x = x, y = y)
}
# function to rbind objects in a list
# putting the names of the list elements as grouping factor into column
rbind.w.name <- function (l){
n <- names (l)
for (i in n)
l [[i]]$class <- i
l <- do.call (rbind, l)
l$class <- factor (l$class, levels = n)
l
}
memberships <- c (list (all = apply (astro$label, 1, max)),
apply (astro$label, 2, function (x) x [x > 0]))
before <- sapply (memberships, length) # curiosity: how much reduction?
memberships <- lapply (memberships, fraction)
after <- sapply (memberships, nrow)
memberships <- rbind.w.name (memberships)
summary (memberships)
dim (memberships)
png ("membership.png", width = 400, height = 250)
ggplot (data = memberships, aes(x = x, y = y, colour = class)) +
geom_step () +
scale_colour_manual ("class", value = cols) +
scale_x_continuous (name = "Fraction of Spectra", expand = c (0, 0), limits = c (0, 1.005)) +
scale_y_continuous (name = "Class Membership", expand = c (0, 0), limits = c (0, 1.005))
dev.off ()
##### spectra plot
library (Hmisc)
wtd.percentilespc <- function (weights, spc, probs = c (.16, .5, .84)){
spc <- spc [weights > 0] # reduce calculation time by removing unrelated spectra
weights <- weights [weights > 0]
spc <- apply (spc, 2, wtd.quantile, weights = weights, probs = probs, normwt = FALSE)
spc$percentile <- probs
spc
}
spc <- apply (astro$label, 2, wtd.percentilespc, spc = astro [, "spc"])
spc <- rbind.w.name (spc)
spc <- sweep (spc, 2, graunorm, `+`)
spc
df <- rbind (cbind (as.long.df (spc [,, min ~ 1800]), wlrange = "low"),
cbind (as.long.df (spc [,, 2800 ~ max]), wlrange = "high"))
df <- cast (df, .wavelength + class + wlrange ~ percentile, value = "spc")
p <- ggplot (data = df) +
geom_ribbon (aes (x = .wavelength, ymin = `0.16`, ymax = `0.84`, fill = class), col = "black", size = 0.15) +
scale_fill_manual ("class", value = dimcols) +
geom_line (aes (x = .wavelength, y = `0.5`), size = 0.25)
p <- p + facet_grid (class ~ wlrange, scales = "free_x", space = "free",
labeller = function (variable, value){
if (variable == "wlrange") "" else value
})
p <- p + ylab (labels (spc, "spc")) +
scale_x_continuous (name = labels (spc, ".wavelength"), breaks = seq (800, 3000, 200), expand = c (0, 50)) +
opts (legend.position = "none")
png ("spc.png", width = 750, height = 300)
p
dev.off ()
####### 2d histogram of LDA projection
library (MASS)
lda <- lda (label.factor ~ spc, subset (astro$., label.factor != "soft"))
desc <- predict (lda, astro$.)$x
library (hexbin)
hist2d <- function (labels, desc, xbins, rng = range (desc)){
h <- hexbin (desc, xbins = xbins,
xbnds = rng, ybnds = rng,
IDs = TRUE)
counts <- hexTapply (h, 1 : nrow (labels),
function (i, labels){
colSums (labels [i,, drop = FALSE])
},
labels = labels)
counts <- t (matrix (unlist (counts), nrow = length (counts [[1]])))
colnames (counts) <- colnames (labels)
data.frame (hcell2xy (h), counts = I(counts))
}
h <- hist2d (astro$label, desc, 75)
colmix.rgb <- function (x, purecol, against = 1, min = 0, max = 1, sub = TRUE){
if (is.character (purecol))
purecol <- t (col2rgb (purecol)) / 255
if (sub)
x <- against - x %*% (against - purecol)
else
x <- x %*% purecol
x [x < min] <- min
x [x > max] <- max
cols <- rep (NA, nrow (x))
cols [! is.na (x [,1])] <- rgb (x [!is.na (x [, 1]),])
cols
}
h$col <- I (sweep (h$counts, 2, apply (h$count, 2, max), `/`))
h$col <- colmix.rgb (h$col, purecol = cols [1:3])
p <- ggplot (data = h, aes (x = x, y = y, fill = col, colour = col, group = 1)) + geom_hex (stat = StatIdentity) +
coord_equal () + scale_fill_identity() + scale_colour_identity() +
opts (panel.background = theme_rect(fill = NA, colour = NA),
panel.grid.major = theme_line(colour = NA),
panel.grid.minor = theme_line(colour = NA),
plot.margin = unit(c(0.5, 0, 0 ,0), "lines")
) +
scale_x_continuous (limits = c (-3.2,3.5)) +
scale_y_continuous (limits = c (-3, 2)) +
labs (x = "LDA 1", y = "LDA 2")
##### contours
library (sp)
peel <- function (x, y, weights = 1, probs = NA, threshold = 1 - 1e-3){
if (missing (y) && ncol (x) == 2){
y <- x [, 2]
x <- x [, 1]
}
if (length (x) != length (y))
stop ("x and y need to have the same length.")
weights <- rep (weights, length.out = length (x))
## start with all points
pts.in <- seq_along (x)
step <- 1
hulls <- list ()
## too small weights can confuse the peeling as the hull polygon treats all points equally
exclude <- weights < threshold
if (any (exclude)) {
## warning (sum (exclude), " points put into first hull due to too small weights")
hulls [[1]] <- pts.in [exclude]
pts.in <- pts.in [! exclude]
step <- step + 1
}
## peel off the hull polygons until nothing is left
while (length (pts.in) > 1){
hull <- chull (x [pts.in], y [pts.in])
hulls [[step]] <- pts.in [hull]
pts.in <- pts.in [-hull]
step <- step + 1
}
# now count the number of point-equivalents in each hull
n <- sapply (hulls, function (i) sum (weights [i]))
## and convert to percentiles
n <- cumsum (n)
qtl <- c(1, 1 - head (n, -1) / tail (n, 1))
names (hulls) <- qtl
if (! all (is.na (probs))){
i <- round (approx (qtl[-1], seq_along (hulls[-1]), probs, rule = 2)$y) + 1
hulls <- hulls [i]
}
hulls
}
median <- data.frame ()
for (class in seq_along (colnames (astro$label))){
contours <- peel (desc, weights = astro$label [, class] > 1-1e-5, probs = c (0, .5))
tmp <- apply (desc [contours [[1]],], 2, median)
median <- rbind (median, data.frame (x = tmp [1], y = tmp [2], class = class))
tmp <- data.frame (desc [contours [[2]],], class = class)
colnames (tmp) <- c ("x", "y")
p <- p + geom_polygon (data = tmp, aes (x = x, y = y), fill = NA, col = cols [class])
}
p <- p + geom_point (data = median, aes (x = x, y = y), fill = "white", col = "black", shape = 21, size = 3)
####### legend
legend <- function (purecolours, counts, dx = 0.33, classlabels = names (purecolours)) {
if (! is.matrix (counts))
counts <- matrix (counts, ncol = length (counts))
maxcnt <- apply (counts, 2, max)
df <- data.frame ()
for (class in seq_along (maxcnt)){
if (max (maxcnt) == 1)
tmp <- c (0, seq_len (maxcnt [class] * 100) / 100)
else
tmp <- c (0, seq_len (maxcnt [class]))
df <- rbind (df, data.frame (class = class,
col = colmix.rgb (tmp / maxcnt [class], purecolours [class]),
counts = tmp,
dx = dx,
dy = tmp[2] - tmp [1]))
}
l <- ggplot (df, aes (x = class), col = col) +
geom_point (aes (x=class, y = 1), col = NA) # trick to access continuous x values
l <- l + geom_rect (aes (xmin = as.numeric (class) - dx,
xmax = as.numeric (class) + dx,
ymin = counts - dy,
ymax = counts + dy,
fill = col,
colour = col), dx = force (dx)
)
l <- l + opts (plot.margin = unit(c(0.5, 0, 0 ,0), "lines"),
axis.text = p$options$legend.text,
axis.title = p$options$legend.text
) +
scale_fill_identity () + scale_colour_identity () +
scale_y_continuous (name = "counts", expand = c(0, max (df$dy)),
minor = pretty (c (0, max (maxcnt)), 25)) +
scale_x_continuous (name = "class", expand = c (0, 0.5 * dx), minor = NA, major = NA,
breaks = seq_along (maxcnt), labels = classlabels)
l
}
l <- legend (cols [-4], h$counts)
library (grid)
plot.with.legend.right <- function (graph, legend, legend.width = 8, legend.unit = "lines"){
plot.new()
pushViewport (viewport (layout = grid.layout (1, 2,
widths = unit (c (1, legend.width), c("null",legend.unit))
)))
print (graph, viewport (layout.pos.col = 1), newpage = FALSE)
print (legend, viewport (layout.pos.col = 2), newpage = FALSE)
popViewport ()
}
png ("hist2d.png", width = 750, height = 450)
plot.with.legend.right (p, l)
dev.off ()
##### histogram hard spectra only
hard <- astro$label.factor != "soft"
h.hard <- hist2d(astro$label[hard,], desc[hard,], 75, rng = range (desc))
h.hard$col <- I (sweep (h.hard$counts, 2, apply (h$count, 2, max), `/`))
h.hard$col <- colmix.rgb (h.hard$col, purecol = cols [1:3])
p <- ggplot (data = h.hard, aes (x = x, y = y, fill = col, colour = col, group = 1)) + geom_hex (stat = StatIdentity) +
coord_equal () + scale_fill_identity() + scale_colour_identity() +
opts (panel.background = theme_rect(fill = NA, colour = NA),
panel.grid.major = theme_line(colour = NA),
panel.grid.minor = theme_line(colour = NA),
plot.margin = unit(c(0.5, 0, 0 ,0), "lines")
) +
scale_x_continuous (limits = c (-3.2,3.5)) +
scale_y_continuous (limits = c (-3, 2)) +
labs (x = "LDA 1", y = "LDA 2")
median <- data.frame ()
for (class in seq_along (colnames (astro$label))){
contours <- peel (desc, weights = astro$label [, class] > 1-1e-5, probs = c (0, .5))
tmp <- apply (desc [contours [[1]],], 2, median)
median <- rbind (median, data.frame (x = tmp [1], y = tmp [2], class = class))
tmp <- data.frame (desc [contours [[2]],], class = class)
colnames (tmp) <- c ("x", "y")
p <- p + geom_polygon (data = tmp, aes (x = x, y = y), fill = NA, col = cols [class])
}
p <- p + geom_point (data = median, aes (x = x, y = y), fill = "white", col = "black", shape = 21, size = 3)
png ("hist2dhard.png", width = 750, height = 450)
plot.with.legend.right (p, l)
dev.off ()
p