# Plot Dataset items

Assume, I have a Dataset like this

ds = Dataset[<|
"Alice" -> <|"food" -> {"apple", "orange"},
"drink" -> {"water", "tea"}|>,
"Bob" -> <|"food" -> {"apple", "banana"},
"drink" -> {"tea", "coffee"}|> ,
"Eve" -> <|"drink" -> {"tea"}|>|>]


This data set consists of three individuals and each has observations for food and drink they consume.

I now want to, for example, plot the percentage of tea drinkers (100%), or the percentage (or number) of banana eaters (33%, 1 out of 3). Ideally this would be a BarChart like this

where the first three a, b, c's would be apples, bananas, or oranges. The second group of a, b, c would be food items.

Please note that there can be missings (e.g. Eve doesn't have food items but she counts towards the three persons)

I tried something like

ds[BarChart, {All, All, "food"}]


but it doesn't work.

• ds[BarChart[#, ChartLabels -> Automatic] &@*Map[Total]@*Transpose@* DeleteMissing@*Map[Counts], "food"] and ds[BarChart[#, ChartLabels -> Automatic] &@*Map[Total]@*Transpose@* DeleteMissing@*Map[Counts], "drink"]?
– kglr
Nov 10, 2021 at 11:22
• @kglr thanks, that's pretty close to what I want. How can I get percentages instead of counts? Nov 10, 2021 at 12:04

ds2 = ds[Map[Merge[100 Total[#]/Length[ds] &] @* Values @* DeleteMissing] @*
Transpose @* Map[Map[Counts]], {"food", "drink"}]


BarChart[Map[KeyValueMap[Labeled[#2, #] &]] @ #,
ChartLabels -> {Callout[Automatic], Automatic}] & @ ds2


Alternatively,

operator = BarChart[#, ChartLabels -> {Callout[Automatic], Automatic}] & @*
Map[KeyValueMap[Labeled[#2, #] &] @*
Merge[100 Total[#]/Length[ds] &] @*
Values @*
DeleteMissing] @*
Transpose @*
Map[Map[Counts]];

ds[operator, {"food", "drink"}]


op = Map[BarChart[#,  ChartLabels -> Automatic] & @*
Merge[100 Total[#] / Length[ds] &] @*
Values @*
DeleteMissing] @*
Transpose @*
Map[Map[Counts]];

ds3 = ds[op]


ds3["food"]


ds3[Row[{Show[#"food", PlotLabel -> "food", ImageSize -> 300],
Show[#"drink", PlotLabel -> "drink", ImageSize -> 300]}] &]
`