-
Notifications
You must be signed in to change notification settings - Fork 29
/
manyboxplots.coffee
406 lines (346 loc) · 11.4 KB
/
manyboxplots.coffee
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# manyboxplots2.coffee
#
# Top panel is like a set of n box plots:
# lines are drawn at the 0.1, 1, 10, 25, 50, 75, 90, 99, 99.9 percentiles
# for each of n distributions
# Hover over a column in the top panel and the corresponding distribution
# is show below; click for it to persist; click again to make it go away.
#
manyboxplots = (data) ->
# dimensions of SVG
w = 1000
h = 450
pad = {left:60, top:20, right:60, bottom: 40}
# axis labels
ylab = "Response"
xlab = "Individuals"
# y-axis limits for top figure
topylim = [data.quant[0][0], data.quant[0][1]]
for i of data.quant
for x in data.quant[i]
topylim[0] = x if x < topylim[0]
topylim[1] = x if x > topylim[1]
topylim[0] = Math.floor(topylim[0])
topylim[1] = Math.ceil(topylim[1])
# y-axis limits for bottom figure
botylim = [0, data.counts[0][1]]
for i of data.counts
for x in data.counts[i]
botylim[1] = x if x > botylim[1]
indindex = d3.range(data.ind.length)
# adjust counts object to make proper histogram
br2 = []
for i in data.breaks
br2.push(i)
br2.push(i)
fix4hist = (d) ->
x = [0]
for i in d
x.push(i)
x.push(i)
x.push(0)
x
for i of data.counts
data.counts[i] = fix4hist(data.counts[i])
# number of quantiles
nQuant = data.quant.length
midQuant = (nQuant+1)/2 - 1
# x and y scales for top figure
xScale = d3.scale.linear()
.domain([-1, data.ind.length])
.range([pad.left, w-pad.right])
# width of rectangles in top panel
recWidth = xScale(1) - xScale(0)
yScale = d3.scale.linear()
.domain(topylim)
.range([h-pad.bottom, pad.top])
# function to create quantile lines
quline = (j) ->
d3.svg.line()
.x((d) -> xScale(d))
.y((d) -> yScale(data.quant[j][d]))
svg = d3.select("div#chart")
.append("svg")
.attr("width", w)
.attr("height", h)
# gray background
svg.append("rect")
.attr("x", pad.left)
.attr("y", pad.top)
.attr("height", h-pad.top-pad.bottom)
.attr("width", w-pad.left-pad.right)
.attr("stroke", "none")
.attr("fill", d3.rgb(200, 200, 200))
.attr("pointer-events", "none")
# axis on left
LaxisData = yScale.ticks(6)
Laxis = svg.append("g").attr("id", "Laxis")
# axis: white lines
Laxis.append("g").selectAll("empty")
.data(LaxisData)
.enter()
.append("line")
.attr("class", "line")
.attr("class", "axis")
.attr("x1", pad.left)
.attr("x2", w-pad.right)
.attr("y1", (d) -> yScale(d))
.attr("y2", (d) -> yScale(d))
.attr("stroke", "white")
.attr("pointer-events", "none")
# function to determine rounding of axis labels
formatAxis = (d) ->
d = d[1] - d[0]
ndig = Math.floor( Math.log(d % 10) / Math.log(10) )
ndig = 0 if ndig > 0
ndig = Math.abs(ndig)
d3.format(".#{ndig}f")
# axis: labels
Laxis.append("g").selectAll("empty")
.data(LaxisData)
.enter()
.append("text")
.attr("class", "axis")
.text((d) -> formatAxis(LaxisData)(d))
.attr("x", pad.left*0.9)
.attr("y", (d) -> yScale(d))
.attr("dominant-baseline", "middle")
.attr("text-anchor", "end")
# axis on bottom
BaxisData = xScale.ticks(10)
Baxis = svg.append("g").attr("id", "Baxis")
# axis: white lines
Baxis.append("g").selectAll("empty")
.data(BaxisData)
.enter()
.append("line")
.attr("class", "line")
.attr("class", "axis")
.attr("y1", pad.top)
.attr("y2", h-pad.bottom)
.attr("x1", (d) -> xScale(d-1))
.attr("x2", (d) -> xScale(d-1))
.attr("stroke", "white")
.attr("pointer-events", "none")
# axis: labels
Baxis.append("g").selectAll("empty")
.data(BaxisData)
.enter()
.append("text")
.attr("class", "axis")
.text((d) -> d)
.attr("y", h-pad.bottom*0.75)
.attr("x", (d) -> xScale(d-1))
.attr("dominant-baseline", "middle")
.attr("text-anchor", "middle")
# colors for quantile curves
colindex = d3.range((nQuant-1)/2)
tmp = d3.scale.category10().domain(colindex)
qucolors = []
for j in colindex
qucolors.push(tmp(j))
qucolors.push("black")
for j in colindex.reverse()
qucolors.push(tmp(j))
# curves for quantiles
curves = svg.append("g").attr("id", "curves")
for j in [0...nQuant]
curves.append("path")
.datum(indindex)
.attr("d", quline(j))
.attr("class", "line")
.attr("stroke", qucolors[j])
.attr("pointer-events", "none")
# vertical rectangles representing each array
indRectGrp = svg.append("g").attr("id", "indRect")
indRect = indRectGrp.selectAll("empty")
.data(indindex)
.enter()
.append("rect")
.attr("x", (d) -> xScale(d) - recWidth/2)
.attr("y", (d) -> yScale(data.quant[nQuant-1][d]))
.attr("id", (d) -> "rect#{data.ind[d]}")
.attr("width", recWidth)
.attr("height", (d) ->
yScale(data.quant[0][d]) - yScale(data.quant[nQuant-1][d]))
.attr("fill", "purple")
.attr("stroke", "none")
.attr("opacity", "0")
.attr("pointer-events", "none")
# vertical rectangles representing each array
longRectGrp = svg.append("g").attr("id", "longRect")
longRect = indRectGrp.selectAll("empty")
.data(indindex)
.enter()
.append("rect")
.attr("x", (d) -> xScale(d) - recWidth/2)
.attr("y", pad.top)
.attr("width", recWidth)
.attr("height", h - pad.top - pad.bottom)
.attr("fill", "purple")
.attr("stroke", "none")
.attr("opacity", "0")
# label quantiles on right
rightAxis = svg.append("g").attr("id", "rightAxis")
rightAxis.selectAll("empty")
.data(data.qu)
.enter()
.append("text")
.attr("class", "qu")
.text( (d) -> "#{d*100}%")
.attr("x", w)
.attr("y", (d,i) -> yScale(((i+0.5)/nQuant/2 + 0.25) * (topylim[1] - topylim[0]) + topylim[0]))
.attr("fill", (d,i) -> qucolors[i])
.attr("text-anchor", "end")
.attr("dominant-baseline", "middle")
# box around the outside
svg.append("rect")
.attr("x", pad.left)
.attr("y", pad.top)
.attr("height", h-pad.top-pad.bottom)
.attr("width", w-pad.left-pad.right)
.attr("stroke", "black")
.attr("stroke-width", 2)
.attr("fill", "none")
# lower svg
lowsvg = d3.select("div#chart").append("svg")
.attr("height", h)
.attr("width", w)
lo = data.breaks[0] - (data.breaks[1] - data.breaks[0])
hi = data.breaks[data.breaks.length-1] + (data.breaks[1] - data.breaks[0])
lowxScale = d3.scale.linear()
.domain([lo, hi])
.range([pad.left, w-pad.right])
lowyScale = d3.scale.linear()
.domain([0, botylim[1]+1])
.range([h-pad.bottom, pad.top])
# gray background
lowsvg.append("rect")
.attr("x", pad.left)
.attr("y", pad.top)
.attr("height", h-pad.top-pad.bottom)
.attr("width", w-pad.left-pad.right)
.attr("stroke", "none")
.attr("fill", d3.rgb(200, 200, 200))
# axis on left
lowBaxisData = lowxScale.ticks(8)
lowBaxis = lowsvg.append("g").attr("id", "lowBaxis")
# axis: white lines
lowBaxis.append("g").selectAll("empty")
.data(lowBaxisData)
.enter()
.append("line")
.attr("class", "line")
.attr("class", "axis")
.attr("y1", pad.top)
.attr("y2", h-pad.bottom)
.attr("x1", (d) -> lowxScale(d))
.attr("x2", (d) -> lowxScale(d))
.attr("stroke", "white")
# axis: labels
lowBaxis.append("g").selectAll("empty")
.data(lowBaxisData)
.enter()
.append("text")
.attr("class", "axis")
.text((d) -> formatAxis(lowBaxisData)(d))
.attr("y", h-pad.bottom*0.75)
.attr("x", (d) -> lowxScale(d))
.attr("dominant-baseline", "middle")
.attr("text-anchor", "middle")
grp4BkgdHist = lowsvg.append("g").attr("id", "bkgdHist")
histline = d3.svg.line()
.x((d,i) -> lowxScale(br2[i]))
.y((d) -> lowyScale(d))
randomInd = indindex[Math.floor(Math.random()*data.ind.length)]
hist = lowsvg.append("path")
.datum(data.counts[randomInd])
.attr("d", histline)
.attr("id", "histline")
.attr("fill", "none")
.attr("stroke", "purple")
.attr("stroke-width", "2")
histColors = ["blue", "red", "green", "MediumVioletRed", "black"]
lowsvg.append("text")
.datum(randomInd)
.attr("x", pad.left*1.1)
.attr("y", pad.top*2)
.text((d) -> data.ind[d])
.attr("id", "histtitle")
.attr("text-anchor", "start")
.attr("dominant-baseline", "middle")
.attr("fill", "blue")
clickStatus = []
for d in indindex
clickStatus.push(0)
longRect
.on "mouseover", (d) ->
d3.select("rect#rect#{data.ind[d]}")
.attr("opacity", "1")
d3.select("#histline")
.datum(data.counts[d])
.attr("d", histline)
d3.select("#histtitle")
.datum(d)
.text((d) -> data.ind[d])
.on "mouseout", (d) ->
if !clickStatus[d]
d3.select("rect#rect#{data.ind[d]}").attr("opacity", "0")
.on "click", (d) ->
console.log("Click: #{data.ind[d]} (#{d+1})")
clickStatus[d] = 1 - clickStatus[d]
d3.select("rect#rect#{data.ind[d]}").attr("opacity", clickStatus[d])
if clickStatus[d]
curcolor = histColors.shift()
histColors.push(curcolor)
d3.select("rect#rect#{data.ind[d]}").attr("fill", curcolor)
grp4BkgdHist.append("path")
.datum(data.counts[d])
.attr("d", histline)
.attr("id", "path#{data.ind[d]}")
.attr("fill", "none")
.attr("stroke", curcolor)
.attr("stroke-width", "2")
else
d3.select("path#path#{data.ind[d]}").remove()
# box around the outside
lowsvg.append("rect")
.attr("x", pad.left)
.attr("y", pad.top)
.attr("height", h-pad.bottom-pad.top)
.attr("width", w-pad.left-pad.right)
.attr("stroke", "black")
.attr("stroke-width", 2)
.attr("fill", "none")
svg.append("text")
.text(ylab)
.attr("x", pad.left*0.2)
.attr("y", h/2)
.attr("fill", "blue")
.attr("transform", "rotate(270 #{pad.left*0.2} #{h/2})")
.attr("dominant-baseline", "middle")
.attr("text-anchor", "middle")
lowsvg.append("text")
.text(ylab)
.attr("x", (w-pad.left-pad.bottom)/2+pad.left)
.attr("y", h-pad.bottom*0.2)
.attr("fill", "blue")
.attr("dominant-baseline", "middle")
.attr("text-anchor", "middle")
svg.append("text")
.text(xlab)
.attr("x", (w-pad.left-pad.bottom)/2+pad.left)
.attr("y", h-pad.bottom*0.2)
.attr("fill", "blue")
.attr("dominant-baseline", "middle")
.attr("text-anchor", "middle")
# add legend
text = "The top panel is like #{data.ind.length} boxplots:\n"
text += "lines are drawn at the "
for q,i in data.qu
if i > 0
text += ", "
text += "#{q*100}"
text += " percentiles for each of #{data.ind.length} distributions.\n"
d3.select("div#legend")
.style("opacity", 1)