3
$\begingroup$

I'm trying to add to the BarChart I have generated from a Dataset however, I seem to be stuck on a couple of points. Firstly given the below syntax, where am I supposed to add in the BarChart options?

test = Dataset[{
    <|"num" -> 0|>, <|"num" -> 0|>, <|"num" -> 1|>, <|"num" -> 2|>, <|
     "num" -> 0|>, <|"num" -> 0|>, <|"num" -> 1|>, <|"num" -> 2|>, <|
     "num" -> 2|>, <|"num" -> 1|>, <|"num" -> 2|>, <|"num" -> 3|>, <|
     "num" -> 3|>, <|"num" -> 5|>, <|"num" -> 4|>, <|"num" -> 5|>, <|
     "num" -> 0|>, <|"num" -> 5|>}];
test[Counts, #num &]
test[Counts /* BarChart, #num &]

Secondly, I know I can add in

...,ChartLabels->{"list","of","things"} ...

but I really want to pass the output of the Counts so that I can do it programatically for a number of different Datasets.

TIA

$\endgroup$
4
$\begingroup$

You need to create a Pure Function with BarChart that takes the results of your query and has the options you want. Use Keys to get the chart labels from Counts

Inline

test[Counts /* (BarChart[#, ChartLabels -> Keys[#]] &), #num &]

Defined

myChart = BarChart[#, ChartLabels -> Keys[#]] &;
test[Counts /* myChart, #num &]

Both methods give

enter image description here

Hope this helps.

Note: You can also replace Keys[#] with Automatic since BarChart understands Associations. In fact using Automatic is better if you have multiple levels.

test[Counts /* (BarChart[#, ChartLabels -> Automatic] &), #num &]

and

myChart = BarChart[#, ChartLabels -> Automatic] &;
test[Counts /* myChart, #num &]
$\endgroup$
  • $\begingroup$ @infinityplusb See update with Keys. $\endgroup$ – Edmund Jul 28 '16 at 13:11
  • $\begingroup$ Ah, I tried the Keys thing, but without the # Thanks for the link too, that should make some good reading. :) $\endgroup$ – infinityplusb Jul 28 '16 at 13:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.