# One ChartLegend for multiple charts in Dataset?

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"]


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.

-
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. – Taliesin Beynon Jul 27 '14 at 0:06

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"];

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


-
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? – alancalvitti Jul 26 '14 at 17:14
Dataset and Association are atomic for now. Hopefully future versions will allow pattern matching. – alancalvitti Jul 26 '14 at 17:16
@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. – Mr.Wizard Jul 26 '14 at 17:33
Part currently only works read only, can't Set. WriteAtomicQ? – alancalvitti Jul 26 '14 at 17:43
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. – alancalvitti Jul 26 '14 at 17:45