Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

The Dataset documentation doesn't indicate how to label a Histogram with the key.

titanic = 
  ExampleData[{"Dataset", "Titanic"}][All, {"survived" ->(# /. {True -> 1, False -> 0} &)}]

enter image description here

titanic[Histogram, #] & /@ {"age", "survived"}


enter image description here

(Note, unlike functions such as Counts, which can operate on tuples of keys (ie, counts the products), it doesn't seem possible to use the syntax: titanic[Histogram, {"age", "survived"}]).

How can a Key be passed programmatically as argument to Histogram (or other operators) in order to label it, eg "age", "survived" ?

Of course it's possible to project individual columns with KeyTake and then histogram but that's not the same as operating on the data.

share|improve this question
Perhaps titanic[Composition[Histogram[#, ChartLabels -> Automatic] &, Pivot[#, 2] & ], {"age"}]. (but Pivot is probably going away soon) –  Rojo Jul 24 at 0:50
@Rojo, Pivot isn't documented and its definition is unreadably full of macros & `PackagePrivate parts. Where did you read about it? –  alancalvitti Jul 30 at 18:11
It used to be documented before the final release. And it will go away, but I am hoping as it goes away, some other function will take its job because I find it pretty useful –  Rojo Jul 30 at 18:32
Do you mean something like titanic[Histogram[#[[All, 1]], PlotLabel -> First@Keys@#[[1]]] &, {#}] & /@ {"age", "survived"} ? –  Ronald Monson Jul 31 at 22:32

3 Answers 3

You don't have to modify what you already tried by very much.

  With[{key = #}, 
    titanic[Histogram[#, PlotLabel -> key] &, key]] & /@ {"age", "survived"}]



The comment by the OP is not clear to me. I offer up the following as my best interpretation of what he is asking for. I hope I'm not being obtuse.

  With[{val = #[[1]], lbl = #[[2]]}, 
      titanic[Select[#survived == val &]]
        [Histogram[#, {0, 80, 5}, PlotLabel -> lbl]&, "age"]]& 
      {{1, "Survivors by Age"}, {0, "Non-survivors by Age"}}]


share|improve this answer
+1, yet this method also is not a pure operator, since it partly straddles Dataset. –  alancalvitti Jul 21 at 19:06
can you redefine this method as a Query operator? –  alancalvitti Jul 29 at 19:09

Hopefully I don't misunderstand the question. It's not fancy or clever but basic programming still works:

f[x_] := Labeled[Histogram @ #, x] &;

titanic[f[#], #] & /@ {"age", "survived"}

enter image description here

The same thing as a Table:

  titanic[Histogram[#] ~Labeled~ x &, x],
  {x, {"age", "survived"}}
share|improve this answer
@m_goldberg Weird! There are no quote marks on screen but they appear when I export (Save Selection As...) to PNG. I didn't even notice that before. –  Mr.Wizard Jul 21 at 7:39
Is it not possible to redefine your solution inline? –  alancalvitti Jul 21 at 19:09
@alancalvitti What do you mean by "inline" in this case? Would you prefer titanic[(x \[Function] Labeled[Histogram@#, x] &)[#], #] & /@ {"age", "survived"} ? –  Mr.Wizard Jul 22 at 1:56
by inline I mean as Query operators. –  alancalvitti Jul 29 at 19:08

How about

Histogram[titanic[[All, #]], ChartLabels -> Placed[#, Above], 
    ImageSize -> 200] & /@ {"age", "survived"} // Row

Gives this

enter image description here

share|improve this answer
titanic[[All,#]] is a projection like I mention in the last paragraph of my Q, it's not an operator applied to Dataset. –  alancalvitti Jul 21 at 17:38

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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