Plot: Example of option transform composition


I was wondering if there are any examples of option transform composition. For example, let’s say I’ve got a dataset with 1K objects and I want to categorically group the data by count and sort in descending order. That part, I get. The group transformation is doing what I need. However, I don’t want to plot all the grouped categories, I only want to plot the first 10. I assume that to do what I want, I’d use a group transform (by count) followed by a simple select transform (first 10). Is composing option transforms the correct way to do this, or am I misunderstanding how option transforms work?


1 Like

I think you can accomplish what you want just by adding limit: 10 to your sort options.
for example: Plotting Wikipedia Recent Changes / Ian Johnson / Observable

if you try it and it’s not quite right, feel free to share a notebook and we can take a closer look!

1 Like

If I could buy you a beer, I would! That did the trick for my immediate need. Thanks so much. However, I’m still curious about option transform composition in general. The documentation talks about passing an options object through more than one transform before passing it to a mark.


Based on this data, I use the index (i) to test if I have read less than 10 elements from the dataset and return the data if true or null if not.

Plot.barY(alphabet.filter((d,i) => (i<10)?d:null), {x: "letter", y: "frequency"}).plot()

Here is an example of using selectLast on a stackY transform for text marks to label a stacked area chart: Instead of a stacked area chart, try an ordered 'faceted' chart. / Zan / Observable

As far as I understand it, transforms always take a set of options as input and return a set of options as output. So you can chain them as long as it makes sense. A group transform lets you specify how you want to output a certain optional channel (like x: “sum” or x: “mean”) so if you wanted to select the largest value in each group you would do selectMaxX on the group and that is plugged into the mark

@enjalot, thanks for pointing me to an example. @hellonearthis thanks too for your reply. In my case, I don’t want to filter the dataset before applying the group transform. Instead, I want to group (by count) the entire dataset and then limit the output of the sort to the top N items. If my understanding is correct, your approach would filter the dataset before it is passed to the group transform. Admittedly, my understanding could be flawed.

Ian’s suggestion of adding a limit: to the sort option did the trick in this case.

I didn’t understand the problem fully. Glad you got it resolved.