livebook-dev / vega_lite

Elixir bindings for Vega-Lite

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Annotating heatmap

lambdaofgod opened this issue · comments

I'm trying to create a heatmap that will contain text annotation for each cell. Is it possible to create this using VegaLite.mark?
If not, is it possible to add text layer to a heatmap? How?

What I tried:

alias VegaLite, as: VL

items = ["a", "b", "c"]
interacting_items = [{"a", "b"}, {"a", "c"}]
interaction_data = 
  for x <- items,  y <- items do
    score = 
      cond do
        {x, y} in interacting_items -> 1
        true -> 0
      end
    %{"x" => x, "y" => y, "xy" => x <> y, "score" => score}
  end


VL.new(width: 500, height: 500)
|> VL.Data.heatmap(interaction_data, x: "x", y: "y", text: "y",
  color: [field: "score", scale: [scheme: "reds"]])
|> VL.mark("text", "xy")

The error I get:

** (ArgumentError) cannot add mark to the view, because it is already a multi-view specification (has the :layer key defined)
    (vega_lite 0.1.8) lib/vega_lite.ex:1028: anonymous fn/4 in VegaLite.validate_blank_view!/2
    (elixir 1.14.2) lib/enum.ex:2468: Enum."-reduce/3-lists^foldl/2-0-"/3
    (vega_lite 0.1.8) lib/vega_lite.ex:1027: VegaLite.validate_blank_view!/2
    (vega_lite 0.1.8) lib/vega_lite.ex:612: VegaLite.mark/3
    (stdlib 3.15) erl_eval.erl:685: :erl_eval.do_apply/6
    (elixir 1.14.2) lib/module/parallel_checker.ex:107: Module.ParallelChecker.verify/1

Hey, does text: [field: "xy", type: :nominal] give you the result you need?

It works. Thanks!