forked from GiovineItalia/Gadfly.jl
/
aesthetics.jl
453 lines (372 loc) · 13.1 KB
/
aesthetics.jl
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
typealias NumericalOrCategoricalAesthetic
Union(Nothing, Vector, DataArray, PooledDataArray)
typealias CategoricalAesthetic
Union(Nothing, PooledDataArray)
typealias NumericalAesthetic
Union(Nothing, Matrix, Vector, DataArray)
@varset Aesthetics begin
x, Union(NumericalOrCategoricalAesthetic, Distribution)
y, Union(NumericalOrCategoricalAesthetic, Distribution)
z, Union(Nothing, Function, Matrix)
size, Maybe(Vector{Measure})
color, Maybe(Union(AbstractDataVector{RGBA{Float32}},
AbstractDataVector{RGB{Float32}}))
label, CategoricalAesthetic
group, CategoricalAesthetic
xmin, NumericalAesthetic
xmax, NumericalAesthetic
ymin, NumericalAesthetic
ymax, NumericalAesthetic
# hexagon sizes used for hexbin
xsize, NumericalAesthetic
ysize, NumericalAesthetic
# fixed lines
xintercept, NumericalAesthetic
yintercept, NumericalAesthetic
# boxplots
middle, NumericalAesthetic
lower_hinge, NumericalAesthetic
upper_hinge, NumericalAesthetic
lower_fence, NumericalAesthetic
upper_fence, NumericalAesthetic
outliers, NumericalAesthetic
width, NumericalAesthetic
# subplots
xgroup, CategoricalAesthetic
ygroup, CategoricalAesthetic
# guides
xtick, NumericalAesthetic
ytick, NumericalAesthetic
xgrid, NumericalAesthetic
ygrid, NumericalAesthetic
color_key_colors, Maybe(Associative)
color_key_title, Maybe(String)
color_key_continuous, Maybe(Bool)
color_function, Maybe(Function)
titles, Maybe(Dict{Symbol, String})
# mark some ticks as initially invisible
xtickvisible, Maybe(Vector{Bool})
ytickvisible, Maybe(Vector{Bool})
# scale at which ticks should become visible
xtickscale, Maybe(Vector{Float64})
ytickscale, Maybe(Vector{Float64})
# plot viewport extents
xviewmin, Any
xviewmax, Any
yviewmin, Any
yviewmax, Any
# labeling functions
x_label, Function, showoff
y_label, Function, showoff
xtick_label, Function, showoff
ytick_label, Function, showoff
color_label, Function, showoff
xgroup_label, Function, showoff
ygroup_label, Function, showoff
end
function show(io::IO, data::Aesthetics)
maxlen = 0
print(io, "Aesthetics(")
for name in fieldnames(Aesthetics)
if getfield(data, name) != nothing
print(io, "\n ", string(name), "=")
show(io, getfield(data, name))
end
end
print(io, "\n)\n")
end
# Alternate aesthetic names
const aesthetic_aliases =
@compat Dict{Symbol, Symbol}(:colour => :color,
:x_min => :xmin,
:x_max => :xmax,
:y_min => :ymin,
:y_max => :ymax,
:x_group => :xgroup,
:y_group => :ygroup,
:x_viewmin => :xviewmin,
:x_viewmax => :xviewmax,
:y_viewmin => :yviewmin,
:y_viewmax => :yviewmax,
:x_group_label => :xgroup_label,
:y_group_label => :ygroup_label,
:x_tick => :xtick,
:y_tick => :ytick,
:x_grid => :xgrid,
:y_grid => :ygrid)
# Index as if this were a data frame
function getindex(aes::Aesthetics, i::Integer, j::String)
getfield(aes, symbol(j))[i]
end
# Return the set of variables that are non-nothing.
function defined_aesthetics(aes::Aesthetics)
vars = Set{Symbol}()
for name in fieldnames(Aesthetics)
if !is(getfield(aes, name), nothing)
push!(vars, name)
end
end
vars
end
# Checking aesthetics and giving reasonable error messages.
# Raise an error if any of the given aesthetics are not defined.
#
# Args:
# who: A string naming the caller which is printed in the error message.
# aes: An Aesthetics object.
# vars: Symbol that must be defined in the aesthetics.
#
function undefined_aesthetics(aes::Aesthetics, vars::Symbol...)
setdiff(Set(vars), defined_aesthetics(aes))
end
function assert_aesthetics_defined(who::String, aes::Aesthetics, vars::Symbol...)
undefined_vars = undefined_aesthetics(aes, vars...)
if !isempty(undefined_vars)
error(@sprintf("The following aesthetics are required by %s but are not defined: %s\n",
who, join(undefined_vars, ", ")))
end
end
function assert_aesthetics_equal_length(who::String, aes::Aesthetics, vars::Symbol...)
defined_vars = Symbol[]
for var in filter(var -> !(getfield(aes, var) === nothing), vars)
push!(defined_vars, var)
end
n = length(getfield(aes, vars[1]))
for i in 2:length(vars)
if length(getfield(aes, vars[1])) != n
error(@sprintf("The following aesthetics are required by %s to be of equal length: %s\n",
who, join(vars, ", ")))
end
end
end
# Create a shallow copy of an Aesthetics instance.
#
# Args:
# a: aesthetics to copy
#
# Returns:
# Copied aesthetics.
#
copy(a::Aesthetics) = Aesthetics(a)
# Replace values in a with non-nothing values in b.
#
# Args:
# a: Destination.
# b: Source.
#
# Returns: nothing
#
# Modifies: a
#
function update!(a::Aesthetics, b::Aesthetics)
for name in fieldnames(Aesthetics)
if issomething(getfield(b, name))
setfield(a, name, getfield(b, name))
end
end
nothing
end
# Serialize aesthetics to JSON.
# Args:
# a: aesthetics to serialize.
#
# Returns:
# JSON data as a string.
#
function json(a::Aesthetics)
join([string(a, ":", json(getfield(a, var))) for var in aes_vars], ",\n")
end
# Concatenate aesthetics.
#
# A new Aesthetics instance is produced with data vectors in each of the given
# Aesthetics concatenated, nothing being treated as an empty vector.
#
# Args:
# aess: One or more aesthetics.
#
# Returns:
# A new Aesthetics instance with vectors concatenated.
#
function concat(aess::Aesthetics...)
cataes = Aesthetics()
for aes in aess
for var in fieldnames(Aesthetics)
if var in [:xviewmin, :yviewmin]
mu, mv = getfield(cataes, var), getfield(aes, var)
setfield!(cataes, var,
mu === nothing ? mv :
mv == nothing ? mu :
min(mu, mv))
elseif var in [:xviewmax, :yviewmax]
mu, mv = getfield(cataes, var), getfield(aes, var)
setfield!(cataes, var,
mu === nothing ? mv :
mv == nothing ? mu :
max(mu, mv))
else
setfield!(cataes, var,
cat_aes_var!(getfield(cataes, var), getfield(aes, var)))
end
end
end
cataes
end
cat_aes_var!(a::Nothing, b::Nothing) = a
cat_aes_var!(a::Nothing, b) = copy(b)
cat_aes_var!(a, b::Nothing) = a
cat_aes_var!(a::Function, b::Function) = a === string || a == showoff ? b : a
function cat_aes_var!(a::Dict, b::Dict)
merge!(a, b)
a
end
function cat_aes_var!{T <: Base.Callable}(a::AbstractArray{T}, b::AbstractArray{T})
return append!(a, b)
end
function cat_aes_var!{T <: Base.Callable, U <: Base.Callable}(a::AbstractArray{T}, b::AbstractArray{U})
return append!(a, b)
end
# Let arrays of numbers clobber arrays of functions. This is slightly odd
# behavior, comes up with with function statistics applied on a layer-wise
# basis.
function cat_aes_var!{T <: Base.Callable, U}(a::AbstractArray{T}, b::AbstractArray{U})
return b
end
function cat_aes_var!{T, U <: Base.Callable}(a::AbstractArray{T}, b::AbstractArray{U})
return a
end
function cat_aes_var!{T}(a::AbstractArray{T}, b::AbstractArray{T})
return append!(a, b)
end
function cat_aes_var!{T, U}(a::AbstractArray{T}, b::AbstractArray{U})
V = promote_type(T, U)
if isa(a, DataArray) || isa(b, DataArray)
ab = DataArray(V, length(a) + length(b))
else
ab = Array(V, length(a) + length(b))
end
i = 1
for x in a
ab[i] = x
i += 1
end
for x in b
ab[i] = x
i += 1
end
return ab
end
function cat_aes_var!(a, b)
a
end
function cat_aes_var!{T}(xs::PooledDataVector{T}, ys::PooledDataVector{T})
newpool = T[x for x in union(Set(xs.pool), Set(ys.pool))]
newdata = vcat(T[x for x in xs], T[y for y in ys])
PooledDataArray(newdata, newpool, [false for _ in newdata])
end
# Summarizing aesthetics
# Produce a matrix of Aesthetic or Data objects partitioning the original
# Aesthetics or Data object by the cartesian product of xgroup and ygroup.
#
# This is useful primarily for drawing facets and subplots.
#
# Args:
# aes: Aesthetics or Data objects to partition.
#
# Returns:
# A Array{Aesthetics} of size max(1, length(xgroup)) by
# max(1, length(ygroup))
#
function by_xy_group{T <: Union(Data, Aesthetics)}(aes::T, xgroup, ygroup,
num_xgroups, num_ygroups)
@assert xgroup === nothing || ygroup === nothing ||
length(xgroup) == length(ygroup)
n = num_ygroups
m = num_xgroups
xrefs = xgroup === nothing ? [1] : xgroup
yrefs = ygroup === nothing ? [1] : ygroup
aes_grid = Array(T, n, m)
staging = Array(AbstractArray, n, m)
for i in 1:n, j in 1:m
aes_grid[i, j] = T()
end
if is(xgroup, nothing) && is(ygroup, nothing)
return aes_grid
end
function make_pooled_data_array{T, U, V}(::Type{PooledDataArray{T,U,V}},
arr::AbstractArray)
PooledDataArray(convert(Array{T}, arr))
end
make_pooled_data_array{T, U, V}(::Type{PooledDataArray{T,U,V}},
arr::PooledDataArray{T, U, V}) = arr
for var in fieldnames(T)
# Skipped aesthetics. Don't try to partition aesthetics for which it
# makes no sense to pass on to subplots.
if var == :xgroup || var == :ygroup||
var == :xtick || var == :ytick ||
var == :xgrid || var == :ygrid ||
var == :x_viewmin || var == :y_viewmin ||
var == :x_viewmax || var == :y_viewmax ||
var == :color_key_colors
continue
end
vals = getfield(aes, var)
if typeof(vals) <: AbstractArray
if !is(xgroup, nothing) && length(vals) != length(xgroup) ||
!is(ygroup, nothing) && length(vals) != length(ygroup)
error("Aesthetic $(var) must be the same length as xgroup or ygroup")
end
for i in 1:n, j in 1:m
staging[i, j] = similar(vals, 0)
end
for (i, j, v) in zip(Iterators.cycle(yrefs), Iterators.cycle(xrefs), vals)
push!(staging[i, j], v)
end
for i in 1:n, j in 1:m
if typeof(vals) <: PooledDataArray
setfield!(aes_grid[i, j], var,
make_pooled_data_array(typeof(vals), staging[i, j]))
else
if !applicable(convert, typeof(vals), staging[i, j])
T = eltype(vals)
if T <: ColorValue T = ColorValue end
da = DataArray(T, length(staging[i, j]))
copy!(da, staging[i, j])
setfield!(aes_grid[i, j], var, da)
else
setfield!(aes_grid[i, j], var,
convert(typeof(vals), copy(staging[i, j])))
end
end
end
else
for i in 1:n, j in 1:m
setfield!(aes_grid[i, j], var, vals)
end
end
end
aes_grid
end
function inherit!(a::Aesthetics, b::Aesthetics;
clobber=[])
clobber_set = Set{Symbol}(clobber)
for field in fieldnames(Aesthetics)
aval = getfield(a, field)
bval = getfield(b, field)
if field in clobber_set
setfield!(a, field, bval)
elseif aval === nothing || aval === string || aval == showoff
setfield!(a, field, bval)
elseif field == :xviewmin || field == :yviewmin
if bval != nothing && (aval == nothing || aval > bval)
setfield!(a, field, bval)
end
elseif field == :xviewmax || field == :yviewmax
if bval != nothing && (aval == nothing || aval < bval)
setfield!(a, field, bval)
end
elseif typeof(aval) <: Dict && typeof(bval) <: Dict
merge!(aval, getfield(b, field))
end
end
nothing
end