IterableTables.jl icon indicating copy to clipboard operation
IterableTables.jl copied to clipboard

Using JuliaDB/IndexedTables with Gadfly

Open cmcaine opened this issue 6 years ago • 4 comments

So I'm just trying to get Gadfly to plot an IndexedTable I've produced with the JuliaDB.table function.

The IterableTables documentation claims to support JuliaDB tables as data sources and Gadfly as a sink, but I'm doing it wrong and/or it doesn't work for me

Complete example:

julia> using IterableTables, JuliaDB, Gadfly

julia> t = table(1:10, (1:10) * 3, names = (:x, :y))
Table with 10 rows, 2 columns:
x   y
──────
1   3
2   6
3   9
4   12
5   15
6   18
7   21
8   24
9   27
10  30

julia> typeof(t)
IndexedTable{StructArrays.StructArray{NamedTuple{(:x, :y),Tuple{Int64,Int64}},1,NamedTuple{(:x, :y),Tuple{Array{Int64,1},Array{Int64,1}}}}}

julia> plot(t, x=:x, y=:y, Geom.point)
ERROR: MethodError: no method matching evalmapping(::IndexedTable{StructArrays.StructArray{NamedTuple{(:x, :y),Tuple{Int64,Int64}},1,NamedTuple{(:x, :y),Tuple{Array{Int64,1},Array{Int64,1}}}}}, ::Symbol)

```Closest candidates are:
  evalmapping(::Any, ::AbstractArray) at /home/colin/.julia/packages/Gadfly/09PWZ/src/mapping.jl:185
  evalmapping(::Any, ::Function) at /home/colin/.julia/packages/Gadfly/09PWZ/src/mapping.jl:186
  evalmapping(::Any, ::Distributions.Distribution) at /home/colin/.julia/packages/Gadfly/09PWZ/src/mapping.jl:187
  ...
Stacktrace:
 [1] evalmapping!(::Dict{Symbol,Any}, ::IndexedTable{StructArrays.StructArray{NamedTuple{(:x, :y),Tuple{Int64,Int64}},1,NamedTuple{(:x, :y),Tuple{Array{Int64,1},Array{Int64,1}}}}}, ::Gadfly.Data) at /home/colin/.julia/packages/Gadfly/09PWZ/src/mapping.jl:220
 [2] #plot#65(::Base.Iterators.Pairs{Symbol,Symbol,Tuple{Symbol,Symbol},NamedTuple{(:x, :y),Tuple{Symbol,Symbol}}}, ::Function, ::IndexedTable{StructArrays.StructArray{NamedTuple{(:x, :y),Tuple{Int64,Int64}},1,NamedTuple{(:x, :y),Tuple{Array{Int64,1},Array{Int64,1}}}}}, ::Type{Gadfly.Geom.PointGeometry}) at /home/colin/.julia/packages/Gadfly/09PWZ/src/Gadfly.jl:327
 [3] (::getfield(Gadfly, Symbol("#kw##plot")))(::NamedTuple{(:x, :y),Tuple{Symbol,Symbol}}, ::typeof(plot), ::IndexedTable{StructArrays.StructArray{NamedTuple{(:x, :y),Tuple{Int64,Int64}},1,NamedTuple{(:x, :y),Tuple{Array{Int64,1},Array{Int64,1}}}}}, ::Type{Gadfly.Geom.PointGeometry}) at ./none:0
 [4] top-level scope at none:0

cmcaine avatar Jun 27 '19 10:06 cmcaine

This does work for VegaLite, though, which is nice.

using VegaLite
t |> @vlplot(
    :point,
    x = :x,
    y = :y)

cmcaine avatar Jun 27 '19 11:06 cmcaine

Support for Gadfly.jl hasn't survived the transition to julia 1.0, I'm afraid...

I had a brief look how difficult it would be to reenable it, and the old strategy won't work anymore... I'll think a little how we could sort this out.

I think in the meantime, what should work is to convert the JuliaDB table to a DataFrame and then pass that to Gadfly. Not exactly elegant, but might do the trick.

davidanthoff avatar Jun 27 '19 13:06 davidanthoff

https://github.com/GiovineItalia/Gadfly.jl/issues/1292

davidanthoff avatar Jun 27 '19 13:06 davidanthoff

I think in the meantime, what should work is to convert the JuliaDB table to a DataFrame and then pass that to Gadfly. Not exactly elegant, but might do the trick.

Yes, this is what I ended up doing:

import IndexedTables
import Gadfly

Gadfly.plot(data_source::IndexedTables.IndexedTable, 
	    elements::Union{Array{Gadfly.Layer,1}, Function, Gadfly.Element, Gadfly.Theme, Type}...;
	    mapping...) =
  Gadfly.plot(data_source |> DataFrame, elements...; mapping...)

cmcaine avatar Jun 30 '19 11:06 cmcaine