# Categorical heatmap

**URL:** https://discourse.julialang.org/t/categorical-heatmap/56931
**Category:** Visualization
**Tags:** makie, algebraofgraphics
**Created:** [March 11, 2021, 4:58am UTC](https://discourse.julialang.org/t/categorical-heatmap/56931 "2021-03-11T04:58:51Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)
#### Post date: [March 11, 2021, 4:58am UTC](https://discourse.julialang.org/t/categorical-heatmap/56931/1 "2021-03-11T04:58:51Z")

</div>

Can I use AoG for a categorical heatmap?

```julia
using RDatasets
let
  diamonds = dataset("ggplot2", "diamonds")
  p = data(diamonds) * mapping(:Cut => categorical, :Color => categorical, color=:Price) * visual(Heatmap)
  draw(p)
end
ERROR: LoadError: There was no `AbstractPlotting.convert_arguments` overload found for
the plot type Combined{AbstractPlotting.heatmap,Tuple{Array{Int16,1},Array{Int16,1}}}, or its conversion trait AbstractPlotting.SurfaceLike().
The arguments were:
(Array{Int16,1}, Array{Int16,1})

To fix this, define `AbstractPlotting.convert_arguments(::Combined{AbstractPlotting.heatmap,Tuple{Array{Int16,1},Array{Int16,1}}}, ::Array{Int16,1}, ::Array{Int16,1})`.

```

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [March 11, 2021, 7:47pm UTC](https://discourse.julialang.org/t/categorical-heatmap/56931/2 "2021-03-11T19:47:48Z")

</div>

Do not know about [AlgebraOfGraphics.jl](https://github.com/JuliaPlots/AlgebraOfGraphics.jl) but in case it helps, [VegaLite.jl](https://github.com/queryverse/VegaLite.jl) can be used for this interesting task:

```julia
using VegaLite, RDatasets

diamonds = RDatasets.dataset("ggplot2", "diamonds")

diamonds |>
    @vlplot(
        :rect, width=300, height=200,
        x="Cut:n", y="Color:n",
        color={"mean(Price)", title="Mean price", scale={scheme="blueorange"}},
        config={view={stroke="transparent"}}
    )

```

 ![vegalite_categorical_heatmap](https://global.discourse-cdn.com/julialang/original/3X/1/e/1e54f0586c63395678108df00b3354006869365c.png)

```julia
julia> diamonds
53940×10 DataFrame
   Row │ Carat Cut Color Clarity Depth Table Price X Y Z       
       │ Float64 Cat… Cat… Cat… Float64 Float64 Int32 Float64 Float64 Float64 
───────┼────────────────────────────────────────────────────────────────────────────────────────
     1 │ 0.23 Ideal E SI2 61.5 55.0 326 3.95 3.98 2.43
     2 │ 0.21 Premium E SI1 59.8 61.0 326 3.89 3.84 2.31
     3 │ 0.23 Good E VS1 56.9 65.0 327 4.05 4.07 2.31
   ⋮ │ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮
 53938 │ 0.7 Very Good D SI1 62.8 60.0 2757 5.66 5.68 3.56
 53939 │ 0.86 Premium H SI2 61.0 58.0 2757 6.15 6.12 3.74
 53940 │ 0.75 Ideal D SI2 62.2 55.0 2757 5.83 5.87 3.64
                                                                              53923 rows omitted

```

---

<div class="post-metadata">

### Author: ![piever](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/piever/32/1815_2.png) [@piever](https://discourse.julialang.org/u/piever)
#### Post date: [March 12, 2021, 9:05am UTC](https://discourse.julialang.org/t/categorical-heatmap/56931/3 "2021-03-12T09:05:12Z")

</div>

If I understand correctly you want to basically create a table with as `x` and `y` axes `Cut` and `Color` and content the mean `Price`, and then draw a heatmap of that. Creating this objects is done with the analysis `reducer`.

However here this won’t quite work, because this would then call a heatmap with 3 vectors (which is essentially a “non-full” heatmap). I wonder if this, ie `heatmap(x::Vector, y::Vector, z::Vector)` is something Makie can support out of the box (using some default value for missing `x, y` combinations), or whether it needs to be handled on the side of AlgebraOfGraphics.

---

<div class="post-metadata">

### Author: ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)
#### Post date: [March 12, 2021, 10:30am UTC](https://discourse.julialang.org/t/categorical-heatmap/56931/4 "2021-03-12T10:30:31Z")

</div>

@piever Can you say why the code I tried doesn’t work?

---

<div class="post-metadata">

### Author: ![piever](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/piever/32/1815_2.png) [@piever](https://discourse.julialang.org/u/piever)
#### Post date: [March 12, 2021, 11:19am UTC](https://discourse.julialang.org/t/categorical-heatmap/56931/5 "2021-03-12T11:19:58Z")

</div>

AlgebraOfGraphics is essentially a way to do some preprocessing before sending data to Makie for the plot. In your case, you end up calling `heatmap` with the following signature:

```julia
heatmap(diamonds.cut::Vector, diamonds.color::Vector, color::Vector=diamonds.Price)

```

but `heatmap(x, y, color=z)` is not supported in Makie. Beyond that, there is no way to guess that you actually want the mean price given the `x` and `y` variable. Taking the mean value can be handled by AoG with `reducer`. For example, here is the 1D case:

```julia
using AlgebraOfGraphics, CairoMakie, RDatasets
using AlgebraOfGraphics: reducer

diamonds = dataset("ggplot2", "diamonds")
p = data(diamonds) *
    mapping(:Cut => categorical, :Price) *
    reducer() *
    visual(BarPlot)
draw(p)

```

 ![bar](https://global.discourse-cdn.com/julialang/original/3X/9/6/96dd688c382ecab8dea90870cc0ad8fbfb4387a3.png)

I was imagining that the following

```julia
diamonds = dataset("ggplot2", "diamonds")
p = data(diamonds) *
    mapping(:Cut => categorical, :Color => categorical, :Price) *
    reducer() *
    visual(Heatmap)
draw(p)

```

would just work, but unfortunately it looks like `heatmap(x::Vector, y::Vector, z::Vector)` is not supported out of the box in Makie. I can check over at Makie if it makes sense to add it there, otherwise the small reshape necessary (from the “long” to the “matrix” format) can be handled in AoG.

UPDATE: the syntax

```julia
diamonds = dataset("ggplot2", "diamonds")
p = data(diamonds) * mapping(:Cut => categorical, :Color => categorical, :Price) * reducer
draw(p)

```

now works on AlgebraOfGraphics master branch.
