Dataset's documentation shows how to leverage GroupBy operator at multiple levels

titanic = ExampleData[{"Dataset", "Titanic"}];

titanic[GroupBy[Key["sex"]], GroupBy[Key["class"]], ratio, "survived"]

Taking a step further, can generate summary graphics in one line:

titanic[GroupBy[Key["sex"]], GroupBy[Key["class"]], 
 Counts /* 
  KeySort /* (PieChart[#, SectorOrigin -> {Automatic, 1}, 
     ChartLegends -> Automatic] &), "survived"]

enter image description here

KeySort insures uniform color assignment to True and False; without it, some charts will have inverted colors.

Since all charts share the same interpretation, the 6 ChartLegends are redundant.

Is there an option for a single legend to appear, outside the frame of the Dataset?

Wouldn't it be great if the various Chart functions returned associations like <|"chart"->Graphics[...], "legend"->Graphics[...] |> that can be more easily processed.

Also, BarChart[#, ChartLegends->Automatic] inside Dataset doesn't work, but adding manual values, eg ChartLegends->{"T","F"} works.

  • 1
    $\begingroup$ I'm really excited by the prospect of having Histogram, BarChart, PieChart etc operate on nested data in precisely the way you are doing it 'manually' here. And then doing things like choosing the same bins across each category would be totally automatic, as would ensuring there is only one legend, colors are consistent, etc... All of this is possible, and in fact even planned, it just takes time. $\endgroup$ Jul 27, 2014 at 0:06

1 Answer 1


I am still largely mystified by Dataset as database querying is alien to me and the Dataset internals seem rather opaque, but at least for the given example this appears to be a solution:

titanic = ExampleData[{"Dataset", "Titanic"}];

panel = titanic[GroupBy[Key["sex"]], GroupBy[Key["class"]], 
   Counts /* KeySort /* (PieChart[#, SectorOrigin -> {Automatic, 1}, 
       ChartLegends -> Automatic] &), "survived"];

  panel[[All, All, 1]],
  panel[[1, 1, 2, 1]]

enter image description here

  • $\begingroup$ Thanks Mr.Wizard, this is usable. I looked at PieChart's FullForm to try to extract the legend but saw nested Dynamic and other wrappers and dropped it. Are the panel Parts generic for BarChart, Histogram etc? $\endgroup$ Jul 26, 2014 at 17:14
  • $\begingroup$ Dataset and Association are atomic for now. Hopefully future versions will allow pattern matching. $\endgroup$ Jul 26, 2014 at 17:16
  • $\begingroup$ @alancalvitti Apparently it's not truly atomic, though that's the term we've been using. Replace and ReplaceAll currently do not work but as you can see Part does, and so does Cases I believe. I didn't check to see if this works for other plot types; if it doesn't please let me know and I'll see what can be done about it. $\endgroup$
    – Mr.Wizard
    Jul 26, 2014 at 17:33
  • $\begingroup$ Part currently only works read only, can't Set. WriteAtomicQ? $\endgroup$ Jul 26, 2014 at 17:43
  • $\begingroup$ How difficult would it be to modify the built in charts to return Associations with consistently named components (main graphics, Legend) and user-named graphics layers? For most tasks I typically build graphics from primitives and am considering similar functionality. $\endgroup$ Jul 26, 2014 at 17:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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