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

how to deal with 2D data / heatmaps?

Open tbenst opened this issue 4 years ago • 3 comments

This is less an AOG issue than a general irk with grammar of graphics. Dealing with 2D data is a bit unwieldly.

matrix = rand(100,100)
matrix_df = DataFrame(matrix, string.(collect(1:size(matrix,2))))
matrix_df[!,:y] = 1:size(matrix,1)
matrix_df = stack(matrix_df, 1:size(matrix,2))
rename!(matrix_df, :variable => :x)
matrix_df[!, :x] = parse.(Int,matrix_df[!, :x])
matrix_df
p = data(matrix_df)  * mapping(:x, :y, :value) * visual(Heatmap) |> draw

is a lot of work compared to:

using Plots
matrix = rand(100,100)
heatmap(matrix)

tbenst avatar Aug 19 '21 00:08 tbenst

You are not forced to have your data in a table in AoG, so for example with the "pre-grouped" syntax (see https://github.com/JuliaPlots/AlgebraOfGraphics.jl/issues/212#issue-917479265 and http://juliaplots.org/AlgebraOfGraphics.jl/dev/gallery/gallery/data%20manipulations/pre_grouped_data/#Pre-grouped-data), you could do

x, y, z = 1:10, 1:10, rand(10, 10)
mapping([x] => "x", [y] => "y", [z] => "z") * visual(Heatmap) |> draw

Ideally all the possible syntaxes (basically there are the 4 combinations of pre-grouped vs not pre-grouped and with a dataset vs without a dataset) should be supported easily. For now only pre-grouped without dataset and not pre-grouped with dataset are supported, but hopefully it should be easier to get all of them to work. Heatmap (or in general these plots with 2-d data) are definitely a good use cases for that).

piever avatar Aug 19 '21 12:08 piever

That’s awesome! Thank you. Might I suggest that the simple example you just posted be added to the pre-grouped documentation? It’s very ergonomic and I didn’t gather that was possible until your kind reply

tbenst avatar Aug 19 '21 17:08 tbenst