|
| 1 | +@require DataFrames begin |
| 2 | + |
| 3 | + """ |
| 4 | + Convert an AxisArray into a long-format DataFrame. |
| 5 | + |
| 6 | + ```julia |
| 7 | + DataFrame(a::AxisArray) |
| 8 | + ``` |
| 9 | + |
| 10 | + ### Arguments |
| 11 | + |
| 12 | + * `a::AxisArray` |
| 13 | + |
| 14 | + ### Returns |
| 15 | + |
| 16 | + * `::DataFrame` : a DataFrame view into the AxisArray; columns are |
| 17 | + added for each axis plus one column for the data (named `:data`). |
| 18 | +
|
| 19 | + """ |
| 20 | + function DataFrames.DataFrame(A::AxisArray) |
| 21 | + colnames = Symbol[axisnames(A)..., :data] |
| 22 | + sz = [1; size(A)...; 1] |
| 23 | + columns = Any[DataFrames.RepeatedVector(a, prod(sz[1:i]), prod(sz[i+2:end])) for (i,a) in enumerate(axes(A))] |
| 24 | + push!(columns, sub(A.data, 1:prod(sz))) |
| 25 | + DataFrames.DataFrame(columns, colnames) |
| 26 | + end |
| 27 | + |
| 28 | +end |
| 29 | + |
| 30 | + |
| 31 | +@require Gadfly begin |
| 32 | + |
| 33 | + import DataArrays |
| 34 | + ## Low-level code patching |
| 35 | + function Gadfly.Scale.discretize_make_pda(values::DataFrames.RepeatedVector, levels=nothing) |
| 36 | + if levels == nothing |
| 37 | + return DataArrays.PooledDataArray(values) |
| 38 | + else |
| 39 | + return DataArrays.PooledDataArray(values[:], levels) |
| 40 | + end |
| 41 | + end |
| 42 | + |
| 43 | + """ |
| 44 | + Plot an AxisArray using Gadfly. |
| 45 | + |
| 46 | + ```julia |
| 47 | + Gadfly.plot(A::AxisArray, args...; kargs...) |
| 48 | + ``` |
| 49 | + |
| 50 | + ### Arguments |
| 51 | + |
| 52 | + * `A::AxisArray` |
| 53 | +
|
| 54 | + All other arguments are passed to Gadfly.plot. |
| 55 | + |
| 56 | + ### Examples |
| 57 | + |
| 58 | + ```julia |
| 59 | + using Gadfly |
| 60 | + A = AxisArray(reshape([1:24], 12,2), (.1:.1:1.2, [:a,:b])) |
| 61 | + plot(A, x = :row, y = :data, color = :col) |
| 62 | + ``` |
| 63 | +
|
| 64 | + """ |
| 65 | + Gadfly.plot(A::AxisArray, args...; kwargs...) = Gadfly.plot(DataFrames.DataFrame(A), args...; kwargs...) |
| 66 | + |
| 67 | +end |
0 commit comments