Plot Geo: fill by color channel domain

I am using Plot Geo and am trying to sync the state party color domain with the total count of legislative bills per US state. I have a BarY chart above that represents the dataset with the color channel domain/range defined. When I add the same domain/range (commented out) to the Plot Geo there is no fill…

The end result should be for example - if the topic guns is selected - TN should be red and the darkest value compared to AK. Likewise, NY should be the darkest blue value compared to OR

Any help will be appreciated. Thanks.

When you group with Plot.groupZ() by z: "state", you have to set the correct reducer (–> how to aggregate within each group)

  • in your code, the reducer was fill:"count" which is what you want for a quantitative scale. But now you changed the domain to expect state names instead (not integers). That’s why the color scale could not compute a color.

  • So you have to change the reducer to fill:"first" and also add to the input the referenced column fill:"state". Now Plot knows to apply the aggregation "first" to the column "state" (pick the first element of the array of elements of this group)


    Plot.geo(
      flat_class,
      Plot.groupZ(
        {
          fill: "first",
          geometry: ([state]) =>
            states.features.find((d) => d.properties.name === state)
        },
        {
          geometry: "state",
          z: "state",
          fill: "state",
          title: (d) => d.identifier
        }
      )
    ),

See OpenStates export test / ee2dev | Observable.

P.s you don’t need to hard code the domain and range to replicate a scale from another plot. Instead, you can assign the previous Plot to a named cell and then reference its scale by …plot.scale(„color“) to access its domain and range.

1 Like