Plot cell mark: Order categorical data

Hi,

I would like to order categorical data on a band axis.
The order should be specific (maybe provided by an array or so?).

cell_cat

Recoding the strings as ints didn’t help since the ints were still sorted as strings. I guess because of the band type of the x-axis.

Code:

Plot.plot({
  color: { scheme: "YlGnBu" },
  x: {
    type: "band",
    label: "Available Imaging Data",
    paddingInner: 0.1
  },
  marks: [Plot.cell(data, Plot.groupX({ fill: "count" }, { x: "modality" }))]
})

You can find the notebook here:

https://observablehq.com/d/6f96e2ba61609a50
How could this be solved?
Thanks.

1 Like

I’m not sure what order you’re looking for but, generally, you can sort a categorical axis by specifying its domain. To reverse the order shown in your picture, for example, you could do the following:

Plot.plot({
  color: { scheme: "YlGnBu" },
  x: {
    type: "band",
    label: "Available Imaging Data",
    paddingInner: 0.1,
    domain: [
      "T2-FLAIR","T2","T1","SWI","MRA","DWI","CTA",
      "CT-2","CT-1","ADC","3D-T1","3D-FLAIR"
    ]},
  marks: [Plot.cell(data, Plot.groupX({ fill: "count" }, 
     { x: "modality" }))]
})

Thanks, @mcmcclur!
That works great.
Now, when I see your solution I remember it from somewhere.
I didn’t find it in the sort/oder related documentation… must have searched
in the wrong place.
Cheers